Skip to content
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 preservation of decorator options on QueryMethods #868

Conversation

DouglasLutz
Copy link
Contributor

References

Description

The context given to a Collection Decorator was being lost when an ActiveRecord::QueryMethod was being called on a Draper::CollectionDecorator.

E.g.

decorated_collection = PostDecorator.decorate_collection(Post.all, context: {user: "foo"}).order("created_at asc")
decorated_collection.context #{}
decorated_collection.decorator_class #nil

This was happening because method_missing on draper/lib/draper/query_methods.rb is invoking ActiveRecord method with send and creating another instance of decorator to decorate the result.

 def method_missing(method, *args, &block) 
   return super unless strategy.allowed? method 
  
   object.send(method, *args, &block).decorate 
 end 

The problem is that the object context and decorator_class were being lost on creation of another instance.
The solution to this was to pass the options alongside the decorate call, it creates the instance of CollectionDecorator with the old options.

Testing

The new behaviour of the Collection Decorator is the following.

decorated_collection = PostDecorator.decorate_collection(Post.all, context: {user: "foo"}).order("created_at asc")
decorated_collection.context #{:user=>"foo"}
decorated_collection.decorator_class #PostDecorator

Also added a test to cover this alteration.

@DouglasLutz DouglasLutz changed the title Add preservation of decorator options on query methods Add preservation of decorator options on QueryMethods Dec 16, 2019
@brunohkbx
Copy link
Contributor

Hey, @DouglasLutz @synth. Good catch! 👍
Sorry, I didn't realize that on #845

@codebycliff could you please take a look?

Copy link
Collaborator

@codebycliff codebycliff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. Now that Draper::QueryMethods depends upon decorator_class and context, I'm not sure it makes sense for this code to be extracted to its own module. The module won't work unless it's included in CollectionDecorator, which I think makes a strong case for just moving the code there. That being said, it's not a big deal the way it is. Just a thought. What do you guys think?

If we don't care to move it in, we can merge this once the formatting suggestion is applied.

@DouglasLutz
Copy link
Contributor Author

@codebycliff I agree with you with the dependency of decorator_class and context, Draper::QueryMethods is modeled to CollectionDecorator.

I will apply the formatting suggestion and depending on your thoughts I can move the code there.

@DouglasLutz DouglasLutz force-pushed the options-preservation-on-query-methods branch from 3c61f5f to 22ac162 Compare December 19, 2019 16:57
Copy link
Collaborator

@codebycliff codebycliff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think it makes sense to move it into CollectionDecorator, but I think that change is bigger than I thought it would be. We'll go with this for now. Thanks for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants