No commit activity in last 3 years
No release in over 3 years
Type information of activerecord models, attributes and associations in good and clear manner
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

< 6.0, >= 3.0
< 6.0, >= 3.0
 Project Readme

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:

  1. has_many, has_one, has_and_belongs_to_many, belongs_to associations
  2. Intermediate and through (deep) associations
  3. Polymorphic associations
  4. Single Table Inheritance
  5. 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'