EasyExports
EasyExports is a rails ActiveRecord ORM extension dedicated to streamlining and simplifying the model data export process by eliminating common complexities.
Installation
Add this line to your application's Gemfile:
gem "easy_exports"
And then execute:
$ bundle
Or install it yourself as:
$ gem install easy_exports
Usage
Upon installation, EasyExports seamlessly integrates with ActiveRecord::Base
, granting all models immediate access to its efficient export methods.
Generating Exportable Attributes
Retrieve exportable attributes using the exportable_attributes
method. This method retrieves attributes of the model itself and those of all its associations.
class User < ApplicationRecord
has_and_belongs_to_many :emails
has_many :phones
end
User.exportable_attributes
class Phone < ApplicationRecord
belongs_to :user
end
Phone.exportable_attributes
Generating Exports from Exportable Attributes
To generate exports, use the generate_exports(exportable_attributes, ids)
method.
- The
exportable_attributes
argument specifies the chosen attributes from the exportable attributes list. - The
ids
argument is optional; provide IDs to export data for specific records. - Omitting
ids
will trigger exports for all records of the given model.
The method returns an EasyExports::Export
object containing hash data from the records and a csv_string
that can be written to a CSV file.
user_exportable_attributes = {"User"=>["id", "first name"], "Phones"=>["id", "number"], "Emails"=>["id", "address"]}
exports_object = User.generate_exports(user_exportable_attributes)
exports_data = exports_object.data
exports_csv_string = exports_object.csv_string
File.open(file_path, 'w') do |file|
file.write(exports_csv_string)
end
Exported CSV showcases data for:
- User "Sydney" with 3 phones and 2 emails
- User "Stan" with 2 phones and 1 email.
- The main CSV header includes association names and attribute names.
Exportable Attributes Aliases
Configure an alternative association name for exportable attributes using the exportable_association_aliases(aliases)
model method.
- Invoke this method below all association definitions.
aliases
should be a hash in the pattern: {valid_association_name or model_name: "alternative_name"}
.- Ensure all hash arguments are snake-cased.
class User < ApplicationRecord
has_many :phones
exportable_association_aliases phones: :mobile_phones
end
User.exportable_attributes
With the exportable_association_aliases configured, the phones association has been renamed to "Mobile phones". This new name will appear in the export header when generating exports with this alias for exportable attributes.
Excluding Specific Exportable Attributes
Configure associations to exclude certain attributes from exportable attributes using the exclude_exportable_attributes(association_attributes)
model method.
- Invoke this method below all association declarations.
association_attributes
should follow the pattern {valid_association_name or model_name: [valid_attributes_to_remove]}
.- For removing attributes across all associations and the model itself, use the "all" key as the association_name.
class User < ApplicationRecord
has_many :phones
exclude_exportable_attributes all: [:id], user: [:last_name], phones: [:user_id]
end
User.exportable_attributes
In this example, note that:
- All associations exclude the id attribute.
- The User model excludes the last_name attribute.
- The Phones association excludes the user_id attribute.
Excluding Specific Exportable Attribute Associations
Configure model's exportable attributes to exclude certain associations using the associations_to_exclude(associations)
model method.
- Apply this method below all association declarations.
associations
should follow the pattern ['association_name']
.
class User < ApplicationRecord
has_many :phones
associations_to_exclude [:phones]
end
User.exportable_attributes
In this example, the attributes of the phones association are excluded from the exportable attributes of the User model.
Adding Custom Attribute to Exportable Attributes
Leverage a handy Rails method to transform a model instance method into an attribute, incorporating it into the exportable attributes.
class User < ApplicationRecord
attribute :total_number_of_phones
has_many :phones
associations_to_exclude [:phones]
def total_number_of_phones
phones.size
end
end
User.exportable_attributes
In this example, the custom attribute "total number of phones" has been seamlessly integrated into the exportable attributes, showcasing the flexibility of Rails' capabilities.
License
The gem is available as open source under the terms of the MIT License.