AliasAssociation
AliasAssociation is a Ruby on Rails concern that provides a simple way to create aliases for ActiveRecord associations, including both getter and setter methods.
Installation
Add this line to your application's Gemfile:
gem 'alias_association'And then execute:
bundle installUsage
Include the AliasAssociation concern in your ActiveRecord model and use the alias_association class method to create aliases for existing associations.
class Order < ApplicationRecord
include AliasAssociation
belongs_to :user
belongs_to :income_payment_system, class_name: 'PaymentSystem'
belongs_to :outcome_payment_system, class_name: 'PaymentSystem'
# Create aliases for associations
alias_association :owner, :user
alias_association :payment_system_from, :income_payment_system
alias_association :payment_system_to, :outcome_payment_system
endNow you can use the aliases just like the original associations:
order = Order.first
# Getter methods
order.owner # equivalent to order.user
order.payment_system_from # equivalent to order.income_payment_system
order.payment_system_to # equivalent to order.outcome_payment_system
# Setter methods
order.owner = User.find(2) # equivalent to order.user = User.find(2)
order.payment_system_from = ps # equivalent to order.income_payment_system = ps
order.payment_system_to = ps # equivalent to order.outcome_payment_system = psError Handling
The concern validates that the original association exists before creating the alias:
class Order < ApplicationRecord
include AliasRelationship
# This will raise an ArgumentError if :nonexistent_association doesn't exist
alias_association :alias_name, :nonexistent_association
# => ArgumentError: Association :nonexistent_association not foundDebug Information
You can retrieve information about association aliases for debugging purposes:
Order.association_aliases
# => { owner: :user, payment_system_from: :income_payment_system, payment_system_to: :outcome_payment_system }API Reference
alias_association(new_name, original_name)
Creates an alias for an existing ActiveRecord association.
-
new_name(Symbol) - The name for the new alias -
original_name(Symbol) - The name of the existing association to alias
Raises: ArgumentError if the original association doesn't exist
association_aliases
Returns a hash containing information about all association aliases for the model.
Returns: Hash with alias names as keys and original association names as values
Development
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.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/dapi/alias_association.
License
The gem is available as open source under the terms of the MIT License.