Pluckify allows you to pluck to Hash
, OpenStruct
or any custom class like presenters.
Inspired by pluck_to_hash
gem (github).
# Hash (only column name args)
User.limit(10).pluckify(:id, :first_name, 'users.last_name as last_name')
# => [ { id: 1, first_name: 'Romain', last_name: 'de Landesen' } ]
# OpenStruct
Administrator.where(id: 1).pluckify(:id, :first_name, :last_name, OpenStruct)
# => [ <#OpenStruct id=1, first_name="Romain", last_name="de Landesen"> ]
# Custom Presenter
Administrator.pluckify(:id, :first_name, :last_name, AdministratorPresenter)
# => [ #<AdministratorPresenter @model=#<OpenStruct id=1, first_name="Judge", last_name="Dredd">> ]
Add this line to your application's Gemfile:
gem 'pluckify', github: 'rdelandesen/pluckify'
And then execute:
$ bundle install
The gem is available as open source under the terms of the MIT License.