项目作者: koss-lebedev

项目描述 :
Gem that allows to add attributes to your active models dynamically
高级语言: Ruby
项目地址: git://github.com/koss-lebedev/active_dynamic.git
创建时间: 2016-12-23T07:07:33Z
项目社区:https://github.com/koss-lebedev/active_dynamic

开源协议:MIT License

下载


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 @koss_lebedev/how-to-dynamically-add-attributes-to-your-activerecord-models-e233b17ad695#.k66n002of">an article explaining how to use active_dynamic.

Installation

Add this line to your application’s Gemfile:

  1. gem 'active_dynamic'

And then execute:

  1. $ bundle

Or install it yourself as:

  1. $ 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:

  1. class Profile < ActiveRecord::Base
  2. has_dynamic_attributes
  3. # ...
  4. end

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

  1. # lib/initializers/dynamic_attribute.rb
  2. ActiveDynamic.configure do |config|
  3. config.provider_class = ProfileAttributeProvider
  4. end
  5. class ProfileAttributeProvider
  6. # Constructor will receive an instance to which dynamic attributes are added
  7. def initialize(model)
  8. @model = model
  9. end
  10. # This method has to return array of dynamic field definitions.
  11. # You can get it from the configuration file, DB, etc., depending on your app logic
  12. def call
  13. [
  14. # attribute definition has to specify attribute display name
  15. ActiveDynamic::AttributeDefinition.new('biography'),
  16. # Optionally you can provide datatype, system name, and default value.
  17. # If system name is not specified, it will be generated automatically from display name
  18. ActiveDynamic::AttributeDefinition.new('age', datatype: ActiveDynamic::DataType::Integer, default_value: 18)
  19. ]
  20. end
  21. end

To resolve dynamic attribute definitions for more than one model:

  1. class Profile < ActiveRecord::Base
  2. has_dynamic_attributes
  3. # ...
  4. end
  5. class Document < ActiveRecord::Base
  6. has_dynamic_attributes
  7. # ...
  8. end
  9. class ProfileAttributeProvider
  10. def initialize(model)
  11. @model = model
  12. end
  13. def call
  14. case @model
  15. when Profile
  16. [
  17. # attribute definitions for Profile model
  18. ]
  19. when Document
  20. [
  21. # attribute definitions for Document model
  22. ]
  23. else
  24. []
  25. end
  26. end
  27. 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:

  1. # lib/initializers/dynamic_attribute.rb
  2. ActiveDynamic.configure do |config|
  3. # ...
  4. # you can set it to Bool value to apply the behavior to all models
  5. config.resolve_persisted = true
  6. # or you can set it to a Proc to configure the behavior on per-class basis
  7. config.resolve_persisted = Proc.new { |model| model.is_a?(Profile) ? true : false }
  8. 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:

  1. 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.