Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Person trying to serialize non-existing methods in User #10575

Merged
merged 4 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ def self.fields_edit_requestable
methods: ["url", "country_iso2"],
}.freeze

USER_COMMON_SERIALIZE_OPTIONS = {
only: ["name", "gender"],
methods: ["country_iso2"],
include: [],
}.freeze

def personal_records
[self.ranksAverage, self.ranksSingle].compact.flatten
end
Expand Down Expand Up @@ -308,6 +314,7 @@ def serializable_hash(options = nil)

# If there's a user for this Person, merge in all their data,
# the Person's data takes priority, though.
(user || User.new).serializable_hash(options).merge(json)
user_override_options = USER_COMMON_SERIALIZE_OPTIONS.merge_union(options)
(user || User.new).serializable_hash(user_override_options).merge(json)
end
end
8 changes: 8 additions & 0 deletions config/initializers/monkey_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ def xss_aware_join(delimiter = '')
end
end

Hash.class_eval do
def merge_union(other = nil)
self.to_h do |key, value|
[key, value & (other&.fetch(key.to_s, []) || [])]
end
end
end

ActiveSupport::Duration.class_eval do
def in_seconds
self.to_i
Expand Down
Loading