Compositor::Rails
Extensions to compositor that allow views to be created and used inside of Rails.
Installation
Add this line to your application's Gemfile:
gem 'compositor-rails'
And then execute:
$ bundle
Or install it yourself as:
$ gem install compositor-rails
Usage
Given a UserCompositor
as follows:
class UserCompositor < Compositor::Leaf
attr_accessor :user
def initialize(context, user, attrs = {})
super(context, attrs)
self.user = user
end
def to_hash
{
id: user.id,
username: user.username,
location: user.location,
bio: user.bio,
url: user.url,
image_url: context.image_path(user.avatar),
...
}
end
end
To render a list of users, create a view and then write out the DSL as you normally would.
list :collection: @users, root: :users do |u|
user: u
end
Partials
Let's move the rendering of a user into a partial.
list :collection: @users, root: :users do |u|
partial! 'show', user: user
end
user: user
Notes on using partials:
- Try and keep them to a minimum. The whole point of compositor is to reduce complexity
in the composition stage. A enourmous number of partials might just mean a need for
another compositor.
- Normal Rails partials support collections. These do not as compositor requires you to
explicitly define your collection rendering. Of course a partial can render a collection
itself, by using the normal compositor list/map functionality.
Contributing
- Fork it ( http://github.com//compositor-rails/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request