Project

geckorate

0.0
No commit activity in last 3 years
No release in over 3 years
A dead simple decorator for Ruby
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.16
~> 5.0
~> 10.0
 Project Readme

Geckorate

A dead simple decorator for Ruby

Why

This gem groups the functionality I used to implement in every Rails project I had so I thought of packaging it into a gem and get over with it. Also, see Build your own decorator pattern.

Installation

Add this line to your application's Gemfile:

gem 'geckorate'

And then execute:

$ bundle

Or install it yourself as:

$ gem install geckorate

Usage

A Rails example

First, you need to add app/decorators to your app load path:

# config/application.rb

module MyApp
  class Application < Rails::Application
    ...
    config.autoload_paths << Rails.root.join('app/decorators')
  end
end

Now, define your decorator as such:

# app/decorators/post_decorator.rb

class PostDecorator < Geckorate::Decorator
  def decorate(options = {})
    {
      title: title,
      username: user.name
    }.as_json
  end
end

And initialize it in your controller and use it for your view:

# app/controllers/posts_contrller.rb

class PostsController < ApplicationController
  def show
    post           = Post.find(params[:id])
    decorated_post = PostDecorator.new(post).decorate

    render json: { post: decorated_post }
  end
end

For collections

  • There's a method decorate_collection(collection, options = {}) for decorating collections
  • Example: PostDecorator.decorate_collection(Post.all) returns an array of decorated Post objects
  • The options hash can hold parameters to control the decoration:
    • :class_name: the name of the decorator class without the Decorator part
    • :method: the method to use for decorating the collection

With paginated collections

Currently, there are two methods for pagination; one for Kaminari decorate_kaminari_collection(collection, options = {}) and another for will_paginate decorate_will_paginate_collection(collection, options = {}). The returned hash from both methods looks like this:

{
  page: 1,
  per_page: 25,
  total: 100,
  records: [{...}, {...}]
}

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test 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/aonemd/geckorate.

License

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