Module: CustomFields::Source::ClassMethods

Defined in:
lib/custom_fields/source.rb

Instance Method Summary collapse

Instance Method Details

#custom_fields_for(name) ⇒ Object

Enhance a referenced collection OR the instance itself (by passing self) by providing methods to manage custom fields.

Examples:

class Company
  embeds_many :employees
  custom_fields_for :employees
end

class Employee
  embedded_in :company, inverse_of: :employees
  field :name, String
end

company.employees_custom_fields.build label: 'His/her position', name: 'position', kind: 'string'
company.save
company.employees.build name: 'Michael Scott', position: 'Regional manager'

Parameters:

  • name (String, Symbol)

    The name of the relation.



256
257
258
259
260
261
262
263
# File 'lib/custom_fields/source.rb', line 256

def custom_fields_for(name)
  declare_embedded_in_definition_in_custom_field(name)

  # stores the relation name
  _custom_fields_for << name.to_s

  extend_for_custom_fields(name)
end

#custom_fields_for?(name) ⇒ true, false

Determines if the relation is enhanced by the custom fields

Examples:

the Person class has somewhere in its code this: “custom_fields_for :addresses”

Person.custom_fields_for?(:addresses)

Parameters:

  • name (String, Symbol)

    The name of the relation.

Returns:

  • (true, false)

    True if enhanced, false if not.



233
234
235
# File 'lib/custom_fields/source.rb', line 233

def custom_fields_for?(name)
  _custom_fields_for.include?(name.to_s)
end