Build Status

TinyDecorator

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.

Installation

Add this line to your application's Gemfile:

gem 'tiny_decorator'

And then execute:

$ bundle

Or install it yourself as:

$ gem install tiny_decorator

Usage

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)

Preload

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.

Development

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.

Contributing

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.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the TinyDecorator project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.