Class: DynamicFieldsets::Field
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- DynamicFieldsets::Field
- Defined in:
- app/models/dynamic_fieldsets/field.rb
Overview
Base class for various fieldtypes, i.e. questions
Direct Known Subclasses
CheckboxField, DateField, DatetimeField, InstructionField, MultipleSelectField, RadioField, SelectField, TextField, TextareaField
Class Method Summary collapse
-
.descendant_collection ⇒ Array<String>
Humanized collection of descendants.
-
.descendants ⇒ Array
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.
Instance Method Summary collapse
-
#collect_default_values ⇒ Object
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.
-
#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.
-
#display_type ⇒ String
Very similar to the output in descendant collection.
-
#form_footer_partial ⇒ String
Name of the input footer for the form.
-
#form_header_partial ⇒ String
Name of the input header for the form.
-
#form_partial ⇒ String
Name of partial to render for the form.
-
#form_partial_locals(args) ⇒ Hash
Data needed for the form partial.
-
#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.
-
#get_values_using_fsa_and_fsc(fsa, fsc) ⇒ Nil
This method should be overridden by the field subclasses.
-
#has_defaults? ⇒ Boolean
False if field_default.value is empty.
-
#html_attribute_hash ⇒ Hash
A hash of html attribute key: value pairs.
-
#in_use? ⇒ Boolean
True if there are any field records for the field or if it is in any fieldsets.
-
#show_footer_partial ⇒ String
This method must be overriden if show_footer_partial returns true.
-
#show_header_partial ⇒ String
This method must be overriden if show_header_partial returns true.
-
#show_partial ⇒ String
This method must be overriden.
-
#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.
-
#update_field_records(fsa, fieldset_child, value) ⇒ Object
Updates the field records for the field based on the given values.
-
#use_form_footer_partial? ⇒ Boolean
By default, use the footer partial.
-
#use_form_header_partial? ⇒ Boolean
By default, use the header partial.
-
#use_show_footer_partial? ⇒ Boolean
By default, do not use the footer partial.
-
#use_show_header_partial? ⇒ Boolean
By default, do not use the header partial.
-
#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.
Class Method Details
.descendant_collection ⇒ Array<String>
Returns 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 |
.descendants ⇒ Array
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
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_values ⇒ Object
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
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_type ⇒ String
Very similar to the output in descendant collection
56 57 58 |
# File 'app/models/dynamic_fieldsets/field.rb', line 56 def display_type type.gsub("DynamicFieldsets::", "").underscore.humanize end |
#form_footer_partial ⇒ String
Returns Name of the input footer for the form.
78 79 80 |
# File 'app/models/dynamic_fieldsets/field.rb', line 78 def "/dynamic_fieldsets/form_partials/input_footer" end |
#form_header_partial ⇒ String
Returns 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_partial ⇒ String
Returns 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.
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
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.
174 175 176 |
# File 'app/models/dynamic_fieldsets/field.rb', line 174 def has_defaults? return self.field_defaults.length > 0 end |
#html_attribute_hash ⇒ Hash
Returns 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.
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 |
#show_footer_partial ⇒ String
This method must be overriden if show_footer_partial returns true
135 136 137 |
# File 'app/models/dynamic_fieldsets/field.rb', line 135 def "/dynamic_fieldsets/show_partials/show_incomplete_footer" end |
#show_header_partial ⇒ String
This method must be overriden if show_header_partial returns true
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_partial ⇒ String
This method must be overriden
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
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
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 |
#use_form_footer_partial? ⇒ Boolean
Returns By default, use the footer partial.
83 84 85 |
# File 'app/models/dynamic_fieldsets/field.rb', line 83 def true end |
#use_form_header_partial? ⇒ Boolean
Returns 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 |
#use_show_footer_partial? ⇒ Boolean
Returns By default, do not use the footer partial.
140 141 142 |
# File 'app/models/dynamic_fieldsets/field.rb', line 140 def false end |
#use_show_header_partial? ⇒ Boolean
Returns 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
187 188 189 |
# File 'app/models/dynamic_fieldsets/field.rb', line 187 def false end |