ActiveModel::Exporters
aims to provide an easy way to export collections of ActiveModel
or ActiveRecord
objects. It's based on object-oriented development and inspired on active_model_serializers.
Add this line to your application's Gemfile:
gem 'active_model_exporters'
And then execute:
$ bundle
Or install it yourself as:
$ gem install active_model_exporters
Generate an exporter in app/exporters/post_exporter.rb
:
class PostExporter < ActiveModel::Exporter
attributes :id, :title, :content
end
In your controller:
class PostsController < ApplicationController
def index
@posts = Post.all
respond_to do |format|
format.csv { render csv: @posts }
format.xls { render xls: @posts }
end
end
end
To specify a custom exporter for each object, you can do the next in your controller:
render csv: @posts, exporter: OtherPostExporter
By default filename is the pluralized collection type. Example: posts.xls
.
To specify another, you can do the next:
render xls: @posts, filename: 'super_posts.xls'
By default encode format is iso-8859-1
. You can change it doing the next:
render csv: @posts, encode: 'UTF-8'
As ActiveModel::Serializers
does, you can access the object being exported as object
.
class UserExporter < ActiveModel::Exporter
attributes :first_name, :last_name, :full_name
def full_name
"#{object.first_name} #{object.last_name}"
end
end
As ActiveModel::Serializers
does, you can access to the current user via scope
.
class UserExporter < ActiveModel::Exporter
attributes :name, :email
def email
object.email unless scope.admin?
end
end
In your controller, include the scope option:
render csv: @posts, scope: current_admin
In your controller, set the exportation scope:
class PostsController < ApplicationController
exportation_scope :current_admin
def index
# Do something...
end
end
As ActiveModel::Serializers
does, you can reject some attributes according to your business rules:
class UserExporter < ActiveModel::Exporter
attributes :name, :email, :address
def filter(attrs)
if object.admin?
attrs - [:address]
else
attrs
end
end
end
Rejected attributes will be blank in the downloaded file.
ActiveModel::Exporters
uses I18n translations in file headers.
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/alejandrodevs/active_model_exporters. 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 ActiveModel::Exporters project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.