0.01
No commit activity in last 3 years
No release in over 3 years
Custom fields forActiveRecord models, suppport Rails 4!
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.3
>= 0

Runtime

>= 4.0.0
 Project Readme

HasMagicFields

Gem Version CI Status

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.

Installation

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

Usage

Model

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 => "charlie@example.com")
@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", :is_required => true)
@charlie.create_magic_field(:name => "birthday", :datatype => :date)
@charlie.create_magic_field(:name => "salary", :default => "40000", :pretty_name => "Yearly Salary")

The :datatype option supports: :check_box_boolean, :date, :datetime, :integer

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

Inherited Model

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

Get All Magic Fields

@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

##To Do

Here's a short list of things that need to be done to polish up this gem:

  • more data_type sppuort
  • Benchmark and optimize

Maintainers

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Credits

  • 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