andypike / rectify

Build maintainable Rails apps
MIT License
596 stars 48 forks source link

Implement `to_ary` in Query #36

Closed mrcasals closed 8 years ago

mrcasals commented 8 years ago

I'm using rectify at https://github.com/codegram/decidim/pull/147 and I'm having problems trying to render the results of a query object in a view using both these methods:

render(participatory_processes)
<%= render partial: "promoted_process", collection: promoted_processes, as: :promoted_process %>

I'm getting this error:

#<Decidim::PublishedProcesses ...> is not an ActiveModel-compatible object. It must implement :to_partial_path

My first try was calling the query method on the query object in the view:

render(participatory_processes.query)

This didn't raise any error, but didn't render anything actually so it's not useful. Also, explicitly calling this method from the view feels weird.

Checking the Rails source code, Rails tries to check if the collection passed to the render method responds to to_ary. If it does not, it uses an empty array as the collection and thus it does not render anything (which is weird, actually):

https://github.com/rails/rails/blob/28a6571bae1d6bc1c774e8e501720374061a31f1/actionview/lib/action_view/renderer/partial_renderer.rb#L406

So my take is defining the to_ary method in the Query class as an alias from to_a. Both the unit tests and the tests I took on my app are green with this change.

andypike commented 8 years ago

@mrcasals Thanks so much for the addition!! It's all merged and I'll cut a release for you now so you can update the gem in your project.

ā¤ļø šŸ’› šŸ’™ šŸ’š šŸ’œ

andypike commented 8 years ago

@mrcasals 0.7.1 is now release with your change :smile:

mrcasals commented 8 years ago

Yay, thanks for the fast reply and for your work on rectify! Works awesomely! šŸ˜„