Class: DynamicFieldsets::Field

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/dynamic_fieldsets/field.rb

Overview

Base class for various fieldtypes, i.e. questions

Author:

  • Jeremiah Hemphill, Ethan Pemble

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.descendant_collectionArray<String>

Returns Humanized collection of descendants.

Returns:

  • (Array<String>)

    Humanized collection of descendants



50
51
52
# File 'app/models/dynamic_fieldsets/field.rb', line 50

def self.descendant_collection
  descendants.collect { |d| [d.to_s.gsub("DynamicFieldsets::", "").underscore.humanize, d.to_s ] }
end

.descendantsArray

Either calls the defaul descendants method or pulls the data from the config Deals with weird single table inheritance issues with cache classes off Causes errors only in development mode

Returns:

  • (Array)

    An array of descendant class constants



41
42
43
44
45
46
47
# File 'app/models/dynamic_fieldsets/field.rb', line 41

def self.descendants
  if ::Rails.application.config.cache_classes
    super
  else
    DynamicFieldsets.config.available_field_types.map(&:constantize)
  end
end

Instance Method Details

#collect_default_valuesObject

this collects defaults so that we can use them for fields note that this should be overridden when the field uses field options in that case, it should return field option ids instead of field option names

I’m sorry



196
197
198
# File 'app/models/dynamic_fieldsets/field.rb', line 196

def collect_default_values
  field_defaults.collect { |d| d[:value] }
end

#collect_field_records_by_fsa_and_fsc(fsa, fsc) ⇒ Array

Collects the field records for the field so they can be used on the front end These are only the currently saved values in the database, don’t worry about defaults here

This works for fields that do not use field options

Returns:

  • (Array)

    An array of field record values



216
217
218
219
220
# File 'app/models/dynamic_fieldsets/field.rb', line 216

def collect_field_records_by_fsa_and_fsc(fsa, fsc)
  # I think this needs to return some sort of hash
  records = DynamicFieldsets::FieldRecord.where(:fieldset_associator_id => fsa.id, :fieldset_child_id => fsc.id)
  return records.collect { |r| { :value => r.value } }
end

#display_typeString

Very similar to the output in descendant collection

Returns:

  • (String)

    A pretty version of the type



56
57
58
# File 'app/models/dynamic_fieldsets/field.rb', line 56

def display_type
  type.gsub("DynamicFieldsets::", "").underscore.humanize
end

Returns Name of the input footer for the form.

Returns:

  • (String)

    Name of the input footer for the form



78
79
80
# File 'app/models/dynamic_fieldsets/field.rb', line 78

def form_footer_partial
  "/dynamic_fieldsets/form_partials/input_footer"
end

#form_header_partialString

Returns Name of the input header for the form.

Returns:

  • (String)

    Name of the input header for the form



68
69
70
# File 'app/models/dynamic_fieldsets/field.rb', line 68

def form_header_partial
  "/dynamic_fieldsets/form_partials/input_header"
end

#form_partialString

Returns Name of partial to render for the form.

Returns:

  • (String)

    Name of partial to render for the form



63
64
65
# File 'app/models/dynamic_fieldsets/field.rb', line 63

def form_partial
  "/dynamic_fieldsets/form_partials/" + self.class.to_s.gsub("DynamicFieldsets::", "").underscore
end

#form_partial_locals(args) ⇒ Hash

Returns Data needed for the form partial.

Returns:

  • (Hash)

    Data needed for the form partial



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/dynamic_fieldsets/field.rb', line 88

def form_partial_locals(args)
  output = {
    :fsa => args[:fsa],
    :fieldset_child => args[:fieldset_child],
    :attrs => self.html_attribute_hash,
    # for use in helpers like text_field and date_select
    :object => "#{DynamicFieldsets::config.form_fieldset_associator_prefix}#{args[:fsa].id}",
    :method => "#{DynamicFieldsets::config.form_field_prefix}#{args[:fieldset_child].id}",
  }
  # name for use in helpers like select_tag, check_box_tag, or anything ending with _tag
  # this is more of a convenience method
  output[:name] = "#{output[:object]}[#{output[:method]}]"
  output[:id] = "#{output[:object]}_#{output[:method]}"
  return output
end

#get_value_for_show(value) ⇒ Object

given a value hash for a field, return the part that needs to be shown on the show page



163
164
165
166
167
168
169
# File 'app/models/dynamic_fieldsets/field.rb', line 163

def get_value_for_show(value)
  if value.nil?
    return nil
  else
    value[:value]
  end
end

#get_values_using_fsa_and_fsc(fsa, fsc) ⇒ Nil

This method should be overridden by the field subclasses

Parameters:

Returns:

  • (Nil)

    An empty result



205
206
207
# File 'app/models/dynamic_fieldsets/field.rb', line 205

def get_values_using_fsa_and_fsc(fsa, fsc)
  return nil
end

#has_defaults?Boolean

Returns False if field_default.value is empty.

Returns:

  • (Boolean)

    False if field_default.value is empty



174
175
176
# File 'app/models/dynamic_fieldsets/field.rb', line 174

def has_defaults?
  return self.field_defaults.length > 0
end

#html_attribute_hashHash

Returns A hash of html attribute key: value pairs.

Returns:

  • (Hash)

    A hash of html attribute key: value pairs



105
106
107
108
109
# File 'app/models/dynamic_fieldsets/field.rb', line 105

def html_attribute_hash
  attrs = {}
  field_html_attributes.each{ |a| attrs.merge! a.attribute_name.to_sym => a.value } if !field_html_attributes.empty?
  return attrs
end

#in_use?Boolean

Returns True if there are any field records for the field or if it is in any fieldsets.

Returns:

  • (Boolean)

    True if there are any field records for the field or if it is in any fieldsets



179
180
181
# File 'app/models/dynamic_fieldsets/field.rb', line 179

def in_use?
  self.fieldset_children.count { |child| !child.fieldset_id.nil? || !child.field_records.empty? } > 0
end

This method must be overriden if show_footer_partial returns true

Returns:

  • (String)

    Name of the input footer for the show



135
136
137
# File 'app/models/dynamic_fieldsets/field.rb', line 135

def show_footer_partial
  "/dynamic_fieldsets/show_partials/show_incomplete_footer"
end

#show_header_partialString

This method must be overriden if show_header_partial returns true

Returns:

  • (String)

    Name of the input header for the show



123
124
125
# File 'app/models/dynamic_fieldsets/field.rb', line 123

def show_header_partial
  "/dynamic_fieldsets/show_partials/show_incomplete_header"
end

#show_partialString

This method must be overriden

Returns:

  • (String)

    Name of partial to render for the show page



116
117
118
# File 'app/models/dynamic_fieldsets/field.rb', line 116

def show_partial
  "/dynamic_fieldsets/show_partials/show_incomplete"
end

#show_partial_locals(args) ⇒ Hash

Note that this method is really weird You would think that the value displayed should be figured out here but instead, it is figured out first, then passed in, in the arguments hash

Returns:

  • (Hash)

    Information needed for the show partial, don’t know what I need yet



149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/models/dynamic_fieldsets/field.rb', line 149

def show_partial_locals(args)
  # these should be incredibly temporary
  output = {
    :value => args[:value],
    :values => args[:values],
    :label => self.label,
    :object => "#{DynamicFieldsets::config.form_fieldset_associator_prefix}#{args[:fsa].id}",
    :method => "#{DynamicFieldsets::config.form_field_prefix}#{args[:fieldset_child].id}",
  }
  output[:id] = "#{output[:object]}_#{output[:method]}"
  return output
end

#update_field_records(fsa, fieldset_child, value) ⇒ Object

Updates the field records for the field based on the given values

This must be overridden if it is used

Parameters:



229
230
231
# File 'app/models/dynamic_fieldsets/field.rb', line 229

def update_field_records(fsa, fieldset_child, value)
  throw "Field.update_field_records must be overridden to save data from the form."
end

Returns By default, use the footer partial.

Returns:

  • (Boolean)

    By default, use the footer partial



83
84
85
# File 'app/models/dynamic_fieldsets/field.rb', line 83

def use_form_footer_partial?
  true
end

#use_form_header_partial?Boolean

Returns By default, use the header partial.

Returns:

  • (Boolean)

    By default, use the header partial



73
74
75
# File 'app/models/dynamic_fieldsets/field.rb', line 73

def use_form_header_partial?
  true
end

Returns By default, do not use the footer partial.

Returns:

  • (Boolean)

    By default, do not use the footer partial



140
141
142
# File 'app/models/dynamic_fieldsets/field.rb', line 140

def use_show_footer_partial?
  false
end

#use_show_header_partial?Boolean

Returns By default, do not use the header partial.

Returns:

  • (Boolean)

    By default, do not use the header partial



128
129
130
# File 'app/models/dynamic_fieldsets/field.rb', line 128

def use_show_header_partial?
  false
end

#uses_field_options?Boolean

Fields such as selects, checkboxes, and radios use predefined field options for their values By default, a field does not use field options

Returns:

  • (Boolean)

    Whether the field uses field options, defaults to false



187
188
189
# File 'app/models/dynamic_fieldsets/field.rb', line 187

def uses_field_options?
  false
end