{"id":16925448,"url":"https://github.com/alejandrodevs/active_model_exporters","last_synced_at":"2025-09-24T13:31:59.315Z","repository":{"id":12710579,"uuid":"15383095","full_name":"alejandrodevs/active_model_exporters","owner":"alejandrodevs","description":"A simple way to export data in Rails.","archived":false,"fork":false,"pushed_at":"2022-08-15T22:22:31.000Z","size":90,"stargazers_count":7,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-23T10:34:03.763Z","etag":null,"topics":["activemodel","exporter","rails","ruby"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alejandrodevs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-12-22T22:25:24.000Z","updated_at":"2022-08-15T19:19:55.000Z","dependencies_parsed_at":"2022-09-05T17:31:05.999Z","dependency_job_id":null,"html_url":"https://github.com/alejandrodevs/active_model_exporters","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alejandrodevs%2Factive_model_exporters","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alejandrodevs%2Factive_model_exporters/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alejandrodevs%2Factive_model_exporters/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alejandrodevs%2Factive_model_exporters/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alejandrodevs","download_url":"https://codeload.github.com/alejandrodevs/active_model_exporters/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234088104,"owners_count":18777881,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activemodel","exporter","rails","ruby"],"created_at":"2024-10-13T20:10:39.837Z","updated_at":"2025-09-24T13:31:54.028Z","avatar_url":"https://github.com/alejandrodevs.png","language":"Ruby","readme":"# ActiveModel::Exporters\n[![Build Status](https://travis-ci.com/alejandrodevs/active_model_exporters.svg?branch=master)](https://travis-ci.org/alejandrodevs/active_model_exporters) [![Coverage Status](https://coveralls.io/repos/github/alejandrodevs/active_model_exporters/badge.svg?branch=master)](https://coveralls.io/github/alejandrodevs/active_model_exporters?branch=master)\n\n`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](https://github.com/rails-api/active_model_serializers).\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'active_model_exporters'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install active_model_exporters\n\n## Usage\n\nGenerate an exporter in `app/exporters/post_exporter.rb`:\n\n```ruby\nclass PostExporter \u003c ActiveModel::Exporter\n  attributes :id, :title, :content\nend\n```\n\nIn your controller:\n\n```ruby\nclass PostsController \u003c ApplicationController\n  def index\n    @posts = Post.all\n\n    respond_to do |format|\n      format.csv { render csv: @posts }\n      format.xls { render xls: @posts }\n    end\n  end\nend\n```\n\n### Custom exporter\n\nTo specify a custom exporter for each object, you can do the next in your controller:\n\n```ruby\nrender csv: @posts, exporter: OtherPostExporter\n```\n\n### Custom filename\n\nBy default filename is the pluralized collection type. Example: `posts.xls`.\nTo specify another, you can do the next:\n\n```ruby\nrender xls: @posts, filename: 'super_posts.xls'\n```\n\n### Custom encode format\n\nBy default encode format is `iso-8859-1`. You can change it doing the next:\n\n```ruby\nrender csv: @posts, encode: 'UTF-8'\n```\n\n### Computed properties\n\nAs `ActiveModel::Serializers` does, you can access the object being exported as `object`.\n\n```ruby\nclass UserExporter \u003c ActiveModel::Exporter\n  attributes :first_name, :last_name, :full_name\n\n  def full_name\n    \"#{object.first_name} #{object.last_name}\"\n  end\nend\n```\n\n### Exporter scope\n\n#### 1. Default scope\n\nAs `ActiveModel::Serializers` does, you can access to the current user via `scope`.\n\n```ruby\nclass UserExporter \u003c ActiveModel::Exporter\n  attributes :name, :email\n\n  def email\n    object.email unless scope.admin?\n  end\nend\n```\n\n#### 2. Explicit scope\n\nIn your controller, include the scope option:\n\n```ruby\nrender csv: @posts, scope: current_admin\n```\n\n#### 3. Calling exportation_scope\n\nIn your controller, set the exportation scope:\n\n```ruby\nclass PostsController \u003c ApplicationController\n  exportation_scope :current_admin\n\n  def index\n    # Do something...\n  end\nend\n```\n\n### Filter attributes\n\nAs `ActiveModel::Serializers` does, you can reject some attributes according to your business rules:\n\n```ruby\nclass UserExporter \u003c ActiveModel::Exporter\n  attributes :name, :email, :address\n\n  def filter(attrs)\n    if object.admin?\n      attrs - [:address]\n    else\n      attrs\n    end\n  end\nend\n```\n\nRejected attributes will be blank in the downloaded file.\n\n### Headers\n\n`ActiveModel::Exporters` uses I18n translations in file headers.\n\n## Development\n\nAfter 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.\n\nTo 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](https://rubygems.org).\n\n## Contributing\n\nBug 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](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the ActiveModel::Exporters project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/alejandrodevs/active_model_exporters/blob/master/CODE_OF_CONDUCT.md).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falejandrodevs%2Factive_model_exporters","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falejandrodevs%2Factive_model_exporters","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falejandrodevs%2Factive_model_exporters/lists"}