-
Notifications
You must be signed in to change notification settings - Fork 323
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
43 additions
and
30 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,38 @@ | ||
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 | ||
|
||
class << self | ||
alias_method :include_root_in_json_getter, :include_root_in_json | ||
|
||
# Rails creates include_root_in_json as a class attribute | ||
# but Her previously had its own implementation combining | ||
# the getter and setter. This provides compatibility. | ||
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 | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
9d528ca
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is/was
include_root_in_json?()
called anywhere else? could it be considered part of a public api? If so, we should probably alias or define that method again shouldn't we?(Unless I've totally missed the point)
9d528ca
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's marked
@private
so I figured it was fair game for removal. It was used inincluded_root_element
and I dealt with that.9d528ca
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry - missed the
@private
comment in the diff. Yeah I agree, seems ok to remove.