-
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
String/Lambda support for conditional attributes/associations #1699
Conversation
Yeah, I had support for lambda in mind when adding this feature, so 👍 on the idea of this PR. |
def condition_lambda | ||
case condition | ||
when Symbol | ||
-> (serializer) { serializer.public_send(condition) } |
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'm not sure building a throwaway lambda here is the best way. How about the following
def condition_lambda(serializer)
case condition
when Symbol
serializer.public_send(condition)
when String
serializer.instance_eval(condition)
when Proc
condition.call(serializer)
...
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.
Yeah, it can be a method 👍
Fixed and force pushed 😄
97ac16b
to
bf5cd79
Compare
LGTM – I'll merge unless somebody raises concerns. |
bf5cd79
to
c1fd222
Compare
when Proc | ||
condition.call(serializer) | ||
else | ||
fail ArgumentError |
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.
would be good to include a message here
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.
👍
By the way, it is better if raise an error on evaluate serializer's DSL I think.
9e43a67
to
72b7191
Compare
Hmm, tests for jruby-9.0.4.0 on appveyor are failed but passed on travis 😕 |
@mtsmfm Looking into the appveyor thing. |
72b7191
to
a50cd44
Compare
a555028
to
b33791b
Compare
@mtsmfm Awesome! Would you mind submitting the AppVeyor settings fix as a separate PR? |
@@ -3,21 +3,19 @@ version: '{build}' | |||
skip_tags: true |
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.
Could you rebase?
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.
rebased!
b33791b
to
aa087a2
Compare
This is great for one field, but what if I've a lot a field I wanted to hide ? Do I have to add a predicate after each attribute / association ? |
Purpose
I want to exclude attribute if its association/attribute is nil.
:if
option accepts only symbol at this time so we must write condition like following:But it is not bad to support String like rails:
I don't have any use case for lambda(proc) at this time but many developers who experienced rails will guess that it accepts lambda.
Related GitHub issues
#1403