Allow using symbols as a condition#102
Conversation
|
|
||
| class Actor < User | ||
| attr_accessor :movies, :movie_ids | ||
| attr_accessor :movies, :movie_ids, :age, :birthplace, :show_birthplace |
There was a problem hiding this comment.
I don't think we need :show_birthplace in the list of attributes, let's just keep it as a method... What do you think?
|
|
||
| def show_birthplace(object) | ||
| object.show_birthplace | ||
| end |
There was a problem hiding this comment.
I don't think we need both conditionals here. I think having just one (for ex. age) is more than enough since the interface of the conditional method will always provide the object and the params (based on FastJsonapi#call_proc) as the arguments. So it's up to the end-user to decide what to do with it.
Let me know if I'm not missing anything here...
|
I would love to see this merged. This would clean up our code quite a bit. |
|
@ngoral @ekampp @epfeen I was reviewing this again and I'm not sure this approach is OK. Particularly I have concerns about this exact change: This suggests we'll be instantiating a new serializer for every attribute with a conditional. I suggest an alternative design where conditional methods are OK, but these need to be class methods and can receive the Here's an example to consider: class MovieSerializer
include JSONAPI::Serializer
attribute :full_access, if: :is_admin?
def self.is_admin?(object, params)
params[:is_admin] && object.admin?
end
endHaving class methods should have minimal performance impact. Let me know what you think?! |
|
I agree that instantiation is not a good solution. Why does the method need to be a class level method? It would be more prudent ruby if the method could be a private instance method, since other objects shouldn't care about this internal logic. |
Class methods do not require instantiation, though can still stay private. Generally, I see no issues having this done using private class methods, but feel free to share your concerns... |
Yes, but isn't the serializer already instantiated at the time of evaluating the |
@ekampp please take a look on how it works at the moment. You'll be surprised to see that it looks more like functional vs object-oriented 😄 |
|
In that case class level is probably the right solution. |
|
i agree with @ekampp |
|
Sorry for abandoning this PR: at some point, I switched to another gem, so this one left unattended. I'll be more than happy to fix your comments and review the situation with instantiation once I have some time for it (work-life balance, all these things, you know=) ). Hopefully, I will in a few weeks. |
By now, if I want to add a conditional attribute or link, I should provide a proc as a value for
ifkey. It may look awkward, especially if there is a block passed along with the condition.I suggest allowing to pass a symbol as a value for
ifkey, which is the name of a method defined below in the serializer. It seems to be common in ruby/rails, e.g. in conditional callbacks in Rails.What is the current behavior?
What is the new behavior?
We can provide a symbolized serializer method name as a value for
ifkeyChecklist
Please make sure the following requirements are complete:
features)