dynamic_fieldsets 0.0.16 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rdebugrc +3 -0
- data/.rspec +1 -0
- data/CHANGELOG +7 -0
- data/Gemfile +17 -8
- data/Gemfile.lock +77 -60
- data/README.rdoc +1 -1
- data/VERSION +1 -1
- data/app/controllers/dynamic_fieldsets/fields_controller.rb +5 -4
- data/app/helpers/dynamic_fieldsets_helper.rb +48 -131
- data/app/models/dynamic_fieldsets.rb +2 -0
- data/app/models/dynamic_fieldsets/checkbox_field.rb +29 -0
- data/app/models/dynamic_fieldsets/date_field.rb +37 -0
- data/app/models/dynamic_fieldsets/datetime_field.rb +36 -0
- data/app/models/dynamic_fieldsets/dependency.rb +1 -1
- data/app/models/dynamic_fieldsets/dependency_clause.rb +1 -1
- data/app/models/dynamic_fieldsets/dependency_group.rb +1 -1
- data/app/models/dynamic_fieldsets/field.rb +170 -42
- data/app/models/dynamic_fieldsets/field_default.rb +3 -3
- data/app/models/dynamic_fieldsets/field_html_attribute.rb +1 -1
- data/app/models/dynamic_fieldsets/field_option.rb +1 -1
- data/app/models/dynamic_fieldsets/field_record.rb +1 -1
- data/app/models/dynamic_fieldsets/fieldset.rb +36 -1
- data/app/models/dynamic_fieldsets/fieldset_associator.rb +9 -34
- data/app/models/dynamic_fieldsets/fieldset_child.rb +43 -24
- data/app/models/dynamic_fieldsets/instruction_field.rb +35 -0
- data/app/models/dynamic_fieldsets/multiple_select_field.rb +43 -0
- data/app/models/dynamic_fieldsets/radio_field.rb +27 -0
- data/app/models/dynamic_fieldsets/select_field.rb +16 -0
- data/app/models/dynamic_fieldsets/text_field.rb +14 -0
- data/app/models/dynamic_fieldsets/textarea_field.rb +40 -0
- data/app/views/dynamic_fieldsets/fields/_form.html.erb +50 -52
- data/app/views/dynamic_fieldsets/fields/edit.html.erb +5 -2
- data/app/views/dynamic_fieldsets/fields/index.html.erb +1 -1
- data/app/views/dynamic_fieldsets/fields/new.html.erb +3 -1
- data/app/views/dynamic_fieldsets/fields/show.html.erb +1 -1
- data/app/views/dynamic_fieldsets/fieldsets/_child.html.erb +1 -1
- data/app/views/dynamic_fieldsets/form_partials/_checkbox_field.html.erb +8 -0
- data/app/views/dynamic_fieldsets/form_partials/_date_field.html.erb +1 -0
- data/app/views/dynamic_fieldsets/form_partials/_datetime_field.html.erb +1 -0
- data/app/views/dynamic_fieldsets/form_partials/_input_footer.html.erb +1 -0
- data/app/views/dynamic_fieldsets/form_partials/_input_header.html.erb +7 -0
- data/app/views/dynamic_fieldsets/form_partials/_instruction_field.html.erb +1 -0
- data/app/views/dynamic_fieldsets/form_partials/_multiple_select_field.html.erb +1 -0
- data/app/views/dynamic_fieldsets/form_partials/_radio_field.html.erb +8 -0
- data/app/views/dynamic_fieldsets/form_partials/_select_field.html.erb +1 -0
- data/app/views/dynamic_fieldsets/form_partials/_text_field.html.erb +1 -0
- data/app/views/dynamic_fieldsets/form_partials/_textarea_field.html.erb +1 -0
- data/app/views/dynamic_fieldsets/shared/_javascript_watcher.html.erb +8 -8
- data/app/views/dynamic_fieldsets/show_partials/_show_incomplete.html.erb +1 -0
- data/app/views/dynamic_fieldsets/show_partials/_show_incomplete_footer.html.erb +2 -0
- data/app/views/dynamic_fieldsets/show_partials/_show_incomplete_header.html.erb +1 -0
- data/app/views/dynamic_fieldsets/show_partials/_show_instruction.html.erb +1 -0
- data/app/views/dynamic_fieldsets/show_partials/_show_multiple_answers.html.erb +13 -0
- data/app/views/dynamic_fieldsets/show_partials/_show_single_answer.html.erb +8 -0
- data/autotest/discover.rb +2 -0
- data/dynamic_fieldsets.gemspec +74 -20
- data/lib/dynamic_fieldsets/config.rb +45 -0
- data/lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb +38 -63
- data/lib/dynamic_fieldsets/field_with_field_options.rb +58 -0
- data/lib/dynamic_fieldsets/field_with_multiple_answers.rb +89 -0
- data/lib/dynamic_fieldsets/field_with_single_answer.rb +84 -0
- data/lib/dynamic_fieldsets/railtie.rb +6 -1
- data/lib/generators/dynamic_fieldsets/install_generator.rb +3 -3
- data/lib/generators/dynamic_fieldsets/templates/config.rb +15 -0
- data/lib/generators/dynamic_fieldsets/templates/migrations/install_migration.rb +1 -1
- data/spec/dummy/config/initializers/dynamic_fieldsets.rb +15 -0
- data/spec/dummy/db/migrate/20120213211033_create_dynamic_fieldsets_tables.rb +1 -1
- data/spec/dummy/db/schema.rb +1 -1
- data/spec/dummy/features/step_definitions/field_steps.rb +4 -4
- data/spec/dummy/features/step_definitions/fieldset_children_steps.rb +2 -2
- data/spec/dummy/features/step_definitions/javascript_steps.rb +7 -7
- data/spec/dynamic_fieldsets_helper_spec.rb +278 -312
- data/spec/dynamic_fieldsets_in_model_spec.rb +2 -2
- data/spec/field_with_field_options_spec.rb +49 -0
- data/spec/field_with_multiple_answers_spec.rb +50 -0
- data/spec/field_with_single_answer_spec.rb +51 -0
- data/spec/models/checkbox_field_spec.rb +19 -0
- data/spec/models/date_field_spec.rb +24 -0
- data/spec/models/datetime_field_spec.rb +24 -0
- data/spec/models/dependency_clause_spec.rb +7 -8
- data/spec/models/dependency_group_spec.rb +27 -30
- data/spec/models/dependency_spec.rb +8 -9
- data/spec/models/field_default_spec.rb +19 -14
- data/spec/models/field_html_attribute_spec.rb +3 -4
- data/spec/models/field_option_spec.rb +8 -9
- data/spec/models/field_record_spec.rb +8 -9
- data/spec/models/field_spec.rb +195 -94
- data/spec/models/fieldset_associator_spec.rb +39 -41
- data/spec/models/fieldset_child_spec.rb +99 -47
- data/spec/models/fieldset_spec.rb +25 -29
- data/spec/models/instruction_field_spec.rb +38 -0
- data/spec/models/multiple_select_field_spec.rb +31 -0
- data/spec/models/radio_field_spec.rb +21 -0
- data/spec/models/text_field_spec.rb +19 -0
- data/spec/models/textarea_field_spec.rb +39 -0
- data/spec/support/field_helper.rb +1 -1
- metadata +106 -28
@@ -0,0 +1,29 @@
|
|
1
|
+
module DynamicFieldsets
|
2
|
+
# Creates checkbox tags on a form
|
3
|
+
#
|
4
|
+
# Includes support for predefined field options and multiple selected answers
|
5
|
+
class CheckboxField < Field
|
6
|
+
acts_as_field_with_field_options
|
7
|
+
acts_as_field_with_multiple_answers
|
8
|
+
|
9
|
+
# Note that the checkbox needs individual data for each of the included options
|
10
|
+
# This means that some things, such as the name are duplicated for each field option
|
11
|
+
#
|
12
|
+
#
|
13
|
+
# @return [Hash] Data needed for the checkbox form partial
|
14
|
+
def form_partial_locals(args)
|
15
|
+
output = super
|
16
|
+
output[:options] = []
|
17
|
+
field_options.each do |option|
|
18
|
+
output[:options] << {
|
19
|
+
:name => "#{DynamicFieldsets.config.form_fieldset_associator_prefix}#{args[:fsa].id}[#{DynamicFieldsets.config.form_field_prefix}#{args[:fieldset_child].id}][]",
|
20
|
+
:value => option.id.to_s,
|
21
|
+
:checked => values_or_defaults_for_form(args[:values]).include?(option.id.to_s),
|
22
|
+
:label => option.name,
|
23
|
+
:html_attributes => html_attribute_hash
|
24
|
+
}
|
25
|
+
end
|
26
|
+
return output
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module DynamicFieldsets
|
2
|
+
# Creates date tags on a form
|
3
|
+
#
|
4
|
+
# The date tag currently defaults to the current date if nothing is set with the year range starting 70 years in the past
|
5
|
+
#
|
6
|
+
# The value of the input is stored as a string so costly Date.parse calls are used to parse the string
|
7
|
+
# This may have to be changed in the future
|
8
|
+
class DateField < Field
|
9
|
+
acts_as_field_with_single_answer
|
10
|
+
|
11
|
+
# Returns the current value if set, then the default value if set, then the current date as a Date object
|
12
|
+
#
|
13
|
+
# @return [Date] The current time stored in the date field
|
14
|
+
def get_date_or_today(value)
|
15
|
+
default_date = value_or_default_for_form(value)
|
16
|
+
if default_date.empty?
|
17
|
+
return Date.today
|
18
|
+
else
|
19
|
+
# not sure why Date.parse doesn't work here
|
20
|
+
return Time.parse(default_date)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Includes information used in the date_options argument (third) on the date select helper
|
25
|
+
#
|
26
|
+
# @return [Hash] Data for the date form partial
|
27
|
+
def form_partial_locals(args)
|
28
|
+
output = super
|
29
|
+
output[:date_options] = {
|
30
|
+
:start_year => Time.now.year - 70,
|
31
|
+
:default => get_date_or_today(args[:value])
|
32
|
+
}
|
33
|
+
return output
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module DynamicFieldsets
|
2
|
+
# Creates datetime tags on a form
|
3
|
+
#
|
4
|
+
# The datetime tag currently defaults to the current time if nothing is set with the year range starting 70 years in the past
|
5
|
+
#
|
6
|
+
# The value of the input is stored as a string so costly Time.parse calls are used to parse the string
|
7
|
+
# This may have to be changed in the future
|
8
|
+
class DatetimeField < Field
|
9
|
+
acts_as_field_with_single_answer
|
10
|
+
|
11
|
+
# Returns the current value if set, then the default value if set, then the current time as a Time object
|
12
|
+
#
|
13
|
+
# @return [Time] The current time stored in the datetime field
|
14
|
+
def get_datetime_or_today(value)
|
15
|
+
default_time = value_or_default_for_form(value)
|
16
|
+
if default_time.empty?
|
17
|
+
return Time.now
|
18
|
+
else
|
19
|
+
return Time.parse(default_time)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Includes information used in the date_options argument (third) on the date select helper
|
24
|
+
#
|
25
|
+
# @return [Hash] Data for the datetime form partial
|
26
|
+
def form_partial_locals(args)
|
27
|
+
output = super
|
28
|
+
output[:date_options] = {
|
29
|
+
:start_year => Time.now.year - 70,
|
30
|
+
:default => get_datetime_or_today(args[:value])
|
31
|
+
}
|
32
|
+
return output
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
@@ -4,7 +4,7 @@ module DynamicFieldsets
|
|
4
4
|
#
|
5
5
|
# @author John "hex" Carter
|
6
6
|
class Dependency < ActiveRecord::Base
|
7
|
-
|
7
|
+
self.table_name = "dynamic_fieldsets_dependencies"
|
8
8
|
|
9
9
|
RELATIONSHIP_LIST = ["equals","not equals","includes","not includes","blank","not blank"]
|
10
10
|
|
@@ -3,7 +3,7 @@ module DynamicFieldsets
|
|
3
3
|
#
|
4
4
|
# @author Jeremiah Hemphill
|
5
5
|
class DependencyClause < ActiveRecord::Base
|
6
|
-
|
6
|
+
self.table_name = "dynamic_fieldsets_dependency_clauses"
|
7
7
|
belongs_to :dependency_group
|
8
8
|
|
9
9
|
has_many :dependencies
|
@@ -3,8 +3,9 @@ module DynamicFieldsets
|
|
3
3
|
#
|
4
4
|
# @author Jeremiah Hemphill, Ethan Pemble
|
5
5
|
class Field < ActiveRecord::Base
|
6
|
-
|
7
|
-
|
6
|
+
self.table_name = "dynamic_fieldsets_fields"
|
7
|
+
|
8
|
+
# Associations
|
8
9
|
|
9
10
|
# parents
|
10
11
|
has_many :fieldset_children, :dependent => :destroy, :as => :child
|
@@ -25,67 +26,194 @@ module DynamicFieldsets
|
|
25
26
|
# Validations
|
26
27
|
validates_presence_of :name
|
27
28
|
validates_presence_of :label
|
28
|
-
validates_presence_of :
|
29
|
+
validates_presence_of :type
|
29
30
|
validates_inclusion_of :enabled, :in => [true, false]
|
30
31
|
validates_inclusion_of :required, :in => [true, false]
|
31
|
-
validate :has_field_options, :field_type_in_field_types
|
32
32
|
|
33
|
-
|
34
|
-
#
|
35
|
-
|
36
|
-
|
37
|
-
|
33
|
+
|
34
|
+
# Scopes and Static Methods
|
35
|
+
|
36
|
+
# Either calls the defaul descendants method or pulls the data from the config
|
37
|
+
# Deals with weird single table inheritance issues with cache classes off
|
38
|
+
# Causes errors only in development mode
|
39
|
+
#
|
40
|
+
# @return [Array<String>] An array of descendant class names
|
41
|
+
def self.descendants
|
42
|
+
if ::Rails.application.config.cache_classes
|
43
|
+
super
|
44
|
+
else
|
45
|
+
DynamicFieldsets.config.available_field_types
|
38
46
|
end
|
39
47
|
end
|
40
48
|
|
41
|
-
#
|
42
|
-
def
|
43
|
-
|
44
|
-
self.errors.add(:field_options, "This field must have options")
|
45
|
-
end
|
49
|
+
# @return [Array<String>] Humanized collection of descendants
|
50
|
+
def self.descendant_collection
|
51
|
+
descendants.collect { |d| [d.to_s.gsub("DynamicFieldsets::", "").underscore.humanize, d.to_s ] }
|
46
52
|
end
|
53
|
+
|
54
|
+
# Form partial Methods
|
47
55
|
|
48
|
-
# @
|
49
|
-
def
|
50
|
-
|
56
|
+
# @return [String] Name of partial to render for the form
|
57
|
+
def form_partial
|
58
|
+
"/dynamic_fieldsets/form_partials/" + self.class.to_s.gsub("DynamicFieldsets::", "").underscore
|
51
59
|
end
|
52
60
|
|
53
|
-
# @
|
54
|
-
def
|
55
|
-
|
61
|
+
# @return [String] Name of the input header for the form
|
62
|
+
def form_header_partial
|
63
|
+
"/dynamic_fieldsets/form_partials/input_header"
|
56
64
|
end
|
57
|
-
|
58
|
-
# @return [Boolean]
|
59
|
-
def
|
60
|
-
|
65
|
+
|
66
|
+
# @return [Boolean] By default, use the header partial
|
67
|
+
def use_form_header_partial?
|
68
|
+
true
|
61
69
|
end
|
62
|
-
|
63
|
-
# @return [
|
64
|
-
def
|
65
|
-
|
70
|
+
|
71
|
+
# @return [String] Name of the input footer for the form
|
72
|
+
def form_footer_partial
|
73
|
+
"/dynamic_fieldsets/form_partials/input_footer"
|
66
74
|
end
|
67
|
-
|
75
|
+
|
76
|
+
# @return [Boolean] By default, use the footer partial
|
77
|
+
def use_form_footer_partial?
|
78
|
+
true
|
79
|
+
end
|
80
|
+
|
81
|
+
# @return [Hash] Data needed for the form partial
|
82
|
+
def form_partial_locals(args)
|
83
|
+
output = {
|
84
|
+
:fsa => args[:fsa],
|
85
|
+
:fieldset_child => args[:fieldset_child],
|
86
|
+
:attrs => self.html_attribute_hash,
|
87
|
+
# for use in helpers like text_field and date_select
|
88
|
+
:object => "#{DynamicFieldsets::config.form_fieldset_associator_prefix}#{args[:fsa].id}",
|
89
|
+
:method => "#{DynamicFieldsets::config.form_field_prefix}#{args[:fieldset_child].id}",
|
90
|
+
}
|
91
|
+
# name for use in helpers like select_tag, check_box_tag, or anything ending with _tag
|
92
|
+
# this is more of a convenience method
|
93
|
+
output[:name] = "#{output[:object]}[#{output[:method]}]"
|
94
|
+
output[:id] = "#{output[:object]}_#{output[:method]}"
|
95
|
+
return output
|
96
|
+
end
|
97
|
+
|
98
|
+
# @return [Hash] A hash of html attribute key: value pairs
|
99
|
+
def html_attribute_hash
|
100
|
+
attrs = {}
|
101
|
+
field_html_attributes.each{ |a| attrs.merge! a.attribute_name.to_sym => a.value } if !field_html_attributes.empty?
|
102
|
+
return attrs
|
103
|
+
end
|
104
|
+
|
105
|
+
# Show partial Methods
|
106
|
+
|
107
|
+
# This method must be overriden
|
108
|
+
#
|
109
|
+
# @return [String] Name of partial to render for the show page
|
110
|
+
def show_partial
|
111
|
+
"/dynamic_fieldsets/show_partials/show_incomplete"
|
112
|
+
end
|
113
|
+
|
114
|
+
# This method must be overriden if show_header_partial returns true
|
115
|
+
#
|
116
|
+
# @return [String] Name of the input header for the show
|
117
|
+
def show_header_partial
|
118
|
+
"/dynamic_fieldsets/show_partials/show_incomplete_header"
|
119
|
+
end
|
120
|
+
|
121
|
+
# @return [Boolean] By default, do not use the header partial
|
122
|
+
def use_show_header_partial?
|
123
|
+
false
|
124
|
+
end
|
125
|
+
|
126
|
+
# This method must be overriden if show_footer_partial returns true
|
127
|
+
#
|
128
|
+
# @return [String] Name of the input footer for the show
|
129
|
+
def show_footer_partial
|
130
|
+
"/dynamic_fieldsets/show_partials/show_incomplete_footer"
|
131
|
+
end
|
132
|
+
|
133
|
+
# @return [Boolean] By default, do not use the footer partial
|
134
|
+
def use_show_footer_partial?
|
135
|
+
false
|
136
|
+
end
|
137
|
+
|
138
|
+
# Note that this method is really weird
|
139
|
+
# You would think that the value displayed should be figured out here
|
140
|
+
# but instead, it is figured out first, then passed in, in the arguments hash
|
141
|
+
#
|
142
|
+
# @return [Hash] Information needed for the show partial, don't know what I need yet
|
143
|
+
def show_partial_locals(args)
|
144
|
+
# these should be incredibly temporary
|
145
|
+
{
|
146
|
+
:value => args[:value],
|
147
|
+
:values => args[:values],
|
148
|
+
:label => self.label,
|
149
|
+
}
|
150
|
+
end
|
151
|
+
|
152
|
+
# given a value hash for a field, return the part that needs to be shown on the show page
|
153
|
+
def get_value_for_show(value)
|
154
|
+
value[:value]
|
155
|
+
end
|
156
|
+
|
157
|
+
# Other Methods
|
158
|
+
|
68
159
|
# @return [Boolean] False if field_default.value is empty
|
69
160
|
def has_defaults?
|
70
161
|
return self.field_defaults.length > 0
|
71
162
|
end
|
72
|
-
|
73
|
-
# @return [Array] Alias for field_defaults
|
74
|
-
def defaults
|
75
|
-
return self.field_defaults if options?
|
76
|
-
return nil
|
77
|
-
end
|
78
|
-
|
79
|
-
# @return [String] Alias for field_defaults.first
|
80
|
-
def default
|
81
|
-
return nil if options?
|
82
|
-
return self.field_defaults.first
|
83
|
-
end
|
84
163
|
|
85
164
|
# @return [Boolean] True if there are any field records for the field or if it is in any fieldsets
|
86
165
|
def in_use?
|
87
166
|
self.fieldset_children.count { |child| !child.fieldset_id.nil? || !child.field_records.empty? } > 0
|
88
167
|
end
|
89
|
-
|
168
|
+
|
169
|
+
# Fields such as selects, checkboxes, and radios use predefined field options for their values
|
170
|
+
# By default, a field does not use field options
|
171
|
+
#
|
172
|
+
# @return [Boolean] Whether the field uses field options, defaults to false
|
173
|
+
def uses_field_options?
|
174
|
+
false
|
175
|
+
end
|
176
|
+
|
177
|
+
# this collects defaults so that we can use them for fields
|
178
|
+
# note that this should be overridden when the field uses field options
|
179
|
+
# in that case, it should return field option ids instead of field option names
|
180
|
+
#
|
181
|
+
# I'm sorry
|
182
|
+
def collect_default_values
|
183
|
+
field_defaults.collect { |d| d[:value] }
|
184
|
+
end
|
185
|
+
|
186
|
+
# This method should be overridden by the field subclasses
|
187
|
+
#
|
188
|
+
# @param [DynamicFieldsets::FieldsetAssociator] fsa The associator
|
189
|
+
# @param [DynamicFieldsets::FieldsetChild] fsc The fieldset child
|
190
|
+
# @return [Nil] An empty result
|
191
|
+
def get_values_using_fsa_and_fsc(fsa, fsc)
|
192
|
+
return nil
|
193
|
+
end
|
194
|
+
|
195
|
+
# Collects the field records for the field so they can be used on the front end
|
196
|
+
# These are only the currently saved values in the database, don't worry
|
197
|
+
# about defaults here
|
198
|
+
#
|
199
|
+
# This works for fields that do not use field options
|
200
|
+
#
|
201
|
+
# @return [Array] An array of field record values
|
202
|
+
def collect_field_records_by_fsa_and_fsc(fsa, fsc)
|
203
|
+
# I think this needs to return some sort of hash
|
204
|
+
records = DynamicFieldsets::FieldRecord.where(:fieldset_associator_id => fsa.id, :fieldset_child_id => fsc.id)
|
205
|
+
return records.collect { |r| { :value => r.value } }
|
206
|
+
end
|
207
|
+
|
208
|
+
# Updates the field records for the field based on the given values
|
209
|
+
#
|
210
|
+
# This must be overridden if it is used
|
211
|
+
#
|
212
|
+
# @param [DynamicFieldsets::FieldsetAssociator] fsa The associator the value is attached to
|
213
|
+
# @param [DynamicFieldsets::FieldsetChild] fieldset_child The fieldset child for the value
|
214
|
+
# @param [Array or String] value The new values inputted by the user from the form
|
215
|
+
def update_field_records(fsa, fieldset_child, value)
|
216
|
+
throw "Field.update_field_records must be overridden to save data from the form."
|
217
|
+
end
|
90
218
|
end
|
91
219
|
end
|
@@ -5,7 +5,7 @@ module DynamicFieldsets
|
|
5
5
|
#
|
6
6
|
# @authors Scott Sampson, Jeremiah Hemphill, Ethan Pemble
|
7
7
|
class FieldDefault < ActiveRecord::Base
|
8
|
-
|
8
|
+
self.table_name = "dynamic_fieldsets_field_defaults"
|
9
9
|
#relations
|
10
10
|
belongs_to :field
|
11
11
|
|
@@ -21,7 +21,7 @@ module DynamicFieldsets
|
|
21
21
|
#
|
22
22
|
# http://www.youtube.com/watch?v=BeP6CpUnfc0
|
23
23
|
def convert_option_name_to_id
|
24
|
-
if
|
24
|
+
if field.uses_field_options?
|
25
25
|
option = FieldOption.find_by_name(self.value)
|
26
26
|
self.value = option.id unless option.nil?
|
27
27
|
end
|
@@ -29,7 +29,7 @@ module DynamicFieldsets
|
|
29
29
|
|
30
30
|
# @return [String] Either the value or the name of the field option reference by the value
|
31
31
|
def pretty_value
|
32
|
-
if !self.field.nil? &&
|
32
|
+
if !self.field.nil? && field.uses_field_options?
|
33
33
|
option = FieldOption.find_by_id(self.value)
|
34
34
|
if !option.nil?
|
35
35
|
return option.name
|
@@ -5,7 +5,7 @@ module DynamicFieldsets
|
|
5
5
|
#
|
6
6
|
# @authors Scott Sampson, Jeremiah Hemphill, Ethan Pemble
|
7
7
|
class FieldHtmlAttribute < ActiveRecord::Base
|
8
|
-
|
8
|
+
self.table_name = "dynamic_fieldsets_field_html_attributes"
|
9
9
|
#relations
|
10
10
|
belongs_to :field
|
11
11
|
|
@@ -5,7 +5,7 @@ module DynamicFieldsets
|
|
5
5
|
#
|
6
6
|
# @authors Scott Sampson, Jeremiah Hemphill, Ethan Pemble
|
7
7
|
class FieldOption < ActiveRecord::Base
|
8
|
-
|
8
|
+
self.table_name = "dynamic_fieldsets_field_options"
|
9
9
|
#relations
|
10
10
|
belongs_to :field
|
11
11
|
|
@@ -2,7 +2,7 @@ module DynamicFieldsets
|
|
2
2
|
# Stores a single record's answer to a field in a fieldset
|
3
3
|
# Fields with multiple answers should have multiple records in this model
|
4
4
|
class FieldRecord < ActiveRecord::Base
|
5
|
-
|
5
|
+
self.table_name = "dynamic_fieldsets_field_records"
|
6
6
|
|
7
7
|
belongs_to :fieldset_child
|
8
8
|
belongs_to :fieldset_associator
|
@@ -3,7 +3,7 @@ module DynamicFieldsets
|
|
3
3
|
#
|
4
4
|
# @author Jeremiah Hemphill, Ethan Pemble
|
5
5
|
class Fieldset < ActiveRecord::Base
|
6
|
-
|
6
|
+
self.table_name = "dynamic_fieldsets_fieldsets"
|
7
7
|
|
8
8
|
# Relations
|
9
9
|
has_many :fieldset_associators
|
@@ -43,6 +43,10 @@ module DynamicFieldsets
|
|
43
43
|
|
44
44
|
# The collected descendents of a fieldset.
|
45
45
|
# This group is sorted by order number on the fieldsetchild model
|
46
|
+
#
|
47
|
+
# IMPORTANT NOTE: At first glance, this looks like it should return a collection of fieldset children. It does not.
|
48
|
+
# This is a relic from the original design, and there is code working around the issue in several different places.
|
49
|
+
#
|
46
50
|
# @return [Array] Ordered collection of descendent fields and fieldsets.
|
47
51
|
def children
|
48
52
|
collected_children = []
|
@@ -60,5 +64,36 @@ module DynamicFieldsets
|
|
60
64
|
def has_children?
|
61
65
|
return !self.fieldset_children.empty?
|
62
66
|
end
|
67
|
+
|
68
|
+
# returns field record values for every field in the fsa
|
69
|
+
#
|
70
|
+
# note that this adds a hierarchy to the values
|
71
|
+
# not sure if this will require a rewrite somewhere else
|
72
|
+
#
|
73
|
+
# @param [DynamicFieldsets::FieldsetAssociator] The parent fsa
|
74
|
+
# @return [Hash] A hash of field record values using the fieldset child id as they key
|
75
|
+
def get_values_using_fsa(fsa)
|
76
|
+
output = {}
|
77
|
+
fieldset_children.each do |child|
|
78
|
+
output[child.id] = child.get_value_using_fsa(fsa)
|
79
|
+
end
|
80
|
+
return output
|
81
|
+
end
|
82
|
+
|
83
|
+
# Updates field options based on information from the form
|
84
|
+
#
|
85
|
+
# The form values should only be values for the current fsa
|
86
|
+
#
|
87
|
+
# @param [Hash] form_values Information from the form
|
88
|
+
def update_field_records_with_form_information(form_values)
|
89
|
+
form_values.each_pair do |fieldset_child_key, value|
|
90
|
+
if fieldset_child_key.start_with?(DynamicFieldsets.config.form_field_prefix)
|
91
|
+
fieldset_child_id = fieldset_child_key.gsub(/^#{DynamicFieldsets.config.form_field_prefix}/, "")
|
92
|
+
fieldset_child = fieldset_children.select { |child| child.id == fieldset_child_id.to_i }.first
|
93
|
+
# this could potentially hit a fieldset and cause problems
|
94
|
+
fieldset_child.child.update_field_records(self, fieldset_child, value)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
63
98
|
end
|
64
99
|
end
|