Skip to content

Svashta/active_dynamic

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ActiveDynamic

Gem Version Code Climate Build Status

ActiveDynamic allows to dynamically add properties to your ActiveRecord models and work with them as regular properties. To see this in practice, check out the demo application available at https://github.com/koss-lebedev/active_dynamic_demo. I also wrote an article explaining how to use active_dynamic.

Installation

Add this line to your application's Gemfile:

gem 'active_dynamic'

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_dynamic

Usage

To make this gem work, first you need to add has_dynamic_attributes to the model that needs to have dynamic attributes. For example, if you have Profile model:

class Profile < ActiveRecord::Base
  has_dynamic_attributes
  
  # ...
end  

After that you need to set a class that will resolve definitions of the dynamic attributes to be created on Profile model:

# lib/initializers/dynamic_attribute.rb

ActiveDynamic.configure do |config|
  config.provider_class = ProfileAttributeProvider
end

class ProfileAttributeProvider

  # Constructor will receive a class to which dynamic attributes are added
  def initialize(model_class)
    @model_class = model_class    
  end
  
  # This method has to return array of dynamic field definitions.
  # You can get it from the configuration file, DB, etc., depending on your app logic
  def call
    [
      # attribute definition has to specify attribute display name
      ActiveDynamic::AttributeDefinition.new('biography'),
      
      # Optionally you can provide datatype, system name, and default value.
      # If system name is not specified, it will be generated automatically from display name
      ActiveDynamic::AttributeDefinition.new('age', datatype: ActiveDynamic::DataType::Integer, default_value: 18)
    ]
  end
  
end

To resolve dynamic attribute definitions for more than one model:

class Profile < ActiveRecord::Base
  has_dynamic_attributes
  
  # ...
end  
 
class Document < ActiveRecord::Base
  has_dynamic_attributes
  
  # ...
end  
 
class ProfileAttributeProvider
 
  def initialize(model_class)
    @model_class = model_class    
  end
  
  def call
    if @model_class == Profile
      [
        # attribute definitions for Profile model
      ] 
    elsif @model_class == Document
      [
        # attribute definitions for Document model
      ] 
    else
      []
    end
  end
  
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/koss-lebedev/active_dynamic. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

About

Gem that allows to add attributes to your active models dynamically

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 99.0%
  • Shell 1.0%