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



219
220
221
222
223
224
225
226
227
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 219

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



296
297
298
299
300
301
302
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 296

def fieldset(sym)
  if match_fieldset_associator?(sym)
    return DynamicFieldsets::Fieldset.find_by_nkey(:nkey => self.dynamic_fieldsets[sym][:fieldset])
  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



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 272

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,
      :fieldset => self.dynamic_fieldsets[sym][:fieldset]).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 => DynamicFieldsets::Fieldset.find_by_nkey(self.dynamic_fieldsets[sym][:fieldset]))
    end
    return fsa
  else
    return nil
  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)


257
258
259
260
261
262
263
264
265
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 257

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)


249
250
251
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 249

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)


235
236
237
238
239
240
241
242
243
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 235

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



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 58

def run_dynamic_fieldset_validations!
  # for each fsa
  self.dynamic_fieldsets.keys.each do |key|
    fsa = self.fieldset_associator(key)
    fsa_tag_id = "fsa-" + 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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 76

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 = "fsa-" + fsa_id.to_s
    field_tag_id = "field-" + 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
    puts "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?



49
50
51
52
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 49

def run_validations!
  run_dynamic_fieldset_validations!
  super
end

#save_dynamic_fieldsetsObject

hacky system to save fieldset values needs to be refactored and tested

among other things, it can edit field records for random fsas if the wrong information comes from the controller



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 152

def save_dynamic_fieldsets
  values = self.dynamic_fieldset_values
  if !values.nil?
    values.keys.each do |key|
      if key.start_with?("fsa-")
        key_id = key.gsub(/^fsa-/, "")

        if key_id.eql? ""
        then fsa = DynamicFieldsets::FieldsetAssociator.create(
          :fieldset_id => values[key][:fieldset_id],
          :fieldset_model_id => self.id,
          :fieldset_model_type => self.class.name,
          :fieldset_model_name => values[key][:fieldset_model_name] )
        else fsa = DynamicFieldsets::FieldsetAssociator.find_by_id key_id
        end
          
        values[key].keys.each do |sub_key| # EACH FIELD
          if sub_key.start_with?("field-")
            sub_key_id = sub_key.gsub(/^field-/, "")
            
            this_value = values[key][sub_key]
            if this_value.is_a? Array
            then # multiple values
              field_records = DynamicFieldsets::FieldRecord.where(:fieldset_associator_id => fsa.id, :fieldset_child_id => sub_key_id)
              
              this_value.each do |value|
                if field_records.select{ |record| record.value.eql? value }.empty? # record does not exist?
                  #ADD
                  DynamicFieldsets::FieldRecord.create( :fieldset_associator_id => fsa.id,
                                                        :fieldset_child_id => sub_key_id,
                                                        :value => value)
                end
              end
              field_records.each do |record|
                if !this_value.include? record.value then
                  #DELETE
                  record.destroy
                else
                  #KEEP
                end
              end
              
            else # single value
              # retrieve record
              field_record = DynamicFieldsets::FieldRecord.where(:fieldset_associator_id => fsa.id, :fieldset_child_id => sub_key_id).first
              if field_record.nil? # create record
                field_record = DynamicFieldsets::FieldRecord.create(:fieldset_associator_id => fsa.id, :fieldset_child_id => sub_key_id, :value => this_value)
              else # update record
                field_record.value = this_value
                field_record.save
              end
            end
            
          end
        end
      end
    end
  end
  self.dynamic_fieldset_values = nil
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)

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.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 121

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)" ) # month
    datefield += '-'
    datefield += '%02d' % post.delete( "#{field}(3i)" ) # day
    if post.keys.include? "#{field}(4i)" then
      datefield += ' '
      datefield += '%02d' % post.delete( "#{field}(4i)" ) # hour
      datefield += ':'
      datefield += '%02d' % post.delete( "#{field}(5i)" ) # 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

Parameters:

  • params (Hash)

    The parameters from the controller that include fsa tags



105
106
107
108
109
110
111
# File 'lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb', line 105

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