ActiveRecord Type Traits
This library provides a series of classes to obtain type information of ActiveRecord models, attributes and associations.
This library is focused only on standard ActiveRecord features and will not provide information for ActiveRecord plugins such as Globalize, CarrierWave, Paranoid and others.
API design is done in good and clear manner for best understanding.
All base ActiveRecord features are supported.
Supported advanced ActiveRecord features:
-
has_many
,has_one
,has_and_belongs_to_many
,belongs_to
associations - Intermediate and through (deep) associations
- Polymorphic associations
- Single Table Inheritance
- SQL Join metadata
Example of usage
class Author < ActiveRecord::Base
has_many :articles
has_one :photo, as: :imageable
end
class Article < ActiveRecord::Base
belongs_to :author
end
class Photo
belongs_to :imageable, polymorphic: true
end
Author.traits.associations[:articles].has_many? # => true
Author.traits.associations[:articles].to.active_record # => Article
Author.traits.associations[:articles].paired_through_polymorphism? # => true
Photo.traits.associations[:imageable].polymorphic? # => true
Photo.traits.associations[:imageable].accepted_classes_through_polymorphism # => [Author]
Photo.traits.attributes[:imageable_type].polymorphic_type? # => true
Article.traits.attributes[:author_id].foreign_key? # => true
class Present < ActiveRecord::Base
end
class Toy < Present
end
class VideoGame < Present
end
Present.traits.inheritance_base? # => true
Toy.traits.inheritance_derived? # => true
Toy.traits.inheritance_chain # => [Present, Toy]
Gemfile
gem 'activerecord-traits', github: 'yivo/activerecord-traits'