-
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
:if option for attribute method #922
Comments
@Le6ow5k1 It isn't the first time I hear this proposition. |
@joaomdmoura didn't understand how it can be done using method to overwrite attribute. The only way to solve this problem that I found is to use |
@Le6ow5k1 this would be an example. class ActivitySerializer < ActiveModel::Serializer
attribute :complete_till
def complete_till
if object.type == 'Task'
# Logic here
end
end
end |
@joaomdmoura but this gives me My current approach: class ActivitySerializer < ActiveModel::Serializer
attribute :complete_till
def type
# logic to determine type
end
def filter(keys)
case type
when 'Task'
keys
else
keys - [:complete_till]
end
end
end |
Yea, while I like the :if syntax for readability, I think it could be argued that this approach violates some of the principles of SOLID. I'm not sure a serializer should ever conditionally remove an attribute, instead, that attribute should be NULL. If you don't want the attribute there at all, then perhaps a different serializer is what you want, as any attributes that exist in the serializer are meant to be behaviors that that class respond to. While the same could be attributed to an association, in that context I think it's OK, particularly if you're working with a collection and need to avoid N+1 in the event that object.association is not loaded. |
@Le6ow5k1 Does your |
@ankit8898 Yes, but I'm using 0.9.3 version. |
So I guess for 0.10.x version there's no solution for such problem. Would like to hear from repository owners how they feel about my proposal. |
@Le6ow5k1 indeed, as @saneshark wrote, in the actual architecture using 2 different serializer might be what fits you better. |
|
I'd be 👍 but I really do think it's not good practice. It would help people keep things DRY by using the same serializer, but I think that's what inheritance and concerns and such are better suited to do with unique serializer classes. @kurko I think to your point, it comes down to do we want ActiveModelSerializer to have the flexibility that jbuilder has or do we want to help reinforce some best practices around writing APIs* if it is the former then I am 👍 but if it is the latter (which I prefer), then I'm 👎* By making it more difficult to remove it using filter vs :if it might help reinforce the idea that you shouldn't be doing this type of thing. |
+1 for filter class BaseSerializer < ActiveModel::Serializer
def attributes
return super.extract!(*scope[:only]) if scope.try(:[], :only)
return super.except(*scope[:except]) if scope.try(:[], :except)
super
end
end That's might be not the best idea to override scope. But the question is how to filter attributes in 0.10.x? |
@iRet and everyone involved. |
@joaomdmoura actually this is not exactly what I'm looking for. It may be useful especially in combination with ability to define |
@joaomdmoura thanks! not exactly but better than nothing. |
@joaomdmoura Is this currently the only way to filter in 0.10.x? Seems odd when I have 30-40 attributes in a few of my models and I need to only remove 1 or 2 based on scope; end up with a 3 line string of fields in my controller as well as multiple render statements depending on which to remove. Is AMS open to having an |
@al3x-edge +1 for except! @joaomdmoura can we do something with this before release? |
I think it would be nice to add an
:if
option for attribute method, so attribute will appear only if result of calling lambda specified at:if
key istrue
.The text was updated successfully, but these errors were encountered: