Module: DynamicFieldsets::DynamicFieldsetsInModel::InstanceMethods

Defined in:
lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

Matches methods that match named_fieldset and named_fieldset_fieldset Or calls super

Parameters:

  • sym (Symbol)

    The name of the method

  • args (Array)

    The arguments of the method



214
215
216
217
218
219
220
221
222
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 214

def method_missing(sym, *args)
  if match_fieldset_associator?(sym)
    return fieldset_associator(sym)
  elsif match_fieldset?(sym)
    return fieldset(sym)
  else
    super(sym, *args)
  end
end

Instance Method Details

#fieldset(sym) ⇒ Object

Returns the fieldset object for the named fieldset

Parameters:

  • The (Symbol)

    name of the named fieldset

  • The (Fieldset)

    fieldset object for the named fieldset



291
292
293
294
295
296
297
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 291

def fieldset(sym)
  if match_fieldset_associator?(sym)
    return DynamicFieldsets::Fieldset.where(:nkey => self.dynamic_fieldsets[sym][:fieldset]).first
  else
    return nil
  end
end

#fieldset_associator(sym) ⇒ Object

Returns the fieldset associator object for the named fieldset. If one doesn’t exist, it creates it and returns it

Parameters:

  • The (Symbol)

    name of the name fieldset

  • The (FieldsetAssociator)

    fieldset associator object for the named fieldset



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 267

def fieldset_associator(sym)
  if match_fieldset_associator?(sym)
    fsa = DynamicFieldsets::FieldsetAssociator.find_by_fieldset_model_parameters(
      :fieldset_model_id => self.id,
      :fieldset_model_type => self.class.name, 
      :fieldset_model_name => sym.to_s,
      :fieldset_nkey => self.dynamic_fieldsets[sym][:fieldset].to_s).first
    if fsa.nil?
      fsa = DynamicFieldsets::FieldsetAssociator.new(
      :fieldset_model_id => self.id,
      :fieldset_model_type => self.class.name,
      :fieldset_model_name => sym.to_s,
      :fieldset_id => DynamicFieldsets::Fieldset.where(:nkey => self.dynamic_fieldsets[sym][:fieldset].to_s).first.id)
    end
    return fsa
  else
    return nil
  end
end

#initialize_fieldset_associatorsObject

If the fieldset is set to initialize_on_create, then attempt to create the fsa



54
55
56
57
58
59
60
61
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 54

def initialize_fieldset_associators
  self.dynamic_fieldsets.each_pair do |key, options|
    if options[:initialize_on_create]
      fsa = fieldset_associator(key)
      fsa.save if fsa.new_record?
    end
  end
end

#match_fieldset?(sym) ⇒ Boolean

Returns whether a method name matches a named fieldset followed by ‘_fieldset’

Parameters:

  • The (Symbol)

    name of the method

  • Whether (Boolean)

    the method name matches a named fieldset

Returns:

  • (Boolean)


252
253
254
255
256
257
258
259
260
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 252

def match_fieldset?(sym)
  sym_string = sym.to_s
  if !sym_string.match(/_fieldset$/)
    return false
  else
    sym_string.gsub!(/_fieldset$/, "")
    return self.dynamic_fieldsets.keys.include?(sym_string.to_sym)
  end
end

#match_fieldset_associator?(sym) ⇒ Boolean

Returns whether a method name matches a named fieldset

Parameters:

  • The (Symbol)

    name of the method

  • Whether (Boolean)

    the method name matches a named fieldset

Returns:

  • (Boolean)


244
245
246
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 244

def match_fieldset_associator?(sym)
  return self.dynamic_fieldsets.keys.include?(sym)
end

#respond_to?(sym, *args) ⇒ Boolean

Matches methods that match named_fieldset and named_fieldset_fieldset Or calls super

Parameters:

  • sym (Symbol)

    The name of the method

  • args (Array)

    The arguments of the method

Returns:

  • (Boolean)


230
231
232
233
234
235
236
237
238
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 230

def respond_to?(sym, *args)
  if match_fieldset_associator?(sym)
    return true
  elsif match_fieldset?(sym)
    return true
  else
    super(sym, *args)
  end
end

#run_dynamic_fieldset_validations!Object

Iterates over the fieldset associator’s children and adds errors

Will not validate fieldsets that are missing from the dynamic_fieldsets_values hash This means that if the data is not provided by the controller, no checks will be run



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 78

def run_dynamic_fieldset_validations!
  # for each fsa
  self.dynamic_fieldsets.keys.each do |key|
    fsa = self.fieldset_associator(key)
    fsa_tag_id = DynamicFieldsets.config.form_fieldset_associator_prefix + fsa.id.to_s

    # check if the values are set, if it matches the current fsa, and if it matches the current fieldset
    if !self.dynamic_fieldset_values.nil? && self.dynamic_fieldset_values.has_key?(fsa_tag_id) && self.dynamic_fieldset_values[fsa_tag_id][:fieldset_model_name] == key
      run_fieldset_child_validations!(fsa.id, fsa.fieldset)
    end
  end
end

#run_fieldset_child_validations!(fsa_id, child) ⇒ Object

Checks if a fieldset child is required and adds an error if it’s value is blank Adds errors to the self.errors array, does not return them

Parameters:

  • fsa_id (Integer)

    The id for the fieldset associator the child belongs to

  • child (Field or Fieldset)

    The child of the fieldset associator



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 96

def run_fieldset_child_validations!(fsa_id, child)
  if child.is_a?(DynamicFieldsets::Fieldset)
    # if a fieldset, then recurse
    child.children.each do |grand_child|
      run_fieldset_child_validations!(fsa_id, grand_child)
    end
  elsif child.is_a?(DynamicFieldsets::Field)
    # if a child, check if the params value is set, check if it is required, check if it satisfies condition
    fsa_tag_id = DynamicFieldsets.config.form_fieldset_associator_prefix + fsa_id.to_s
    field_tag_id = DynamicFieldsets.config.form_field_prefix + child.id.to_s
    if !self.dynamic_fieldset_values[fsa_tag_id].has_key?(field_tag_id)
      self.errors.add(:base, child.label + " is missing from the form data")
    else
      # get the value
      value = self.dynamic_fieldset_values[fsa_tag_id][field_tag_id]
      if child.required?
        # empty works on array or string, so simplifying here
        self.errors.add(:base, child.label + " is required") if value.nil? || value.empty?
      end
    end
  else
    # found a major problem, not sure how to get here
    throw "found a child that wasn't a field or fieldset" + child.inspect
  end
end

#run_validations!Boolean

Overrides the ActiveModel Validations run_validations! method It additionally adds validations for the fields that are required

I am not sure if this is the correct place to put this. Seems like a reasonable one.

Returns:

  • (Boolean)

    The result of run_validations! with the extra errors added, should be true if errors.empty?



69
70
71
72
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 69

def run_validations!
  run_dynamic_fieldset_validations!
  super
end

#save_dynamic_fieldsetsObject

Given the form values, finds the keys that correspond to fieldsets then passes the value for the key to the fieldset associator object for saving into individual field records



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 175

def save_dynamic_fieldsets
  values = self.dynamic_fieldset_values
  if !values.nil?
    values.keys.each do |key|
      if key.start_with?(DynamicFieldsets.config.form_fieldset_associator_prefix)
        save_fsa(key, values[key])
      end
    end
  end
  self.dynamic_fieldset_values = nil
end

#save_fsa(key, fsa_values) ⇒ Object

save all of the fields and fieldsets in the values hash for this fieldset associator

Parameters:

  • The (String)

    key that includes the fieldset associator’s id

  • fsa_values (Hash)

    An array of input information from the form



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 191

def save_fsa(key, fsa_values)
  key_id = key.gsub(/^#{DynamicFieldsets.config.form_fieldset_associator_prefix}/, "")

  if(key_id == "")
    fsa = DynamicFieldsets::FieldsetAssociator.create(
      :fieldset_id => fsa_values[:fieldset_id],
      :fieldset_model_id => self.id,
      :fieldset_model_type => self.class.name,
      :fieldset_model_name => fsa_values[:fieldset_model_name] 
    )
  else 
    fsa = DynamicFieldsets::FieldsetAssociator.find(key_id)
  end

  fsa.update_fieldset_records_with_form_information(fsa_values)
end

#set_date_to_mysql(post) ⇒ Hash

This turns your date fields into a MySQL-happy single format. This modifies the hash.

This method may cause bugs for servers not using UTC time because of the way rails deals with time conversions. If the query receives a string instead of a time object, time zone information may be saved incorrectly. (1-25-2012)

At some point this should be moved to the date_field and datetime_field models. Right now, it needs to stay here because we are taking the values from the form and iterating over the keys. This will not work because the date information is stored int 3-5 keys. We need to change the form data parser to get a list of expected field and iterate over them, looking for the values in the form post. That is a big change that we don’t have time for now. (JH 2-27-2012)

Parameters:

  • post (Hash)

    The post parameters that include date fields like date(1i), date(2i), …

Returns:

  • (Hash)

    The modified hash containing one key-pair value in YYYY-MM-DD[ hh:mm] format.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 148

def set_date_to_mysql( post )
  # 'dates' is an array of the "field-ID"s that have multiple date fields of the format field(1i), field(2i), ...
  # e.g. [ "field-4", "field-7" ]
  dates = post.select{ |k| k =~ /\([0-9]i\)/ }.keys.map{ |k| k.gsub /\([0-9]i\)/, '' }.uniq
  dates.each do |field|
    datefield  = ''
    datefield +=          post.delete( "#{field}(1i)" ) # year
    datefield += '-'
    datefield += '%02d' % post.delete( "#{field}(2i)" ).to_i # month
    datefield += '-'
    datefield += '%02d' % post.delete( "#{field}(3i)" ).to_i # day
    if post.keys.include? "#{field}(4i)" then
      datefield += ' '
      datefield += '%02d' % post.delete( "#{field}(4i)" ).to_i # hour
      datefield += ':'
      datefield += '%02d' % post.delete( "#{field}(5i)" ).to_i # minute
      datefield += ':'
      datefield += '00' # second
    end
    # adding the formatted string to the hash to be saved.
    post.merge! field => datefield
  end
  return post
end

#set_fieldset_values(params) ⇒ Object

Stores data from the controller into the dynamic_fieldset_values instance variable Also combines the date and time fields into a single field

Parameters:

  • params (Hash)

    The parameters from the controller that include fsa tags



126
127
128
129
130
131
132
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 126

def set_fieldset_values( params )
  values = params.select{ |key| key.match(/^#{DynamicFieldsets.config.form_fieldset_associator_prefix}/) }
  values.keys.each do |key|
    values[key] = set_date_to_mysql( values[key] )
  end
  self.dynamic_fieldset_values = values
end