dynamic_fieldsets 0.0.3 → 0.0.4

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.
Files changed (89) hide show
  1. data/CHANGELOG +7 -0
  2. data/Gemfile +1 -0
  3. data/Gemfile.lock +1 -0
  4. data/README.rdoc +15 -2
  5. data/Rakefile +24 -4
  6. data/VERSION +1 -1
  7. data/app/controllers/dynamic_fieldsets/fields_controller.rb +25 -0
  8. data/app/controllers/dynamic_fieldsets/fieldset_children_controller.rb +57 -0
  9. data/app/controllers/dynamic_fieldsets/fieldsets_controller.rb +91 -5
  10. data/app/helpers/dynamic_fieldsets/fields_helper.rb +1 -15
  11. data/app/helpers/dynamic_fieldsets/fieldset_children_helper.rb +5 -0
  12. data/app/helpers/dynamic_fieldsets/nested_model_helper.rb +18 -0
  13. data/app/helpers/dynamic_fieldsets_helper.rb +70 -44
  14. data/app/models/dynamic_fieldsets/dependency.rb +76 -0
  15. data/app/models/dynamic_fieldsets/dependency_clause.rb +32 -0
  16. data/app/models/dynamic_fieldsets/dependency_group.rb +108 -0
  17. data/app/models/dynamic_fieldsets/field.rb +17 -12
  18. data/app/models/dynamic_fieldsets/field_default.rb +27 -1
  19. data/app/models/dynamic_fieldsets/field_record.rb +19 -4
  20. data/app/models/dynamic_fieldsets/fieldset.rb +35 -30
  21. data/app/models/dynamic_fieldsets/fieldset_associator.rb +52 -8
  22. data/app/models/dynamic_fieldsets/fieldset_child.rb +148 -0
  23. data/app/views/dynamic_fieldsets/fields/_disable_field_form.html.erb +4 -0
  24. data/app/views/dynamic_fieldsets/fields/_field_default_fields.html.erb +1 -1
  25. data/app/views/dynamic_fieldsets/fields/_form.html.erb +9 -27
  26. data/app/views/dynamic_fieldsets/fields/index.html.erb +8 -5
  27. data/app/views/dynamic_fieldsets/fields/new.html.erb +5 -1
  28. data/app/views/dynamic_fieldsets/fields/show.html.erb +1 -13
  29. data/app/views/dynamic_fieldsets/fieldset_children/_dependency_clause_fields.html.erb +25 -0
  30. data/app/views/dynamic_fieldsets/fieldset_children/_dependency_fields.html.erb +12 -0
  31. data/app/views/dynamic_fieldsets/fieldset_children/_dependency_group_fields.html.erb +16 -0
  32. data/app/views/dynamic_fieldsets/fieldset_children/_form.html.erb +34 -0
  33. data/app/views/dynamic_fieldsets/fieldset_children/edit.html.erb +6 -0
  34. data/app/views/dynamic_fieldsets/fieldsets/_associate_child.html.erb +4 -0
  35. data/app/views/dynamic_fieldsets/fieldsets/_child.html.erb +41 -0
  36. data/app/views/dynamic_fieldsets/fieldsets/_form.html.erb +2 -8
  37. data/app/views/dynamic_fieldsets/fieldsets/children.html.erb +47 -39
  38. data/app/views/dynamic_fieldsets/fieldsets/index.html.erb +4 -4
  39. data/app/views/dynamic_fieldsets/fieldsets/new.html.erb +5 -1
  40. data/app/views/dynamic_fieldsets/fieldsets/reorder.html.erb +4 -0
  41. data/app/views/dynamic_fieldsets/fieldsets/show.html.erb +1 -12
  42. data/app/views/dynamic_fieldsets/shared/_javascript_watcher.html.erb +255 -0
  43. data/app/views/dynamic_fieldsets/shared/_nested_model_javascript.html.erb +35 -0
  44. data/config/.routes.rb.swp +0 -0
  45. data/config/routes.rb +11 -0
  46. data/dynamic_fieldsets.gemspec +41 -4
  47. data/lib/dynamic_fieldsets/dynamic_fieldsets_in_model.rb +141 -14
  48. data/lib/generators/dynamic_fieldsets/templates/migrations/install_migration.rb +39 -5
  49. data/spec/dummy/app/controllers/information_forms_controller.rb +2 -1
  50. data/spec/dummy/app/models/information_form.rb +1 -1
  51. data/spec/dummy/app/views/information_forms/dynamic_view.html.erb +18 -0
  52. data/spec/dummy/app/views/information_forms/show.html.erb +1 -0
  53. data/spec/dummy/app/views/layouts/application.html.erb +9 -1
  54. data/spec/dummy/config/routes.rb +1 -1
  55. data/spec/dummy/db/migrate/{20110726215814_create_dynamic_fieldsets_tables.rb → 20110809161724_create_dynamic_fieldsets_tables.rb} +39 -5
  56. data/spec/dummy/db/schema.rb +40 -11
  57. data/spec/dummy/features/field.feature +17 -2
  58. data/spec/dummy/features/fieldset.feature +1 -21
  59. data/spec/dummy/features/fieldset_children.feature +50 -0
  60. data/spec/dummy/features/javascript_tests.feature +91 -0
  61. data/spec/dummy/features/step_definitions/field_steps.rb +9 -5
  62. data/spec/dummy/features/step_definitions/fieldset_associator_steps.rb +3 -1
  63. data/spec/dummy/features/step_definitions/fieldset_children_steps.rb +65 -0
  64. data/spec/dummy/features/step_definitions/fieldset_steps.rb +0 -27
  65. data/spec/dummy/features/step_definitions/javascript_steps.rb +208 -0
  66. data/spec/dummy/features/step_definitions/web_steps.rb +5 -0
  67. data/spec/dummy/features/support/paths.rb +10 -1
  68. data/spec/dummy/features/support/selectors.rb +2 -0
  69. data/spec/dummy/public/javascripts/jquery-1.6.2.min.js +18 -0
  70. data/spec/dummy/public/javascripts/jquery-ui-1.8.15.custom.min.js +111 -0
  71. data/spec/dummy/public/javascripts/jquery-ui-nestedSortable.js +356 -0
  72. data/spec/dummy/public/stylesheets/scaffold.css +101 -0
  73. data/spec/dynamic_fieldsets_helper_spec.rb +236 -55
  74. data/spec/dynamic_fieldsets_in_model_spec.rb +122 -4
  75. data/spec/models/dependency_clause_spec.rb +55 -0
  76. data/spec/models/dependency_group_spec.rb +237 -0
  77. data/spec/models/dependency_spec.rb +173 -0
  78. data/spec/models/field_default_spec.rb +49 -0
  79. data/spec/models/field_record_spec.rb +11 -2
  80. data/spec/models/field_spec.rb +30 -6
  81. data/spec/models/fieldset_associator_spec.rb +138 -25
  82. data/spec/models/fieldset_child_spec.rb +122 -0
  83. data/spec/models/fieldset_spec.rb +78 -80
  84. data/spec/support/dependency_group_helper.rb +9 -0
  85. data/spec/support/dependency_helper.rb +13 -0
  86. data/spec/support/field_helper.rb +0 -2
  87. data/spec/support/fieldset_child_helper.rb +10 -0
  88. data/spec/support/fieldset_helper.rb +2 -18
  89. metadata +51 -5
@@ -77,56 +77,169 @@ describe FieldsetAssociator do
77
77
  before(:each) do
78
78
  @fsa = FieldsetAssociator.new
79
79
 
80
- @field = mock_model(DynamicFieldsets::Field)
81
- @field.stub!(:id).and_return(37)
80
+ @field = mock_model DynamicFieldsets::Field
81
+ @fieldset_child = mock_model DynamicFieldsets::FieldsetChild
82
+ @fieldset_child.stub!(:child).and_return @field
83
+ @fieldset_child.stub!(:id).and_return 37
82
84
 
83
- @field_record = mock_model(DynamicFieldsets::FieldRecord)
84
- @field_record.stub!(:field).and_return(@field)
85
- @field_record.stub!(:value).and_return("forty two")
86
- @field_record.stub!(:id).and_return(42)
85
+ @field_record = mock_model DynamicFieldsets::FieldRecord
86
+ @field_record.stub!(:fieldset_child).and_return @fieldset_child
87
+ @field_record.stub!(:value).and_return 42
87
88
 
88
- @fsa.stub!(:field_records).and_return([@field_record, @field_record])
89
+ @fsa.stub!(:field_records).and_return [@field_record, @field_record]
89
90
  end
90
91
 
91
- it "should return a hash" do
92
- @field.stub!(:field_type).and_return("")
92
+ it "returns a hash" do
93
+ @fieldset_child.child.stub!(:field_type).and_return ''
93
94
  @fsa.field_values.should be_a_kind_of Hash
94
95
  end
95
96
 
96
- it "should call field_records" do
97
- @fsa.should_receive(:field_records).and_return([])
97
+ it "calls field_records" do
98
+ @fsa.should_receive(:field_records).and_return []
98
99
  @fsa.field_values
99
100
  end
100
101
 
101
102
  # I am aware these two tests aren't really realistic because ids should be different
102
103
  # Results should be consistent with these when ids are different
103
- it "should return multiple select values as an array of ids" do
104
- pending "Pending for 0.0.3 release"
105
- @field.stub!(:field_type).and_return("multiple_select")
104
+ it "returns multiple select values as an array of ids" do
105
+ @fieldset_child.child.stub!(:field_type).and_return 'multiple_select'
106
106
  @fsa.field_values.should == { 37 => [42, 42] }
107
107
  end
108
108
 
109
- it "should return checkboxes values as an array of ids" do
110
- pending "Pending for 0.0.3 release"
111
- @field.stub!(:field_type).and_return("checkbox")
109
+ it "returns checkboxes values as an array of ids" do
110
+ @field.stub!(:field_type).and_return 'checkbox'
112
111
  @fsa.field_values.should == { 37 => [42, 42] }
113
112
  end
114
113
 
115
- it "should return select values as an id" do
116
- pending "Pending for 0.0.3 release"
117
- @field.stub!(:field_type).and_return("select")
114
+ it "returns select values as an id" do
115
+ @field.stub!(:field_type).and_return 'select'
118
116
  @fsa.field_values.should == { 37 => 42 }
119
117
  end
120
118
 
121
- it "should return radio values as an id" do
122
- pending "Pending for 0.0.3 release"
123
- @field.stub!(:field_type).and_return("radio")
119
+ it "returns radio values as an id" do
120
+ @field.stub!(:field_type).and_return 'radio'
124
121
  @fsa.field_values.should == { 37 => 42 }
125
122
  end
126
123
 
127
- it "should return all other field types as strings" do
128
- @field.stub!(:field_type).and_return("text")
124
+ it "returns all other field types as strings" do
125
+ @field.stub!(:field_type).and_return 'textfield'
126
+ @field_record.stub!(:value).and_return 'forty two'
129
127
  @fsa.field_values.should == { 37 => "forty two" }
130
128
  end
131
129
  end
130
+
131
+ describe "dependency_child_hash and look_for_dependents" do
132
+ before(:each) do
133
+ @fsa = FieldsetAssociator.new
134
+
135
+ @fieldset1 = Fieldset.new
136
+ @fieldset1.stub!(:id).and_return(100)
137
+ @fieldset2 = Fieldset.new
138
+ @fieldset2.stub!(:id).and_return(200)
139
+
140
+ @field1 = Field.new
141
+ @field1.stub!(:id).and_return(100)
142
+ @field2 = Field.new
143
+ @field2.stub!(:id).and_return(200)
144
+
145
+ @fsa.stub!(:fieldset_id).and_return(100)
146
+ @fsa.stub!(:fieldset).and_return(@fieldset1)
147
+
148
+ @fsc1 = FieldsetChild.new
149
+ @fsc1.stub!(:id).and_return(100)
150
+ @fsc1.stub!(:fieldset_id).and_return(@fieldset1.id)
151
+ @fsc1.stub!(:child_id).and_return(@fieldset2.id)
152
+ @fsc1.stub!(:child_type).and_return("DynamicFieldsets::Fieldset")
153
+ @fsc1.stub!(:child).and_return(@fieldset2)
154
+ @fieldset1.stub!(:fieldset_children).and_return([@fsc1])
155
+
156
+ @fsc2 = FieldsetChild.new
157
+ @fsc2.stub!(:id).and_return(200)
158
+ @fsc2.stub!(:fieldset_id).and_return(@fieldset2.id)
159
+ @fsc2.stub!(:child_id).and_return(@field1.id)
160
+ @fsc2.stub!(:child_type).and_return("DynamicFieldsets::Field")
161
+ @fsc2.stub!(:child).and_return(@field1)
162
+
163
+ @fsc3 = FieldsetChild.new
164
+ @fsc3.stub!(:id).and_return(300)
165
+ @fsc3.stub!(:fieldset_id).and_return(@fieldset2.id)
166
+ @fsc3.stub!(:child_id).and_return(@field2.id)
167
+ @fsc3.stub!(:child_type).and_return("DynamicFieldsets::Field")
168
+ @fsc3.stub!(:child).and_return(@field2)
169
+
170
+ @fieldset2.stub!(:fieldset_children).and_return([@fsc2, @fsc3])
171
+
172
+ @group = DependencyGroup.new
173
+ @group.stub!(:id).and_return(100)
174
+ @group.stub!(:fieldset_child_id).and_return(@fsc3.id)
175
+ @group.stub!(:fieldset_child).and_return(@fsc3)
176
+ @group.stub!(:action).and_return("show")
177
+
178
+ @clause = DependencyClause.new
179
+ @clause.stub!(:id).and_return(100)
180
+ @clause.stub!(:dependency_group).and_return(@group)
181
+ @clause.stub!(:dependency_group_id).and_return(@group.id)
182
+ @group.stub!(:dependency_clauses).and_return([@clause])
183
+
184
+ @dependency = Dependency.new
185
+ @dependency.stub!(:id).and_return(100)
186
+ @dependency.stub!(:value).and_return(5)
187
+ @dependency.stub!(:relationship).and_return("equals")
188
+ @dependency.stub!(:dependency_clause).and_return(@clause)
189
+ @dependency.stub!(:dependency_clause_id).and_return(@clause.id)
190
+ @dependency.stub!(:fieldset_child).and_return(@fsc2)
191
+ @dependency.stub!(:fieldset_child_id).and_return(@fsc2.id)
192
+ @fsc2.stub!(:dependencies).and_return([@dependency])
193
+ @clause.stub!(:dependencies).and_return([@dependency])
194
+ end
195
+
196
+ # The next two tests are specifically for look_for_dependents
197
+
198
+ it "should update the correct array" do
199
+ pending
200
+ @fieldset_child_collection = []
201
+ look_for_dependents(@fieldset1)
202
+ @fieldset_child_collection.empty?.should be_false
203
+ end
204
+
205
+ it "should update the array to appropriate value" do
206
+ pending
207
+ @fieldset_child_collection = []
208
+ expected_results = [@fsc2]
209
+ look_for_dependents(@fieldset1)
210
+ @fieldset_child_collection.should == expected_results
211
+ end
212
+
213
+ # The next few tests are specifically for dependency_child_hash
214
+
215
+ it "should return a hash" do
216
+ stub!(:look_for_dependents).and_return([@fsc1])
217
+ @fsa.dependency_child_hash.should be_a_kind_of Hash
218
+ end
219
+
220
+ it "should have a precise response" do
221
+ stub!(:look_for_dependents).and_return([@fsc1])
222
+ expected_results = {
223
+ @fsc2.id => {
224
+ @group.id => {
225
+ "action" => "show",
226
+ "fieldset_child_id" => @fsc3.id,
227
+ "field_id" => @field2.id,
228
+ "clause" => {
229
+ @clause.id => {
230
+ @dependency.id => {
231
+ "fieldset_child_id" => @fsc2.id,
232
+ "relationship" => "equals",
233
+ "value" => 5
234
+ }
235
+ }
236
+ }
237
+ }
238
+ }
239
+ }
240
+ @fsa.dependency_child_hash.should == expected_results
241
+ end
242
+
243
+ end
244
+
132
245
  end
@@ -0,0 +1,122 @@
1
+ require 'spec_helper'
2
+ include DynamicFieldsets
3
+
4
+ describe FieldsetChild do
5
+ include FieldsetChildHelper
6
+
7
+ it "should respond to fieldset" do
8
+ FieldsetChild.new.should respond_to :fieldset
9
+ end
10
+
11
+ it "should respond to child" do
12
+ FieldsetChild.new.should respond_to :child
13
+ end
14
+
15
+ it "should respond to dependency_group" do
16
+ FieldsetChild.new.should respond_to :dependency_group
17
+ end
18
+
19
+ it "should respond to field_records" do
20
+ FieldsetChild.new.should respond_to :field_records
21
+ end
22
+
23
+ it "should respond to order_num" do
24
+ FieldsetChild.new.should respond_to :order_num
25
+ end
26
+
27
+ describe "validations" do
28
+ before(:each) do
29
+ @fieldset_child = FieldsetChild.new
30
+ end
31
+ it "are valid" do
32
+ @fieldset_child.attributes = valid_attributes
33
+ @fieldset_child.should be_valid
34
+ end
35
+
36
+ it "require fieldset" do
37
+ @fieldset_child.should have(1).error_on(:fieldset_id)
38
+ end
39
+
40
+ it "require child" do
41
+ @fieldset_child.should have(1).error_on(:child_id)
42
+ @fieldset_child.should have(1).error_on(:child_type)
43
+ end
44
+
45
+ it "require order_num" do
46
+ @fieldset_child.stub!(:assign_order)
47
+ @fieldset_child.should have(1).error_on(:order_num)
48
+ end
49
+
50
+ it "should allow a child type of 'DynamicFieldsets::Field'" do
51
+ @fieldset_child.child_type = "DynamicFieldsets::Field"
52
+ @fieldset_child.should have(0).error_on(:child_type)
53
+ end
54
+
55
+ it "should allow a child type of 'DynamicFieldsets::Fieldset'" do
56
+ @fieldset_child.child_type = "DynamicFieldsets::Fieldset"
57
+ @fieldset_child.should have(0).error_on(:child_type)
58
+ end
59
+
60
+ it "should not allow a child type if not 'DynamicFieldsets::Field' or 'DynamicFieldsets::Fieldset'" do
61
+ @fieldset_child.child_type = "Not Field or Fieldset"
62
+ @fieldset_child.should have(1).error_on(:child_type)
63
+ end
64
+
65
+ it "should not allow duplicate pairings of fieldsets and fields" do
66
+ fieldset = Fieldset.new
67
+ field = Field.new
68
+ field.stub!(:id).and_return(100)
69
+ fieldset_child1 = FieldsetChild.new(:fieldset => fieldset, :child => field, :order_num => 1)
70
+ fieldset_child1.stub!(:id).and_return(100)
71
+ fieldset_child2 = FieldsetChild.new(:fieldset => fieldset, :child => field, :order_num => 2)
72
+ FieldsetChild.stub!(:where).and_return([fieldset_child1])
73
+ fieldset_child2.should have(1).error_on(:child_id)
74
+ end
75
+
76
+ # this should have two errors, because it's also a loop
77
+ it "cannot be it's own parent" do
78
+ fieldset = Fieldset.new
79
+ fieldset.stub!(:id).and_return(100)
80
+ fieldset_child = FieldsetChild.new(:fieldset_id => fieldset.id, :child => fieldset, :order_num => 1)
81
+ fieldset_child.should have(2).error_on(:child_id)
82
+ end
83
+
84
+ it "should not allow a parent fieldset when it would create a cycle" do
85
+ fieldset1 = Fieldset.new
86
+ fieldset1.stub!(:id).and_return(100)
87
+ fieldset2 = Fieldset.new
88
+ fieldset2.stub!(:id).and_return(200)
89
+ fieldset3 = Fieldset.new
90
+ fieldset3.stub!(:id).and_return(300)
91
+ fieldset_child1 = FieldsetChild.create(:fieldset_id => fieldset1.id, :child => fieldset2, :order_num => 1)
92
+ fieldset_child2 = FieldsetChild.create(:fieldset_id => fieldset2.id, :child => fieldset3, :order_num => 1)
93
+ fieldset_child3 = FieldsetChild.new(:fieldset_id => fieldset3.id, :child => fieldset1, :order_num => 1)
94
+ fieldset_child3.should have(1).error_on(:child_id)
95
+ end
96
+ end
97
+
98
+ describe "to_hash method" do
99
+ it "needs specs"
100
+ end
101
+
102
+ describe "root_fieldset method" do
103
+ before(:each) do
104
+ # too many issues with stubbing, getting lazy and saving
105
+ # could be refactored at some point
106
+
107
+ @field = Field.create({ :name => "Test field name", :label => "Test field label", :field_type => "textfield", :required => true, :enabled => true, })
108
+ @fieldset = Fieldset.create({:name => "Hire Form", :description => "Hire a person for a job", :nkey => "hire_form"})
109
+ @root_fieldset = Fieldset.create({:name => "Hire Form2", :description => "Hire a person for a job2", :nkey => "hire_form2"})
110
+
111
+ @field_child = FieldsetChild.create(:child => @child, :fieldset => @fieldset, :order_num => 1)
112
+ @child = FieldsetChild.create(:child => @fieldset, :fieldset => @root_fieldset, :order_num => 2)
113
+ end
114
+ it "should return a fieldset if it is not present as the child in fieldset child" do
115
+ @child.root_fieldset.should == @root_fieldset
116
+ end
117
+
118
+ it "should recurse if the fieldset is present as a child in a fieldset child" do
119
+ @field_child.root_fieldset.id.should == @root_fieldset.id
120
+ end
121
+ end
122
+ end
@@ -4,19 +4,16 @@ include DynamicFieldsets
4
4
  describe Fieldset do
5
5
  include FieldsetHelper
6
6
 
7
- it "should respond to fields" do
8
- fieldset = Fieldset.new
9
- fieldset.should respond_to :fields
7
+ it "should respond to parent" do
8
+ Fieldset.new.should respond_to :parent
10
9
  end
11
10
 
12
- it "should respond to parent_fieldset" do
13
- fieldset = Fieldset.new
14
- fieldset.should respond_to :parent_fieldset
11
+ it "should respond to child_fields" do
12
+ Fieldset.new.should respond_to :child_fields
15
13
  end
16
14
 
17
15
  it "should respond to child_fieldsets" do
18
- fieldset = Fieldset.new
19
- fieldset.should respond_to :child_fieldsets
16
+ Fieldset.new.should respond_to :child_fieldsets
20
17
  end
21
18
 
22
19
  describe "validations" do
@@ -24,18 +21,11 @@ describe Fieldset do
24
21
  @fieldset = Fieldset.new
25
22
  end
26
23
 
27
- it "should be valid as a top level fieldset" do
28
- @fieldset.attributes = valid_root_attributes
24
+ it "should be valid" do
25
+ @fieldset.attributes = valid_attributes
29
26
  @fieldset.nkey = "top_level_nkey"
30
27
  @fieldset.should be_valid
31
28
  end
32
-
33
- it "should be valid as a child fieldset" do
34
- @fieldset.attributes = valid_child_attributes
35
- @fieldset.nkey = "child_level_nkey"
36
- @fieldset.should be_valid
37
- end
38
-
39
29
  it "should require a name" do
40
30
  @fieldset.should have(1).error_on(:name)
41
31
  end
@@ -49,51 +39,60 @@ describe Fieldset do
49
39
  end
50
40
 
51
41
  it "should require a unique nkey" do
52
- Fieldset.create(valid_root_attributes)
53
- @fieldset.attributes = valid_root_attributes
42
+ Fieldset.create(valid_attributes)
43
+ @fieldset.attributes = valid_attributes
54
44
  @fieldset.should have(1).error_on(:nkey)
55
45
  end
56
-
57
- it "should not require an order number if there is no parent fieldset" do
58
- @fieldset.should have(0).error_on(:order_num)
59
- end
60
-
61
- it "should require an order number if there is a parent fieldset" do
62
- @fieldset.parent_fieldset = Fieldset.new
63
- @fieldset.should have(1).error_on(:order_num)
64
- end
65
-
66
- it "should not allow a parent fieldset when it would create a cycle" do
67
- fieldset1 = Fieldset.new(:nkey => "fieldset1")
68
- fieldset2 = Fieldset.new(:parent_fieldset => fieldset1, :nkey => "fieldset2")
69
- fieldset3 = Fieldset.new(:parent_fieldset => fieldset2, :nkey => "fieldset3")
70
- fieldset1.parent_fieldset = fieldset3
71
-
72
- fieldset1.should have(1).error_on(:parent_fieldset)
73
- end
74
46
  end
75
47
 
76
- describe "roots scope" do
48
+ describe ".roots scope" do
77
49
  before(:each) do
78
- @root_fieldset = Fieldset.new( valid_root_attributes )
50
+ # we could stub this one but I am not convinced the polymorphic relationships actually work
51
+ @root_fieldset = Fieldset.new( valid_attributes )
79
52
  @root_fieldset.save
80
53
 
81
- @child_fieldset = Fieldset.new( valid_child_attributes )
54
+ @child_fieldset = Fieldset.new( valid_attributes )
55
+ @child_fieldset.nkey = "something_else" # need to pass validations
82
56
  @child_fieldset.save
57
+
58
+ @fieldset_children = FieldsetChild.new(:child => @child_fieldset, :fieldset => @root_fieldset, :order_num => 1)
59
+ @fieldset_children.save
83
60
  end
84
61
 
85
- it "should respond to roots scope" do
62
+ it ": Fieldset responds to .roots" do
86
63
  Fieldset.should respond_to :roots
87
64
  end
88
65
 
89
- it "should return fieldsets with no parent fieldset" do
66
+ it "returns fieldsets with no parent" do
90
67
  roots = Fieldset.roots
91
- roots.select{ |f| f.parent_fieldset.nil? }.should have(1).fieldset
68
+ roots.each do |root|
69
+ root.parent.should be_nil
70
+ end
92
71
  end
93
72
 
94
- it "should not return fieldsets with a parent fieldset" do
73
+ it "does not return fieldsets with a parent" do
95
74
  roots = Fieldset.roots
96
- roots.select{ |f| !f.parent_fieldset.nil? }.should have(0).fieldset
75
+ roots.should_not include @child_fieldset
76
+ end
77
+ end
78
+
79
+ describe ".root? method" do
80
+ before(:each) do
81
+ @fieldset = Fieldset.new
82
+ end
83
+
84
+ it "calls parent" do
85
+ @fieldset.should_receive(:parent).and_return nil
86
+ @fieldset.root?
87
+ end
88
+
89
+ it "returns true if parent is nil" do
90
+ @fieldset.stub!(:parent).and_return nil
91
+ @fieldset.root?.should be_true
92
+ end
93
+ it "returns false if parent is not nil" do
94
+ @fieldset.stub!(:parent).and_return "some object goes here"
95
+ @fieldset.root?.should be_false
97
96
  end
98
97
  end
99
98
 
@@ -105,63 +104,62 @@ describe Fieldset do
105
104
  end
106
105
  end
107
106
 
107
+ # gave up on stubs and mocks on this one due to how the data is constantized
108
108
  describe "children method" do
109
109
  before(:each) do
110
- @root_fieldset = Fieldset.new( valid_root_attributes )
110
+ @root_fieldset = Fieldset.new( valid_attributes )
111
+ @root_fieldset.stub!(:id).and_return(1234)
111
112
 
112
- @child_fieldset = mock_model Fieldset
113
- @child_fieldset.stub!(:id).and_return 2
114
- @child_fieldset.stub!(:order_num).and_return 1
115
- @child_fieldset.stub!(:name).and_return
113
+ @child_fieldset = Fieldset.new( valid_attributes )
114
+ @child_fieldset.nkey = "child_fieldset"
115
+ @child_fieldset.save
116
+ @cfs = FieldsetChild.new(:child => @child_fieldset, :fieldset => @root_fieldset, :order_num => 1)
116
117
 
117
- @field1 = mock_model Field
118
- @field1.stub!(:id).and_return 1
119
- @field1.stub!(:order_num).and_return 2
120
- @field1.stub!(:name).and_return 'z-field'
121
- @field1.stub!(:enabled).and_return true
118
+ @field1 = Field.new(
119
+ :name => "Test field name",
120
+ :label => "Test field label",
121
+ :field_type => "textfield",
122
+ :required => true,
123
+ :enabled => true)
124
+ @field1.save
125
+ @cf1 = FieldsetChild.new(:child => @field1, :fieldset => @root_fieldset, :order_num => 2)
122
126
 
123
- @field2 = mock_model Field
124
- @field2.stub!(:id).and_return 2
125
- @field2.stub!(:order_num).and_return 2
126
- @field2.stub!(:name).and_return 'a-field'
127
- @field2.stub!(:enabled).and_return true
127
+ @field2 = Field.new(
128
+ :name => "Test field name",
129
+ :label => "Test field label",
130
+ :field_type => "textfield",
131
+ :required => true,
132
+ :enabled => false)
133
+ @field2.save
134
+ @cf2 = FieldsetChild.new(:child => @field2, :fieldset => @root_fieldset, :order_num => 3)
128
135
 
136
+ @root_fieldset_children = [@cfs, @cf1, @cf2]
137
+ @root_fieldset.stub!(:fieldset_children).and_return(@root_fieldset_children)
129
138
  end
130
139
 
131
140
  it "should respond to children" do
132
141
  @root_fieldset.should respond_to :children
133
142
  end
134
143
 
135
- it "should call fields and return them" do
136
- @root_fieldset.should_receive(:fields).and_return [@field1]
137
- @root_fieldset.children.should include @field1
138
- end
139
-
140
- it "should call child_fieldsets and return them" do
141
- @root_fieldset.should_receive(:child_fieldsets).and_return [@child_fieldset]
142
- @root_fieldset.children.should include @child_fieldset
144
+ it "should call fieldset children" do
145
+ @root_fieldset.should_receive(:fieldset_children).and_return(@root_fieldset_children)
146
+ children = @root_fieldset.children
143
147
  end
144
-
148
+
145
149
  it "should return a mixture of fieldsets and fields" do
146
- @root_fieldset.stub!(:fields).and_return [@field1]
147
- @root_fieldset.stub!(:child_fieldsets).and_return [@child_fieldset]
148
150
  children = @root_fieldset.children
149
151
  children.should include @field1
150
152
  children.should include @child_fieldset
151
153
  end
152
-
153
- it "should maintain the order of the children regardless of class" do
154
- @root_fieldset.stub!(:fields).and_return [@field1]
155
- @root_fieldset.stub!(:child_fieldsets).and_return [@child_fieldset]
154
+
155
+ it "should not include disabled fields" do
156
156
  children = @root_fieldset.children
157
- children.first.should == @child_fieldset
158
- children.last.should == @field1
157
+ children.should_not include @field2
159
158
  end
160
159
 
161
- it "should handle duplicate order numbers by alphabetical order of name" do
162
- @root_fieldset.stub!(:fields).and_return [@field1, @field2]
160
+ it "should maintain the order of the children regardless of class" do
163
161
  children = @root_fieldset.children
164
- children.first.should == @field2
162
+ children.first.should == @child_fieldset
165
163
  children.last.should == @field1
166
164
  end
167
165
  end
@@ -0,0 +1,9 @@
1
+ # Helper for the dependency group model to create valid attributes
2
+ module DependencyGroupHelper
3
+ def valid_attributes
4
+ {
5
+ :fieldset_child_id => 42,
6
+ :action => "show",
7
+ }
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ # Helper for the field model to create valid attributes
2
+ module DependencyHelper
3
+ # this has issues with not being able to set type
4
+ # I am manually setting it where it is needed to avoid problems
5
+ def valid_attributes
6
+ {
7
+ :fieldset_child_id => 100,
8
+ :relationship => "equals",
9
+ :value => "test string",
10
+ :dependency_clause_id => 100
11
+ }
12
+ end
13
+ end
@@ -4,13 +4,11 @@ module FieldHelper
4
4
  # I am manually setting it where it is needed to avoid problems
5
5
  def valid_attributes
6
6
  {
7
- :fieldset_id => 1,
8
7
  :name => "Test field name",
9
8
  :label => "Test field label",
10
9
  :field_type => "textfield",
11
10
  :required => true,
12
11
  :enabled => true,
13
- :order_num => 1
14
12
  }
15
13
  end
16
14
  end
@@ -0,0 +1,10 @@
1
+ module FieldsetChildHelper
2
+ def valid_attributes
3
+ {
4
+ :child_id => 1,
5
+ :child_type => "DynamicFieldsets::Field",
6
+ :fieldset_id => 1,
7
+ :order_num => 1
8
+ }
9
+ end
10
+ end
@@ -1,28 +1,12 @@
1
1
  # Helper for the fieldset to create valid attributes and other data used in multiple tests
2
2
  module FieldsetHelper
3
- # attributes for a root fieldset with no parent
3
+ # attributes for a fieldset
4
4
  # @returns [Hash] all attributes needed to pass validations
5
- def valid_root_attributes
5
+ def valid_attributes
6
6
  {
7
- :id => 1,
8
7
  :name => "Hire Form",
9
8
  :description => "Hire a person for a job",
10
9
  :nkey => "hire_form"
11
10
  }
12
11
  end
13
-
14
- # attributes for a child fieldset with a parent
15
- # the parent_fieldset may be wrong
16
- # @returns [Hash] all attributes needed to pass validations
17
- def valid_child_attributes
18
- {
19
- :name => "Hire Form",
20
- :description => "Hire a person for a job",
21
- :nkey => "hire_form",
22
- :order_num => 1,
23
- :parent_fieldset_id => 1
24
- }
25
- # this should be in there but isnt working right now
26
- #:parent_fieldset => DynamicFieldset::Fieldset.new
27
- end
28
12
  end