-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Utilise ActiveModel::Serializers::JSON in Her models
This makes as_json and to_json behave like they do under ActiveRecord, excluding associations unless they are explicitly included. This is necessary to avoid infinite loops. Unfortunately this caused Her's include_root_in_json code to collide with ActiveModel's include_root_in_json code. I removed Her's getter/setter method in favour of ActiveModel's class attribute but also provided a compatibility fix.
- Loading branch information
Showing
5 changed files
with
54 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module Her | ||
module Model | ||
module Serialization | ||
extend ActiveSupport::Concern | ||
include ActiveModel::Serializers::JSON | ||
|
||
def serializable_hash(options = nil) | ||
options = options.try(:dup) || {} | ||
|
||
options[:except] = Array(options[:except]).map(&:to_s) | ||
options[:except] |= self.class.association_names.map(&:to_s) | ||
|
||
super(options) | ||
end | ||
|
||
included do | ||
# Rails 3 defaulted to true but Her has always defaulted to | ||
# false. This can be dropped when Rails 3 support is dropped. | ||
self.include_root_in_json = false | ||
|
||
# Rails creates include_root_in_json as a class attribute but | ||
# Her previously had its own implementation combining the | ||
# getter and setter. This monstrosity tries to achieve | ||
# compatibility in the simplest way possible. | ||
class << self | ||
realias = proc do | ||
alias_method :include_root_in_json_getter, :include_root_in_json | ||
|
||
def include_root_in_json(value = nil) | ||
if value.nil? | ||
include_root_in_json_getter | ||
else | ||
self.include_root_in_json = value | ||
end | ||
end | ||
end | ||
|
||
realias.call | ||
alias_method :include_root_in_json_without_realias, :include_root_in_json= | ||
|
||
define_method :include_root_in_json= do |value| | ||
include_root_in_json_without_realias value | ||
realias.call | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.