
Security News
New Website “Is It Really FOSS?” Tracks Transparency in Open Source Distribution Models
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
composable_decorator
Advanced tools
A simple, composable decorator for Rails models.
class User < ActiveRecord::Base
decorate_with NameDecorator, PhoneNumberDecorator
end
@user = User.find(1).decorate
@user.full_name # => "Jane Smith"
Add this line to your application's Gemfile:
gem 'composable_decorator'
And then execute:
$ bundle
Or install it yourself as:
$ gem install composable_decorator
Decorators are declared as modules.
module NameDecorator
def full_name
"#{first_Name} #{last_name}"
end
end
A #decorate_with
method is called in the model that lists the order of decorators to be added to the instance.
# /app/models/user.rb
class User < ActiveRecord::Base
decorate_with NameDecorator, PhoneNumberDecorator
end
This is roughly the equivalent of wrapping the instance in first the Name decorator, then the PhoneNumber decorator, i.e.:
class User < ActiveRecord::Base
extend NameDecorator
extend PhoneNumberDecorator
end
We can then decorate the model in the controller and access the decorated methods in the view.
# /app/controllers/users_controller.rb
class UsersController
def show
...
@user.decorate
end
end
# /app/views/users/show.html.slim
@user.full_name
@user.full_phone_number
Decorators are inherited
module HatDecorator
def hat_decorated_method
"hat decorator method"
end
end
class Hat < ActiveRecord::Base
decorate_with HatDecorator
end
class Stetson < Hat
end
Stetson.new.decorate.hat_decorator_method #=> "hat decorator method"
We can delegate the association's decorated methods to the instance all at once.
module AddressDecorator
def simple_format
"#{street}, #{city}"
end
end
class Address < ActiveRecord::Base
decorate_with AddressDecorator
end
class User
delegate_decorated_to :address
end
# /app/views/users/show.html.slim
User.find(1).decorate.address_simple_format
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/composable_decorator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that composable_decorator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.