Module: DynamicFieldsets::DynamicFieldsetsInModel::ClassMethods

Defined in:
lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_dynamic_fieldset(args) ⇒ Object

Adds a dynamic fieldset to a model

Each set of options should be put inside of a hash with the unqiue name for the fieldset as the key: :child_form => options Options: fieldset (mandatory): The unique name of the fieldset the class is associated with multiple (optional): Boolean value to allow multiple answer sets for the same fieldset in the class.

Deafult to false.  Not curently implemented (7-25-2011).

initialize_on_create (optional): Create the fieldste associator after create

Parameters:

  • args (Hash)

    A hash of arguments for the fieldsets.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 25

def acts_as_dynamic_fieldset(args)
  mattr_accessor :dynamic_fieldsets unless self.respond_to?(:dynamic_fieldsets)
  self.dynamic_fieldsets = {} unless self.dynamic_fieldsets.is_a?(Hash)

  # default values for the arguments
  # fieldset is mandatory
  defaults = {
    :multiple => false,
    :initialize_on_create => false
  }

  args.each_pair do |key, value|
    value_with_defaults = defaults.merge(value)
    self.dynamic_fieldsets[key] = value_with_defaults
  end

  # hacky system to save fieldset values
  # needs to be refactored and tested
  mattr_accessor :dynamic_fieldset_values unless self.respond_to?(:dynamic_fieldset_values)
  after_create :initialize_fieldset_associators
  after_save :save_dynamic_fieldsets

  include DynamicFieldsets::DynamicFieldsetsInModel::InstanceMethods
end