https://github.com/dbackowski/trivial_decorator
A simple decorator for Rails
https://github.com/dbackowski/trivial_decorator
decorator decorator-pattern decorators gem rails ruby
Last synced: about 1 month ago
JSON representation
A simple decorator for Rails
- Host: GitHub
- URL: https://github.com/dbackowski/trivial_decorator
- Owner: dbackowski
- License: mit
- Created: 2017-11-30T19:56:03.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-04T10:10:46.000Z (over 7 years ago)
- Last Synced: 2026-02-20T01:58:47.262Z (3 months ago)
- Topics: decorator, decorator-pattern, decorators, gem, rails, ruby
- Language: Ruby
- Size: 29.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# TrivialDecorator [](https://travis-ci.org/dbackowski/trivial_decorator)
A simple decorator for Rails.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'trivial_decorator'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install trivial_decorator
## Usage
#### Writing Decorators
Decorators inherit from TrivialDecorator::Base, live in your app/decorators directory, and are named for the model that they decorate:
```ruby
# app/decorators/user_decorator.rb
class UserDecorator < TrivialDecorator::Base
def full_name
first_name + " " + last_name
end
end
```
#### Accessing helpers
```ruby
# app/decorators/user_decorator.rb
class UserDecorator < TrivialDecorator::Base
def full_name
h.content_tag(:strong, first_name + " " + last_name)
end
end
```
#### Decorating Objects
```ruby
# app/controllers/users_controller.rb
class UsersController < ApplicationController
def index
@users = decorate(User.all)
end
end
```
This will use UserDecorator, if you want to use other decorator, just pass his class name as second param to decorate.
```ruby
# app/controllers/users_controller.rb
class UsersController < ApplicationController
def index
@users = decorate(User.all, OtherDecorator)
end
end
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/dbackowski/trivial_decorator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the TrivialDecorator project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/dbackowski/trivial_decorator/blob/master/CODE_OF_CONDUCT.md).