Simple model decorators for Rails 4.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
.gitignore
Gemfile
LICENSE.txt
README.md
Rakefile
curtains.gemspec

README.md

Curtains

Curtains provides simple model decorators for Rails 4.

Installation

Add this line to your application's Gemfile:

gem "curtains"

And then execute:

$ bundle

Or install it yourself as:

$ gem install curtains

Usage

In either controllers or views you may call decorate to wrap a model in a decorator. The name of the decorator class is inferred from the name of the model, so for example, the Address model will be wrapped with AddressDecorator.

# app/views/addresses/show.html.erb
<%= decorate(@address).formatted_address_tag %>

The decorate helper also supports enumerable collections and will return an array of decorated objects:

# app/views/addresses/index.html.erb
<%= decorate(@addresses).each do |address| %>
	<%= address.formatted_address_tag %>
<% end %>

Additionally, the decorate helper yields its result to a block if provided:

# app/views/addresses/show.html.erb
<%= decorate(@address) do |address| %>
	<div class="name">
		<%= address.formatted_name %>
	</div>
	<%= address.formatted_address_tag %>
<% end %>

These decorators should be placed in the app/decorators folder and should inherit from Curtains::Decorator. For example:

# app/decorators/address_decorator.rb
class AddressDecorator < Curtains::Decorator
	def formatted_address_tag
		content_tag :address, address.lines.join("<br/>").html_safe
	end

	def formatted_name
		return "N/A" if name.blank?
		name
	end
end

Inherited Resources Compatibility

Call decorate_resources in your controller to add automatic decorating to the resource, build_resource and collection methods provided by Inherited Resources:

# app/controllers/address_controller.rb
class AddressesController < InheritedResources::Base
  decorate_resources
end
# app/views/addresses/show.html.erb
<%= resource.formatted_address_tag %>

Meta

Handcrafted by Patrick Hogan [github]

Released under the MIT License.