
Security News
rv Is a New Rust-Powered Ruby Version Manager Inspired by Python's uv
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
The world of ruby decorator is dominated by draper
, I use it all the time.
But when project grows and we chase the dealine, the flexible and easily of draper
make us keep abusing it. Now, when using a decorated model we dont know which method/attribute are define in which decorator, which decorators depend on which ones.
tiny_decorator
aims to separate decoration logic into different decorators, and centralize logic to determine which decorators are used. It isn't built to replace draper
as it just solve our internal problem of abusing draper
, it's better if you using draper
wisely instead tiny_decorator
But if you're interesting in this gem, every contributes are welcome.
Add this line to your application's Gemfile:
gem 'tiny_decorator'
And then execute:
$ bundle
Or install it yourself as:
$ gem install tiny_decorator
Model decorated by 1 single decorator
class NewDecorator < TinyDecorator::SingleDecorator
def new_method
'decorated'
end
end
NewDecorator.decorate(Object.new).new_method # 'decorated'
NewDecorator.decorate_collection([Object.new, Object.new])[1].new_method # 'decorated'
Model decorate by more than 1 decorators
class NewDecorator2
extend TinyDecorator::CompositeDecorator
decorated_by :default, 'DefaultDecorator'
decorated_by :nil, ->(record) { record.nil? ? 'NilDecorator' : 'DefaultDecorator' }
decorated_by :nil, ->(record, context) { record.in(context).nil? ? 'NilDecorator' : 'DefaultDecorator' }
end
And use it
context = { current_user: user }
#
NewDecorator.decorate(model)
NewDecorator.decorate(model, context)
# OR
NewDecorator.decorate_collection(models)
NewDecorator.decorate_collection(models, context)
In case rails' eager loading doesn't fit, or we don't use. preload
could be used to avoid N+1.
preload :count_all, ->(all_records, context, preloaded) { Group(all_records).count }
# all_records - all the records to decorate
# context - The context passed to collection decorating,
# because preload run once before all decratings, this is the only cotext we have at this time
# preloaded - all preloaded before. For perfomrance, it's mutable, please handle with care
Then each single decorator could access through preload[:count_all]
.
Note: preload
will be run once before decorating all records, compare to context will be ran for each record.
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/anvox/tiny_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.
Everyone interacting in the TinyDecorator project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
FAQs
Unknown package
We found that tiny_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
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.
Security News
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.