Socket
Book a DemoInstallSign in
Socket

curtains

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

curtains

1.0.1
bundlerRubygems
Version published
Maintainers
1
Created
Source

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.

FAQs

Package last updated on 27 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.