All Projects → koss-lebedev → active_dynamic

koss-lebedev / active_dynamic

Licence: MIT license
Gem that allows to add attributes to your active models dynamically

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to active dynamic

ar-dynattribute
Provide ActiveRecord dynamic attributes stored into the single field in serialized state
Stars: ✭ 43 (-29.51%)
Mutual labels:  activerecord, dynamic-attributes
attribute-depends-calculator
Automatically calculate a collection of depends attribute of ActiveRecord
Stars: ✭ 41 (-32.79%)
Mutual labels:  activerecord
Secondbase
Seamless second database integration for Rails.
Stars: ✭ 216 (+254.1%)
Mutual labels:  activerecord
query-objects-example
Example of rails app using Query Objects
Stars: ✭ 37 (-39.34%)
Mutual labels:  activerecord
Seamless database pool
Add support for master/slave database clusters in ActiveRecord to improve performance.
Stars: ✭ 222 (+263.93%)
Mutual labels:  activerecord
sql-builder
A simple SQL builder for generate SQL for non-ActiveRecord supports databases
Stars: ✭ 34 (-44.26%)
Mutual labels:  activerecord
Randumb
Adds ability to pull back random records from Active Record
Stars: ✭ 208 (+240.98%)
Mutual labels:  activerecord
blade-jdbc
🐜 move to https://github.com/biezhi/anima
Stars: ✭ 36 (-40.98%)
Mutual labels:  activerecord
activerecord-debug errors
An extension of activerecord to display useful debug logs on errors
Stars: ✭ 23 (-62.3%)
Mutual labels:  activerecord
n1 loader
Loader to solve N+1 issues for good. Highly recommended for GraphQL API.
Stars: ✭ 182 (+198.36%)
Mutual labels:  activerecord
Scenic
Scenic is maintained by Derek Prior, Caleb Hearth, and you, our contributors.
Stars: ✭ 2,856 (+4581.97%)
Mutual labels:  activerecord
Octopus
Database Sharding for ActiveRecord
Stars: ✭ 2,496 (+3991.8%)
Mutual labels:  activerecord
embedded
Rails/Activerecord plugin to make value objects embedded into active record objects
Stars: ✭ 48 (-21.31%)
Mutual labels:  activerecord
Activerecord json validator
🔩 ActiveRecord::JSONValidator makes it easy to validate JSON attributes against a JSON schema.
Stars: ✭ 220 (+260.66%)
Mutual labels:  activerecord
sinatra-bootstrap
My opinionated Sinatra base application
Stars: ✭ 14 (-77.05%)
Mutual labels:  activerecord
Anima
Minimal database operation library.
Stars: ✭ 210 (+244.26%)
Mutual labels:  activerecord
Occams Record
The missing high-efficiency query API for ActiveRecord
Stars: ✭ 240 (+293.44%)
Mutual labels:  activerecord
activerecord-migrations
A gem to simplify activerecord migrations in non-rails projects.
Stars: ✭ 15 (-75.41%)
Mutual labels:  activerecord
ar-search
Provides unified search model for Yii ActiveRecord
Stars: ✭ 31 (-49.18%)
Mutual labels:  activerecord
active snapshot
Simplified snapshots and restoration for ActiveRecord models and associations with a transparent white-box implementation
Stars: ✭ 67 (+9.84%)
Mutual labels:  activerecord

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 an instance to which dynamic attributes are added
  def initialize(model)
    @model = model
  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)
    @model = model   
  end
  
  def call
    case @model
      when Profile
        [
          # attribute definitions for Profile model
        ]
      when Document
        [
          # attribute definitions for Document model
        ]
      else
        []
    end
  end
  
end

How ActiveDynamic resolves dynamic attributes

When you work with unsaved models, ActiveDynamic will use provider_class to resolve a list of dynamic attributes, and it will store them alongside the model when the model is saved. So next time when you load that model from DB, ActiveDynamic won't look into provider_class and it will load only the dynamic attributes that were created when the model was saved for the first time.

If you want dynamic attributes to be resolved from provider_class for persisted models as well, you can use resolve_persisted configuration option:

# lib/initializers/dynamic_attribute.rb

ActiveDynamic.configure do |config|
  # ... 
  
  # you can set it to Bool value to apply the behavior to all models
  config.resolve_persisted = true
  
  # or you can set it to a Proc to configure the behavior on per-class basis
  config.resolve_persisted = Proc.new { |model| model.is_a?(Profile) ? true  : false }
end

Querying

This is still work in progress, so think twice before using it in production 🙂

ActiveDynamic provides where_dynamic class method, that you can use to search by dynamic fields. For example, if you have a Profile model with age attribute, you can use it like this:

  Profile.where_dynamic(age: 21)

At the moment, only hash arguments are supported.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].