Hard fork of has_magic_fields
Allows the addition of custom "magic" fields and attributes on a per-model or per-parent-model basis. This is useful for situations where custom fields are required for a specific model or for multi-user, multi-account environments where accounts can customize attributes for subordinate models.
Add this line to your application's Gemfile:
gem 'has_magic_fields'
And then execute:
$ bundle
Create the migrations for MagicFileds and migrate:
rails g has_magic_fields:install
rake db:migrate
Sprinkle a little magic into an existing model:
class Person < ActiveRecord::Base
include HasMagicFields::Extend
has_magic_fields
end
Add magic fields to your model:
@charlie = Person.create(email: "[email protected]")
@charlie.create_magic_field(name: "first_name")
Supply additional options if you have more specific requirements for your fields:
@charlie.create_magic_field(name: "last_name", required: true)
@charlie.create_magic_field(name: "birthday", datatype: :date)
@charlie.create_magic_field(name: "salary", default: "40000", label: "Yearly Salary")
Use your new fields just like you would with any other ActiveRecord attribute:
@charlie.first_name = "Charlie"
@charlie.last_name = "Magic!"
@charlie.birthday = Date.today
@charlie.save
Find @charlie and inspect him:
@charlie = User.find(@charlie.id)
@charlie.first_name #=> "Charlie"
@charlie.last_name #=> "Magic!"
@charlie.birthday #=> #<Date: 4908497/2,0,2299161>
@charlie.salary #=> "40000", this is from :salary having a :default
A child can inherit magic fields from a parent. To do this, declare the parent as having magic fields:
class Account < ActiveRecord::Base
include HasMagicFields::Extend
has_many :users
has_magic_fields
end
@account = Account.create(name: "BobCorp", type_scoped: "User")
And declare the child as having magic fields :through the parent.
class User < ActiveRecord::Base
include HasMagicFields::Extend
belongs_to :account
has_magic_fields through: :account
end
@alice = User.create(name: "alice", account: @account)
To see all the magic fields available for a type_scoped(User) child from its parent:
@alice.magic_fields #=> [#<MagicColumn>,...]
To add magic fields, go through the parent or child:
@alice.create_magic_field(...)
@account.create_magic_field(…, type_scoped: "User")
All User children for a given parent will have access to the same magic fields:
@alice.create_magic_field(name: "salary")
@alice.salary = "40000"
@bob = User.create(name: "bob", account: @account)
# Magic! No need to add the column again!
@bob.salary = "50000"
###Different Model Inherited from The Samle Model the other modle Inherited from Account
class Product < ActiveRecord::Base
include HasMagicFields::Extend
belongs_to :account
has_magic_fields through: :account
end
@product = Product.create(name: "car", account: @account)
@product haven't salary magic field, @product.salary should raise NoMethodError
parent @account also haven't salary magic field
@account.magic_fields #get all meagic_fields both self and children
@account.magic_fields_with_scoped #get all meagic_fields self
@account.magic_fields_with_scoped("User") #get all meagic_fields User model
- Benchmark and optimize
- More datatype support (with ActiveRecord::Type.lookup)
- Use to_i instead of to_int
- Remove self.datatypes (with ActiveRecord::Type.lookup it is no longer necessary)
- Update migration template
- Add spec :on_potential_false_positives
- magic_attributes are only saved when calling #save
- Fix #create_magic_filed
- Add rubocop
- Remove lambdas from specs
- Use required instead is_required
- Use label instead pretty_name
- Remove pretty_name_*
- Support for migration[version]
- Avoid (partially) N+1
- Davy Zhou(ikeqiao)
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
- Thank you to Brandon Keene for his original work making this plugin.
- Thank you to latortuga for his original work making this plugin. has_magic_fields