0.0
No commit activity in last 3 years
No release in over 3 years
Dynamic attributes for ActiveRecord!
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

= 2.13.0

Runtime

~> 3.2.12
~> 3.2.12
 Project Readme

Dynamic Attrs


Make dynamic ActiveRecord attribute columns.

In your rails gem file, add:

gem 'dynamic_attrs'

Generate related migration:

$ rails g dynamic_attrs
$ rake db:migrate

Then invoke something like this in your model definition:

class User
  include DynamicAttr::Owner

  # Supporting 4 data types
  # :string, :datetime, :boolean, :integer
 
  has_dynamic_attrs :student_attrs,
                    :fields => {
                      :sid          => :string,
                      :birthday     => :datetime,
                      :is_graduated => :boolean,
                      :age          => :integer
                    }
 
  has_dynamic_attrs :teacher_attrs,
                    :fields => {
                      :tid      => :string,
                      :birthday => :datetime,
                      :age      => :integer
                    }
end

Get and set values:

@user.teacher_attrs_tid = '0001'
@user.teacher_attrs_tid
# => '0001'
 
@user.student_attrs_is_graduated? = true
@user.student_attrs_is_graduated = true
@user.student_attrs_is_graduated?
@user.student_attrs_is_graduated
# => true

Query:

User.where_student_attrs(:is_graduated, true)
# => ActiveRecord::Relation [...]
 
User.where_teacher_attrs(:age, 35)
# => ActiveRecord::Relation [...]