CustomFields
Manage custom fields to a mongoid document or a collection. This module is one of the core features we implemented in our custom cms, LocomotiveCMS. Basically, its aim is to provide to editors a way to manage extra fields to a Mongoid document through, for instance, a web UI.
The main goals:
- offering a very secure way to add / edit / delete extra fields to a Mongoid document
- scoping the modifications added to a Mongoid document so that other documents of the same class won’t be updated.
Requirements
ActiveSupport 3.2.13, MongoDB 2.0 and Mongoid 3.1.3
Examples
On a has_many relationship
class Company
include CustomFields::Source</code>
<code> has_many :employees</code>
<code> custom_fields_for :employees
end</code>
<code>class Employee
include CustomFields::Target</code>
<code> field :name, String</code>
<code> belongs_to :company, :inverse_of => :employees
end</code>
<code>company = Company.new
company.employees_custom_fields.build :label => 'His/her position', :name => 'position', :type => 'string', :required => true</code>
<code>company.save</code>
<code>company.employees.build :name => 'Michael Scott', :position => 'Regional manager'</code>
<code>another_company = Company.new
employee = another_company.employees.build
employee.position # returns a "not defined method" error
On the class itself
[IN PROGRESS]
class Company
custom_fields_for_itself
end</code>
<code>company = Company.new
company.self_metadata_custom_fields.build :label => 'Shipping Address', :name => 'address', :type => 'text'</code>
<code>company.save</code>
<code>company.self_metadata.address = '700 S Laflin, 60607 Chicago'</code>
<code>another_company = Company.new
other_company.self_metadata.address # returns a "not defined method" error
Contact
Feel free to contact me at didier at nocoffee dot fr.
Copyright © 2013 NoCoffee, released under the MIT license