ActiveDelegate
Stores and retrieves delegatable data through attributes on a ActiveRecord class, with support for translatable attributes.
Installation
Add this line to your application's Gemfile:
gem 'active_delegate'
And then execute:
$ bundle
Or install it yourself as:
$ gem install active_delegate
Usage
class Address < ActiveRecord::Base
end
class Person < ActiveRecord::Base
include ActiveDelegate
belongs_to :address
has_one :user
has_many :books
delegate_attributes to: :address, prefix: true, localized: true
end
params = {
name: 'John',
email: 'john@mail.com',
address_city: 'Athens',
address_city_el: 'Αθήνα',
address_district: 'Attiki',
address_district_el: 'Αττική'
}
person = Person.new(params)
person.name
person.address_city
person.address.city
person.address_city_el
person.address.city_el
person.address_district
person.address.district
person.address_district_el
person.address.district_el
class User < ActiveRecord::Base
include ActiveDelegate
belongs_to :person, autosave: true
delegate_associations to: :person
delegate_attributes to: :person
end
params = {
login: 'jonian',
password: 'passwd',
name: 'John',
email: 'john@mail.com'
}
user = User.new(params)
user.name
user.login
user.user
user.books
user.email
user.email?
user.email_changed?
user.email_was
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
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
.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/hardpixel/active-delegate.
License
The gem is available as open source under the terms of the MIT License.