dynamic_fieldsets 0.1.16 → 0.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,5 +1,23 @@
1
1
  == unreleased changes
2
2
 
3
+ == 0.1.17
4
+ * Handling for nested fieldsets dependencies and for 'parallele' fieldset dependencies
5
+ * Wrote a js watcher for the show page, since previous solution is limited to a single fieldset.
6
+ * Fixed problems that came up for AND and OR dependencies during more complex testing.
7
+
8
+ == 0.1.16
9
+ * Added fieldset associator ids to fieldeset dependency hash so we can handle inter fieldset dependencies.
10
+
11
+ == 0.1.12
12
+ * Removed blank option from selects, it was causing problems on submission
13
+ changed the way 'checked' radios are read to jquery
14
+
15
+ == 0.1.11
16
+
17
+ * Fixed dependencies: AND, OR, multiple, and nested working,
18
+ -textboxes are cleared when hidden (still need to handle inputs and selects)
19
+ All values are now evaluated when any change, for radios only the checked one is read
20
+
3
21
  == 0.1.10
4
22
 
5
23
  * Fixed a namespacing issue in the nested model helper where it was overriding the helper of another project with the same method name.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.16
1
+ 0.1.17
@@ -32,6 +32,8 @@ module DynamicFieldsetsHelper
32
32
  end
33
33
 
34
34
  args = {
35
+ :fsa => fsa,
36
+ :fieldset_child => fieldset_child,
35
37
  :value => values,
36
38
  :values => values
37
39
  }
@@ -83,46 +85,6 @@ module DynamicFieldsetsHelper
83
85
  return field_markup
84
86
  end
85
87
 
86
- # Removes fieldset children that do not show in the edit form due to dependencies
87
- # @param [Fieldset] fieldset The Fieldset to render
88
- # @param [Hash] values Stored values for the fieldset
89
- # @return [Array <FieldsetChild>] children that should render in show page
90
- #
91
- def hide_children(fieldset, values)
92
- children = []
93
- fieldset.fieldset_children.each do |child_field|
94
- value = values[child_field.id]
95
- dependent_on = DynamicFieldsets::DependencyGroup.where(:fieldset_child_id => child_field.id).first
96
- if dependent_on.nil?
97
- children << child_field.child
98
- else
99
- dependencies = dependent_on.dependency_clauses.collect(&:dependencies).flatten.uniq
100
- dependencies.each do |dependency|
101
- if values[dependency.fieldset_child_id].present?
102
- dependent_on_type = dependency.fieldset_child.child.type
103
- dependent_on_values = values[dependency.fieldset_child_id]
104
- check_values = []
105
-
106
- if dependent_on_type == "DynamicFieldsets::CheckboxField" || dependent_on_type == "DynamicFieldsets::MultipleSelectField"
107
- dependent_on_values.each do |current_value|
108
- check_values << current_value[:name]
109
- end
110
- else
111
- check_values << (dependent_on_values.has_key?(:name) ? dependent_on_values[:name] : dependent_on_values[:value])
112
- end
113
-
114
- check_values.each do |check|
115
- if dependent_on.action == "show" && check == dependency.value
116
- children << child_field.child
117
- end
118
- end
119
- end
120
- end
121
- end
122
- end
123
- return children.uniq
124
- end
125
-
126
88
  # Builds HTML for the provided fieldset and its children.
127
89
  # @param [FieldsetAssociator] fsa parent FieldsetAssociator
128
90
  # @param [Fieldset] fieldset The Fieldset to render
@@ -132,8 +94,7 @@ module DynamicFieldsetsHelper
132
94
  lines = []
133
95
  lines.push render(:partial => "/dynamic_fieldsets/shared/fieldset_header", :locals => {:fieldset => fieldset})
134
96
 
135
- # still have to add children that are fieldsets.. have not tested far enough to get into it
136
- children = form_type == "show" ? hide_children(fieldset, values) : fieldset.children
97
+ children = fieldset.children
137
98
 
138
99
  # this returns field/fieldset objects rather than fieldset children
139
100
  # that is why this code looks like it is accessing odd objects
@@ -155,14 +116,14 @@ module DynamicFieldsetsHelper
155
116
  # @param [FieldsetAssociator] The fieldset associator for the dynamic fieldset to render
156
117
  # @return [String] The HTML for the entire dynamic fieldset
157
118
  def dynamic_fieldset_show_renderer(fsa)
158
- return dynamic_fieldset_renderer(fsa, "show")
119
+ return dynamic_fieldset_renderer(fsa, "show") << javascript_renderer("show",fsa)
159
120
  end
160
121
 
161
122
  # Build HTML for a specific dynamic fieldset on a form page
162
123
  # @param [FieldsetAssociator] The fieldset associator for the dynamic fieldset to render
163
124
  # @return [String] The HTML for the entire dynamic fieldset
164
125
  def dynamic_fieldset_form_renderer(fsa)
165
- return dynamic_fieldset_renderer(fsa, "form") << javascript_renderer(fsa)
126
+ return dynamic_fieldset_renderer(fsa, "form") << javascript_renderer("form",fsa)
166
127
  end
167
128
 
168
129
  # Builds HTML for a specific dynamic fieldset in a form.
@@ -184,15 +145,21 @@ module DynamicFieldsetsHelper
184
145
  #
185
146
  # @params [FieldsetAssociator] The fieldset associator for the dynamic fieldset to render
186
147
  # @return [String] The javascript variable that shows what fields have dependencies
187
- def javascript_renderer(fsa)
148
+ def javascript_renderer(form_type, fsa)
188
149
  unless fsa.id == nil
189
150
  rendered_javascript = "<script type='text/javascript'>
190
- if ( typeof dynamic_fieldsets_dependencies == 'undefined' ){
191
- var dynamic_fieldsets_dependencies = #{fsa.dependency_child_hash.to_json};
192
- } else {
193
- $.extend(dynamic_fieldsets_dependencies, #{fsa.dependency_child_hash.to_json});
194
- }</script>"
195
- rendered_javascript += render "dynamic_fieldsets/shared/javascript_watcher"
151
+ if ( typeof dynamic_fieldsets_dependencies == 'undefined' ){
152
+ var dynamic_fieldsets_dependencies = #{fsa.dependency_child_hash.to_json};
153
+ } else {
154
+ $.extend(dynamic_fieldsets_dependencies, #{fsa.dependency_child_hash.to_json});
155
+ }</script>"
156
+
157
+ if form_type == "form"
158
+ rendered_javascript += render "dynamic_fieldsets/shared/form_javascript_watcher"
159
+ elsif form_type == "show"
160
+ rendered_javascript += render "dynamic_fieldsets/shared/show_javascript_watcher"
161
+ end
162
+
196
163
  return rendered_javascript.html_safe
197
164
  else
198
165
  return ""
@@ -148,11 +148,15 @@ module DynamicFieldsets
148
148
  # @return [Hash] Information needed for the show partial, don't know what I need yet
149
149
  def show_partial_locals(args)
150
150
  # these should be incredibly temporary
151
- {
151
+ output = {
152
152
  :value => args[:value],
153
153
  :values => args[:values],
154
154
  :label => self.label,
155
+ :object => "#{DynamicFieldsets::config.form_fieldset_associator_prefix}#{args[:fsa].id}",
156
+ :method => "#{DynamicFieldsets::config.form_field_prefix}#{args[:fieldset_child].id}",
155
157
  }
158
+ output[:id] = "#{output[:object]}_#{output[:method]}"
159
+ return output
156
160
  end
157
161
 
158
162
  # given a value hash for a field, return the part that needs to be shown on the show page
@@ -91,7 +91,7 @@ module DynamicFieldsets
91
91
  fieldset_child_id = fieldset_child_key.gsub(/^#{DynamicFieldsets.config.form_field_prefix}/, "")
92
92
  fieldset_child = fieldset_children.select { |child| child.id == fieldset_child_id.to_i }.first
93
93
  # this could potentially hit a fieldset and cause problems
94
- fieldset_child.child.update_field_records(fsa, fieldset_child, value)
94
+ fieldset_child.child.update_field_records(fsa, fieldset_child, value) if fieldset_child.present?
95
95
  end
96
96
  end
97
97
  end
@@ -72,12 +72,12 @@ module DynamicFieldsets
72
72
  end
73
73
 
74
74
 
75
- # Converts the list of fields with dependencies generated by the look_for_dependents method into a hash
75
+ # Converts the list of fields with dependencies generated by the look_for_dependent_ons method into a hash
76
76
  # The hash contains the actual dependencies for the list of fields that have them
77
77
  #
78
78
  # @return [Hash] dependent fields for fields in the fieldset
79
79
  def dependency_child_hash
80
- fieldset_child_collection = look_for_dependents(self.fieldset)
80
+ fieldset_child_collection = look_for_dependent_ons(self.fieldset)
81
81
 
82
82
  output = {}
83
83
  fieldset_child_collection.each do |fieldset_child|
@@ -85,29 +85,46 @@ module DynamicFieldsets
85
85
  fieldset_child.dependencies.each do |dependency|
86
86
  dependency_group = dependency.dependency_clause.dependency_group
87
87
  output[fieldset_child.id][dependency_group.id] = dependency_group.to_hash
88
- output[fieldset_child.id][dependency_group.id][:fieldset_associators] = dependency_group.fieldset_child.fieldset.fieldset_associators.collect(&:id)
88
+ fieldset = dependency_group.fieldset_child.fieldset
89
+ if fieldset.fieldset_associators.collect(&:id).present?
90
+ output[fieldset_child.id][dependency_group.id][:fieldset_associators] = fieldset.fieldset_associators.collect(&:id)
91
+ else
92
+ output[fieldset_child.id][dependency_group.id][:fieldset_associators] = find_parent_fsa(fieldset).collect(&:id)
93
+ end
89
94
  end
90
95
  end
91
96
  return output
92
97
  end
93
-
94
98
 
95
- # Gets a list of fields in the given fieldset that have dependencies
99
+ # Gets finds 'ancestor' fieldset with fsa in case of nested fieldset when generating dependency hash
100
+ # we need an fsa in the dependencies hash (used for cross-fsa dependencies) AU 08-16-13
101
+ #
102
+ # @param [Fieldset] fieldset
103
+ # @return [Array <FieldsetAssociator>] fsa's associated to parent fieldset
104
+ #
105
+ def find_parent_fsa(fieldset)
106
+ if fieldset.fieldset_associators.present?
107
+ return fieldset.fieldset_associators
108
+ else
109
+ return find_parent_fsa(fieldset.parent)
110
+ end
111
+ end
112
+
113
+ # Gets a list of fields in the given Fieldset That Have Dependencies
96
114
  # I am not exactly sure why this method is necessary (JH 3-8-2012)
97
115
  # It seems like we could just make the dependency_child_hash method run recursively
98
116
  #
99
117
  # @param parent_fieldset [DynamicFieldsets::Fieldset] The fieldset to check inside of
100
118
  # @return [Array] The list of fields with dependencies
101
- def look_for_dependents(parent_fieldset)
119
+ def look_for_dependent_ons(parent_fieldset)
102
120
  output = []
103
121
  parent_fieldset.fieldset_children.each do |fieldset_child|
104
122
  if fieldset_child.child_type == "DynamicFieldsets::Field"
105
123
  if fieldset_child.dependencies.present?
106
124
  output.push fieldset_child
107
- # else then next
108
125
  end
109
126
  else # if child type is a fieldset, then recurse
110
- output += look_for_dependents(fieldset_child.child)
127
+ output += look_for_dependent_ons(fieldset_child.child)
111
128
  end
112
129
  end
113
130
  return output
@@ -98,7 +98,7 @@ module DynamicFieldsets
98
98
  # Scopes and Static Methods
99
99
 
100
100
  scope :ordered, order( 'order_num asc' )
101
-
101
+
102
102
  # Methods
103
103
 
104
104
  # @return [ActiveRecord::Relation] Collection of FieldsetChildren that are direct descendents; ascending order.
@@ -1,5 +1,5 @@
1
1
  <tr>
2
- <td><%= f.select :fieldset_child_id, options_for_select(DynamicFieldsets::FieldsetChild.all.select { |x| x.child.type != "DynamicFieldsets::InstructionField" }.collect { |c| [c.child.name, c.id]}, obj.fieldset_child_id)%></td>
2
+ <td><%= f.select :fieldset_child_id, options_for_select(DynamicFieldsets::FieldsetChild.select { |x| x.child_type == "DynamicFieldsets::Field" && x.child.type != "DynamicFieldsets::InstructionField" }.collect { |c| [c.child.name, c.id]}, obj.fieldset_child_id)%></td>
3
3
  <td><%= f.select :relationship, DynamicFieldsets::Dependency::RELATIONSHIP_LIST %></td>
4
4
  <td><%= f.text_field :value %></td>
5
5
  <td>
@@ -31,7 +31,7 @@
31
31
  <span class="name"><%= child.label %></span>
32
32
  <span class="admin">
33
33
  <span class="show"><%= link_to 'Show', dynamic_fieldsets_field_path(child) %></span>
34
- <span class="edit"><%= link_to "Edit Dependency", edit_dynamic_fieldsets_fieldset_child_path(child) %></span>
34
+ <span class="edit"><%= link_to "Edit Dependency", edit_dynamic_fieldsets_fieldset_child_path(fieldset_child) %></span>
35
35
  <span class="edit"><%= link_to 'Edit Field', edit_dynamic_fieldsets_field_path(child) %></span>
36
36
  <span class="destroy"><%= button_to 'Remove', dynamic_fieldsets_remove_fieldset_child_path(fieldset_child), :method => :post %></span>
37
37
  </span>
@@ -1,7 +1,7 @@
1
1
  <script type='text/javascript'>
2
2
 
3
3
  // The only inputs we care about are visible... not hidden
4
- var all_inputs = $(":input:not(:hidden)[id^=fsa]").add("p[id^='fsa']");
4
+ var all_inputs = $(":input:not(:hidden)[id^=fsa]").add("p[id^='fsa']").add("textarea[id^='fsa']");
5
5
 
6
6
  //Return the fieldset child id for the input
7
7
  //This uniquely associated the fieldset child with the fieldset associator
@@ -61,7 +61,7 @@ function get_input_value(field, type) {
61
61
  }
62
62
  return output;
63
63
  case 'checkbox':
64
- return $('input[name="' + field.attr('name') + '"]:checked').map(function(index,option) {
64
+ return field.parent().parent().find(':checked').map(function(index,option) {
65
65
  return $.trim($(option).parent('label').text())
66
66
  });
67
67
  case 'select':
@@ -110,7 +110,7 @@ all_inputs.change( function() {
110
110
  var all_fieldset_child_id = get_fieldset_child_id($(input), all_type)
111
111
  user_inputs[all_fieldset_child_id] = get_input_value($(input), all_type);
112
112
  });
113
-
113
+
114
114
  if (fieldset_child_id in dynamic_fieldsets_dependencies) {
115
115
  $.each(dynamic_fieldsets_dependencies[fieldset_child_id], function(index, group) {
116
116
  fieldset_associator_ids = dynamic_fieldsets_dependencies[fieldset_child_id][index]["fieldset_associators"];
@@ -139,7 +139,7 @@ function update_dependency_group_for_fieldset_child(fieldset_child_id, group, us
139
139
  function all_dependency_clauses_true(fieldset_child_id, group, user_inputs) {
140
140
  for(var key in group["clause"]) {
141
141
  var clause = group["clause"][key]
142
- if(!at_least_one_dependency_true(fieldset_child_id, clause, user_inputs)) {
142
+ if(!at_least_one_dependency_true(clause, user_inputs)) {
143
143
  return false;
144
144
  }
145
145
  }
@@ -151,16 +151,13 @@ function all_dependency_clauses_true(fieldset_child_id, group, user_inputs) {
151
151
  //Only return false if all are false
152
152
  //It looks to see what dependencies inside the clause should actually be tested (AU 08-07-13)
153
153
  // If dependency doesn't match current field, it looks up the correct stored input to compare
154
- function at_least_one_dependency_true(fieldset_child_id, clause, user_inputs) {
154
+ function at_least_one_dependency_true(clause, user_inputs) {
155
155
  for(var key in clause) {
156
156
  var dependency = clause[key]
157
+ var fieldset_child_id = dependency["fieldset_child_id"]
157
158
  if( evaluate_dependency(user_inputs[fieldset_child_id], dependency["relationship"], dependency["value"]) ){
158
159
  return true
159
- } else if( fieldset_child_id != dependency['fieldset_child_id'] ) {
160
- if( evaluate_dependency(user_inputs[dependency['fieldset_child_id']], dependency['relationship'], dependency['value']) ){
161
- return true
162
- }
163
- }
160
+ }
164
161
  }
165
162
  return false
166
163
  }
@@ -202,7 +199,8 @@ function evaluate_dependency(user_value, relationship, stored_value) {
202
199
  // group_field: the field to update (generally not the field that triggered the change)
203
200
  function dependency_action(success_flag, action, group_fields, counter) {
204
201
  $.each(group_fields, function(index, group_field){
205
- if (success_flag){
202
+ if( $('[id$='+group_field+']').size() > 0 ) {
203
+ if (success_flag){
206
204
  switch(action)
207
205
  {
208
206
  case 'show':
@@ -210,65 +208,63 @@ function dependency_action(success_flag, action, group_fields, counter) {
210
208
  case 'enable':
211
209
  $('#' + group_field + ' :input').removeAttr('disabled');
212
210
  }
213
-
214
- } else {
215
- switch(action)
216
- {
217
- case 'show':
218
- $('[id$=' + group_field +']').hide();
219
- case 'enable':
220
- $('#' + group_field + ' :input').attr('disabled', true);
221
- }
222
211
 
223
- clear_unused_fields(group_field)
212
+ } else {
213
+ switch(action)
214
+ {
215
+ case 'show':
216
+ $('[id$=' + group_field +']').hide();
217
+ case 'enable':
218
+ $('#' + group_field + ' :input').attr('disabled', true);
219
+ }
220
+ clear_unused_field(group_field)
224
221
 
225
- //save from infinite recursion, only goes as deep as there are dependency groups
226
- if( counter < Object.keys(dynamic_fieldsets_dependencies).length ) {
227
- nested_dependencies(success_flag, action, group_field, counter)
228
- }
222
+ nested_dependencies(success_flag, action, group_field)
223
+ }
229
224
  }
230
225
  });
231
226
  };
232
227
 
233
- function clear_unused_fields(group_field) {
234
- field = $('[id$='+group_field+']').children(':input');
235
- type = get_field_type(field);
236
- switch(type)
237
- {
238
- case 'text':
239
- case 'textarea':
240
- field.val("");
241
- case 'radio':
242
- if (field.attr('checked') == 'checked') {
243
- }
244
- case 'checkbox':
245
- //$('input[name="' + field.attr('name') + '"]:checked').map(function(index,option) {
246
- //});
247
- case 'select':
248
- //field.find(':selected').text();
249
- case 'multi-select':
250
- //return field.find(':selected').map(function(index, option) {
251
- //});
252
- }
228
+ //clears fields that are being hidden
229
+ function clear_unused_field(group_field) {
230
+ fields = $('[id$='+group_field+'] :input')
231
+ fields.each( function(index, field) {
232
+ field = $(field)
233
+ type = get_field_type(field);
234
+ switch(type)
235
+ {
236
+ case 'text':
237
+ case 'textarea':
238
+ field.val("");
239
+ case 'radio':
240
+ case 'checkbox':
241
+ if (field.attr('checked') == 'checked') {
242
+ field.removeAttr('checked')
243
+ }
244
+ case 'select':
245
+ case 'multi-select':
246
+ }
247
+ });
253
248
  };
254
249
 
255
250
  //looks through dependency hash to find dependent fields that may need to be hidden (AU 08-07-13)
256
- function nested_dependencies(success_flag, action, group_field, counter) {
251
+ function nested_dependencies(success_flag, action, group_field) {
257
252
  var dependent_on = group_field.split("-").pop();
258
253
  for( var key in dynamic_fieldsets_dependencies ) {
259
254
  var child = dynamic_fieldsets_dependencies[key]
260
255
  for( var keyg in child ) {
261
- //ignore own group since dependents won't be here
262
- if( keyg != dependent_on ) {
263
- var group_search = child[keyg]
264
- for( var keyc in group_search["clause"] ) {
265
- var clause = group_search["clause"][keyc]
266
- for( var keyd in clause ) {
267
- var dependency = clause[keyd]
268
- if ( dependency["fieldset_child_id"] == dependent_on ) {
269
- var dependent_id = group_field.replace(dependent_on, group_search["fieldset_child_id"])
270
- dependency_action(success_flag, action, dependent_id, counter+1)
271
- }
256
+ var group_search = child[keyg]
257
+ for( var keyc in group_search["clause"] ) {
258
+ var clause = group_search["clause"][keyc]
259
+ for( var keyd in clause ) {
260
+ var dependency = clause[keyd]
261
+ //select dependent by comparing who the dependency belongs to
262
+ if ( dependency["fieldset_child_id"] == dependent_on ) {
263
+ var fsa = group_field.split("_")[0]
264
+ var field = group_field.split("_")[1]
265
+ var dependent_id = field.replace(dependent_on, group_search["fieldset_child_id"])
266
+ var full_dep_id = fsa+"_"+dependent_id
267
+ dependency_action(success_flag, action, [full_dep_id])
272
268
  }
273
269
  }
274
270
  }
@@ -0,0 +1,138 @@
1
+ <script type='text/javascript'>
2
+
3
+ var all_divs = $("div :not(:hidden)[id^=fsa]").add("p[id^='fsa']");
4
+
5
+ function hide_fields(){
6
+ var values = {}
7
+ $.each(all_divs, function(index, div){
8
+ var div_id = $(div).attr('id');
9
+ var fieldset_child_id = div_id.substring(div_id.length-1);
10
+ var fsa_id = div_id.split("_")[0].split("-")[1]
11
+
12
+ if($(div).is("p")){
13
+ values[fieldset_child_id] = div.innerHTML
14
+ } else {
15
+ if($(div).attr('type') == 'single'){
16
+ values[fieldset_child_id] = $(div).find('span').find('span').text()
17
+ } else {
18
+ values[fieldset_child_id] = $(div).find('li').map(function(index, li){
19
+ return $(div).find('span').find('span').text()
20
+ });
21
+ }
22
+ }
23
+
24
+ var dependency_group = dynamic_fieldsets_dependencies[fieldset_child_id];
25
+ if((typeof dependency_group) != "undefined"){
26
+ $.each(dependency_group, function(index, group){
27
+ var fieldset_associator_ids = group['fieldset_associators'];
28
+ evaluate_dependencies(fieldset_child_id, group, values, fieldset_associator_ids)
29
+ })
30
+ }
31
+ });
32
+ }
33
+
34
+ function evaluate_dependencies(fieldset_child_id, group, values, fieldset_associator_ids) {
35
+ var action = group['action'];
36
+ var group_fsc_id = group['fieldset_child_id'];
37
+ var group_field_id = group['field_id'];
38
+ var group_fields = []
39
+ $.each(fieldset_associator_ids, function(index, fsa_id) {
40
+ group_fields[index] = '<%= DynamicFieldsets.config.form_fieldset_associator_prefix %>' + fsa_id + '_' + '<%=DynamicFieldsets.config.form_field_prefix %>' + group_fsc_id;
41
+ });
42
+ dependency_action(all_dependency_clauses_true(fieldset_child_id, group, values), action, group_fields, 0 )
43
+ }
44
+
45
+ function all_dependency_clauses_true(fieldset_child_id, group, values) {
46
+ for(var key in group["clause"]) {
47
+ var clause = group["clause"][key]
48
+ if(!at_least_one_dependency_true(clause, values)) {
49
+ return false;
50
+ }
51
+ }
52
+ return true;
53
+ }
54
+
55
+ function at_least_one_dependency_true(clause, values) {
56
+ for(var key in clause) {
57
+ var dependency = clause[key]
58
+ var fieldset_child_id = dependency["fieldset_child_id"]
59
+ if( evaluate_dependency(values[fieldset_child_id], dependency["relationship"], dependency["value"]) ){
60
+ return true
61
+ }
62
+ }
63
+ return false
64
+ }
65
+
66
+ function evaluate_dependency(user_value, relationship, stored_value) {
67
+ switch(relationship)
68
+ {
69
+ case 'equals':
70
+ return user_value == stored_value;
71
+ case 'not equals':
72
+ return user_value != stored_value;
73
+ case 'includes':
74
+ return $.inArray(stored_value, user_value) != -1;
75
+ case 'not includes':
76
+ return $.inArray(stored_value, user_value) == -1;
77
+ case 'blank':
78
+ return user_value == "";
79
+ case 'not blank':
80
+ return user_value != "";
81
+ default:
82
+ return false;
83
+ }
84
+ };
85
+
86
+ function dependency_action(success_flag, action, group_fields, counter) {
87
+ $.each(group_fields, function(index, group_field){
88
+ if (success_flag){
89
+ switch(action)
90
+ {
91
+ case 'show':
92
+ $('[id$=' + group_field +']').show();
93
+ case 'enable':
94
+ $('#' + group_field + ' :input').removeAttr('disabled');
95
+ }
96
+
97
+ } else {
98
+ switch(action)
99
+ {
100
+ case 'show':
101
+ $('[id$=' + group_field +']').hide();
102
+ case 'enable':
103
+ $('#' + group_field + ' :input').attr('disabled', true);
104
+ }
105
+
106
+ //save from infinite recursion, only goes as deep as there are dependency groups
107
+ if( counter < Object.keys(dynamic_fieldsets_dependencies).length ) {
108
+ nested_dependencies(success_flag, action, group_field, counter)
109
+ }
110
+ }
111
+ });
112
+ };
113
+
114
+ function nested_dependencies(success_flag, action, group_field, counter) {
115
+ var dependent_on = group_field.split("-").pop();
116
+ for( var key in dynamic_fieldsets_dependencies ) {
117
+ var child = dynamic_fieldsets_dependencies[key]
118
+ for( var keyg in child ) {
119
+ //ignore own group since dependents won't be here
120
+ if( keyg != dependent_on ) {
121
+ var group_search = child[keyg]
122
+ for( var keyc in group_search["clause"] ) {
123
+ var clause = group_search["clause"][keyc]
124
+ for( var keyd in clause ) {
125
+ var dependency = clause[keyd]
126
+ if ( dependency["fieldset_child_id"] == dependent_on ) {
127
+ var dependent_id = group_field.replace(dependent_on, group_search["fieldset_child_id"])
128
+ dependency_action(success_flag, action, [dependent_id], counter+1)
129
+ }
130
+ }
131
+ }
132
+ }
133
+ }
134
+ }
135
+ };
136
+
137
+ hide_fields();
138
+ </script>
@@ -1 +1 @@
1
- <p><%= label %></p>
1
+ <p id='<%= id %>'><%= label %></p>
@@ -1,4 +1,4 @@
1
- <div class='dynamic_fieldsets field'>
1
+ <div id='<%= id %>' class='dynamic_fieldsets field' type='multiple'>
2
2
  <span class='label'><%= label %></label>
3
3
  <% if values.nil? || values.empty? %>
4
4
  <em class='empty'>No answer given</em>
@@ -1,4 +1,4 @@
1
- <div class='dynamic_fieldsets field'>
1
+ <div id='<%= id %>' class='dynamic_fieldsets field' type='single'>
2
2
  <span class='label'><%= label %></label>
3
3
  <% if value.nil? || value.empty? %>
4
4
  <em class='empty'>No answer given</em>
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "dynamic_fieldsets"
8
- s.version = "0.1.16"
8
+ s.version = "0.1.17"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jeremiah Hemphill", "Ethan Pemble", "John Carter"]
12
- s.date = "2013-08-15"
12
+ s.date = "2013-09-10"
13
13
  s.description = "Dynamic fieldsets for rails controllers"
14
14
  s.email = "jeremiah@cloudspace.com"
15
15
  s.extra_rdoc_files = [
@@ -92,8 +92,9 @@ Gem::Specification.new do |s|
92
92
  "app/views/dynamic_fieldsets/form_partials/_textarea_field.html.erb",
93
93
  "app/views/dynamic_fieldsets/shared/_fieldset_footer.html.erb",
94
94
  "app/views/dynamic_fieldsets/shared/_fieldset_header.html.erb",
95
- "app/views/dynamic_fieldsets/shared/_javascript_watcher.html.erb",
95
+ "app/views/dynamic_fieldsets/shared/_form_javascript_watcher.html.erb",
96
96
  "app/views/dynamic_fieldsets/shared/_nested_model_javascript.html.erb",
97
+ "app/views/dynamic_fieldsets/shared/_show_javascript_watcher.html.erb",
97
98
  "app/views/dynamic_fieldsets/show_partials/_show_incomplete.html.erb",
98
99
  "app/views/dynamic_fieldsets/show_partials/_show_incomplete_footer.html.erb",
99
100
  "app/views/dynamic_fieldsets/show_partials/_show_incomplete_header.html.erb",
@@ -1,5 +1,6 @@
1
1
  class InformationForm < ActiveRecord::Base
2
2
  # important note: for this to work, there needs to have a fieldset with an nkey of "first" in the db
3
3
  # so that DynamicFieldsets::Fieldset.find_by_nkey("first") returns a fieldset
4
- acts_as_dynamic_fieldset :child_form => {:fieldset => :first}
4
+ acts_as_dynamic_fieldset :child_form => {:fieldset => :first, :initialize_on_create => true}
5
+ acts_as_dynamic_fieldset :child_form2 => {:fieldset => :second, :initialize_on_create => true}
5
6
  end
@@ -16,6 +16,7 @@
16
16
  <%= f.text_field :name %>
17
17
  </div>
18
18
  <%= dynamic_fieldset_form_renderer(@information_form.child_form) %>
19
+ <%= dynamic_fieldset_form_renderer(@information_form.child_form2) %>
19
20
  <div class="actions">
20
21
  <%= f.submit %>
21
22
  </div>
@@ -7,6 +7,7 @@
7
7
 
8
8
 
9
9
  <%= dynamic_fieldset_show_renderer(@information_form.child_form) %>
10
+ <%= dynamic_fieldset_show_renderer(@information_form.child_form2) %>
10
11
 
11
12
  <%= link_to 'Edit', edit_information_form_path(@information_form) %> |
12
13
  <%= link_to 'Back', information_forms_path %>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamic_fieldsets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.16
4
+ version: 0.1.17
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-08-15 00:00:00.000000000 Z
14
+ date: 2013-09-10 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
@@ -384,8 +384,9 @@ files:
384
384
  - app/views/dynamic_fieldsets/form_partials/_textarea_field.html.erb
385
385
  - app/views/dynamic_fieldsets/shared/_fieldset_footer.html.erb
386
386
  - app/views/dynamic_fieldsets/shared/_fieldset_header.html.erb
387
- - app/views/dynamic_fieldsets/shared/_javascript_watcher.html.erb
387
+ - app/views/dynamic_fieldsets/shared/_form_javascript_watcher.html.erb
388
388
  - app/views/dynamic_fieldsets/shared/_nested_model_javascript.html.erb
389
+ - app/views/dynamic_fieldsets/shared/_show_javascript_watcher.html.erb
389
390
  - app/views/dynamic_fieldsets/show_partials/_show_incomplete.html.erb
390
391
  - app/views/dynamic_fieldsets/show_partials/_show_incomplete_footer.html.erb
391
392
  - app/views/dynamic_fieldsets/show_partials/_show_incomplete_header.html.erb
@@ -520,7 +521,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
520
521
  version: '0'
521
522
  segments:
522
523
  - 0
523
- hash: 433715359672350075
524
+ hash: -1940297788818609247
524
525
  required_rubygems_version: !ruby/object:Gem::Requirement
525
526
  none: false
526
527
  requirements: