-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
add if/unless options to attribute #1266
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,8 @@ def self.digest_caller_file(caller_line) | |
self._attributes_keys ||= {} | ||
class_attribute :_links # @api private : links definitions, @see Serializer#link | ||
self._links ||= {} | ||
|
||
class_attribute :_attributes_conditions # @api private : maps condition of attributes | ||
self._attributes_conditions ||= {} | ||
serializer.class_attribute :_cache # @api private : the cache object | ||
serializer.class_attribute :_fragmented # @api private : @see ::fragmented | ||
serializer.class_attribute :_cache_key # @api private : when present, is first item in cache_key | ||
|
@@ -76,6 +77,7 @@ def self.inherited(base) | |
base._attributes = _attributes.dup | ||
base._attributes_keys = _attributes_keys.dup | ||
base._links = _links.dup | ||
base._attributes_conditions = _attributes_conditions.dup | ||
base._cache_digest = digest_caller_file(caller_line) | ||
super | ||
end | ||
|
@@ -95,10 +97,11 @@ def self.link(name, value = nil, &block) | |
# class AdminAuthorSerializer < ActiveModel::Serializer | ||
# attributes :id, :name, :recent_edits | ||
def self.attributes(*attrs) | ||
options = attrs.last.class == Hash ? attrs.pop : {} | ||
attrs = attrs.first if attrs.first.class == Array | ||
|
||
attrs.each do |attr| | ||
attribute(attr) | ||
attribute(attr, options) | ||
end | ||
end | ||
|
||
|
@@ -115,6 +118,17 @@ def self.attribute(attr, options = {}) | |
_attributes_keys[attr] = { key: key } if key != attr | ||
_attributes << key unless _attributes.include?(key) | ||
|
||
[:if, :unless].each do |switch| | ||
next unless options.key?(switch) | ||
condition = [switch, options] | ||
|
||
if _attributes_conditions[condition] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not simply store a pair |
||
_attributes_conditions[condition] << key | ||
else | ||
_attributes_conditions[condition] = [key] | ||
end | ||
end | ||
|
||
ActiveModelSerializers.silence_warnings do | ||
define_method key do | ||
object.read_attribute_for_serialization(attr) | ||
|
@@ -247,7 +261,9 @@ def json_key | |
# Return the +attributes+ of +object+ as presented | ||
# by the serializer. | ||
def attributes(requested_attrs = nil) | ||
self.class._attributes.each_with_object({}) do |name, hash| | ||
attributes = self.class._attributes - excluded_attribute_keys | ||
|
||
attributes.each_with_object({}) do |name, hash| | ||
next unless requested_attrs.nil? || requested_attrs.include?(name) | ||
if self.class._fragmented | ||
hash[name] = self.class._fragmented.public_send(name) | ||
|
@@ -263,8 +279,36 @@ def links | |
self.class._links | ||
end | ||
|
||
def excluded_attribute_keys | ||
self.class | ||
._attributes_conditions | ||
.each_with_object([]) do |((switch, condition), keys), excluded_keys| | ||
case switch | ||
when :if | ||
excluded_keys << keys unless eval_attribute_condition(condition[:if]) | ||
when :unless | ||
excluded_keys << keys if eval_attribute_condition(condition[:unless]) | ||
else | ||
excluded_keys | ||
end | ||
end.flatten | ||
end | ||
|
||
protected | ||
|
||
attr_accessor :instance_options | ||
|
||
private | ||
|
||
def eval_attribute_condition(condition) | ||
case | ||
when condition.is_a?(Symbol) | ||
send(condition) | ||
when condition.respond_to?(:call) | ||
instance_eval(&condition) | ||
else | ||
warn 'The condition is invalid' | ||
end | ||
end | ||
end | ||
end |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't think we should support any options for
attributes
. That's always just a list. We useattribute
for an attribute with optionsThere 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.
You want to use
instead of
attributes :id, :title, if: :admin?
?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.
@bf4 I'm not sure I understand why we would not want to support options for
attributes
. Could you elaborate?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.
value to end-user is minimal, cost to library is relatively high
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.
I would usually agree, but since we already support variable number of arguments and array of symbols as parameters to
attributes
, I don't believe the added cost is that high.