freeform 1.0.11 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,239 +1,67 @@
1
1
  require 'spec_helper'
2
+ require 'freeform/form/model'
2
3
  require 'freeform/form/property'
3
4
 
4
5
  describe FreeForm::Property do
5
- describe "class methods", :class_methods => true do
6
- describe "models", :models => true do
7
- describe "declared model", :declared_model => true do
8
- let(:form_class) do
9
- Class.new(Module) do
10
- include FreeForm::Property
11
- declared_model :test_model_1
12
- declared_model :test_model_2
13
-
14
- def initialize(h={})
15
- h.each {|k,v| send("#{k}=",v)}
16
- end
17
- end
18
- end
19
-
20
- let(:form) do
21
- form_class.new(
22
- :test_model_1 => OpenStruct.new(:dummy => "yes"),
23
- :test_model_2 => OpenStruct.new(:test => "clearly")
24
- )
25
- end
26
-
27
- it "sets declared models as accessor" do
28
- form.test_model_1.should eq(OpenStruct.new(:dummy => "yes"))
29
- form.test_model_2.should eq(OpenStruct.new(:test => "clearly"))
30
- end
31
-
32
- it "has writable set accessors" do
33
- form.test_model_1 = "Something New"
34
- form.test_model_1.should eq("Something New")
35
- end
36
-
37
- it "sets @models in class to declared models" do
38
- form_class.models.should eq([:test_model_1, :test_model_2])
39
- end
40
- end
41
-
42
- describe "form model", :form_model => true do
43
- let(:form_class) do
44
- Class.new(Module) do
45
- include FreeForm::Property
46
- form_model :test_model_1
47
- form_model :test_model_2
48
-
49
- def initialize(h={})
50
- h.each {|k,v| send("#{k}=",v)}
51
- end
52
- end
53
- end
54
-
55
- let(:form) do
56
- form_class.new(
57
- :test_model_1 => OpenStruct.new(:dummy => "yes"),
58
- :test_model_2 => OpenStruct.new(:test => "clearly")
59
- )
60
- end
61
-
62
- it "sets declared models as accessor" do
63
- form.test_model_1.should eq(OpenStruct.new(:dummy => "yes"))
64
- form.test_model_2.should eq(OpenStruct.new(:test => "clearly"))
65
- end
66
-
67
- it "has writable set accessors" do
68
- form.test_model_1 = "Something New"
69
- form.test_model_1.should eq("Something New")
70
- end
71
-
72
- it "sets @models in class to declared models" do
73
- form_class.models.should eq([:test_model_1, :test_model_2])
74
- end
75
- end
76
-
77
- describe "declared models", :declared_models => true do
78
- let(:form_class) do
79
- Class.new(Module) do
80
- include FreeForm::Property
81
- declared_models :test_model_1, :test_model_2
82
-
83
- def initialize(h={})
84
- h.each {|k,v| send("#{k}=",v)}
85
- end
86
- end
87
- end
88
-
89
- let(:form) do
90
- form_class.new(
91
- :test_model_1 => OpenStruct.new(:dummy => "yes"),
92
- :test_model_2 => OpenStruct.new(:test => "clearly")
93
- )
94
- end
95
-
96
- it "sets declared models as accessor" do
97
- form.test_model_1.should eq(OpenStruct.new(:dummy => "yes"))
98
- form.test_model_2.should eq(OpenStruct.new(:test => "clearly"))
99
- end
100
-
101
- it "has writable set accessors" do
102
- form.test_model_1 = "Something New"
103
- form.test_model_1.should eq("Something New")
104
- end
105
-
106
- it "sets @models in class to declared models" do
107
- form_class.models.should eq([:test_model_1, :test_model_2])
6
+ describe "class methods", :class_methods => true do
7
+ describe "property", :property => true do
8
+ let(:form_class) do
9
+ Class.new(Module) do
10
+ include FreeForm::Model
11
+ include FreeForm::Property
12
+ declared_model :test_model
13
+
14
+ property :attribute_1, :on => :test_model
15
+ property :attribute_2, :on => :test_model, :ignore_blank => true
16
+ property :attribute_3, :as => :test_attribute_3, :on => :test_model
17
+
18
+ def initialize(h={})
19
+ h.each {|k,v| send("#{k}=",v)}
20
+ end
108
21
  end
109
22
  end
110
-
111
- describe "form models", :form_models => true do
112
- let(:form_class) do
113
- Class.new(Module) do
114
- include FreeForm::Property
115
- form_models :test_model_1, :test_model_2
116
-
117
- def initialize(h={})
118
- h.each {|k,v| send("#{k}=",v)}
119
- end
120
- end
121
- end
122
-
123
- let(:form) do
124
- form_class.new(
125
- :test_model_1 => OpenStruct.new(:dummy => "yes"),
126
- :test_model_2 => OpenStruct.new(:test => "clearly")
127
- )
128
- end
129
-
130
- it "sets declared models as accessor" do
131
- form.test_model_1.should eq(OpenStruct.new(:dummy => "yes"))
132
- form.test_model_2.should eq(OpenStruct.new(:test => "clearly"))
133
- end
134
-
135
- it "has writable set accessors" do
136
- form.test_model_1 = "Something New"
137
- form.test_model_1.should eq("Something New")
138
- end
139
-
140
- it "sets @models in class to declared models" do
141
- form_class.models.should eq([:test_model_1, :test_model_2])
142
- end
23
+
24
+ let(:form) do
25
+ form_class.new(
26
+ :test_model => OpenStruct.new(:attribute_1 => "yes"),
27
+ )
143
28
  end
29
+
30
+ describe "delegation" do
31
+ it "delegates properties to models" do
32
+ form.attribute_1.should eq("yes")
33
+ form.attribute_2.should be_nil
34
+ end
35
+
36
+ it "allows properties to be written, and delegates the values" do
37
+ form.attribute_1 = "changed"
38
+ form.attribute_1.should eq("changed")
39
+ form.test_model.attribute_1.should eq("changed")
40
+ end
144
41
 
145
- describe "child models", :child_models => true do
146
- let(:form_class) do
147
- Class.new(Module) do
148
- include FreeForm::Property
149
- form_model :test_model_1
150
- child_model :test_model_2 do
151
- test_model_1.build_child
152
- end
153
-
154
- def initialize(h={})
155
- h.each {|k,v| send("#{k}=",v)}
156
- initialize_test_model_2
157
- end
158
- end
159
- end
160
-
161
- let(:form) do
162
- form_class.new(
163
- :test_model_1 => OpenStruct.new(:dummy => "yes",
164
- :build_child => OpenStruct.new(:field => "child model"))
165
- )
166
- end
167
-
168
- it "sets declared models as accessor" do
169
- form.test_model_1.should eq(OpenStruct.new(:dummy => "yes", :build_child => OpenStruct.new(:field => "child model")))
170
- form.test_model_2.should eq(OpenStruct.new(:field => "child model"))
171
- end
172
-
173
- it "sets @models in class to declared models" do
174
- form_class.models.should eq([:test_model_1, :test_model_2])
175
- end
42
+ it "delegates using :as" do
43
+ form.test_attribute_3.should be_nil
44
+ form.test_attribute_3 = "set_using_as"
45
+ form.test_attribute_3.should eq("set_using_as")
46
+ form.test_model.attribute_3.should eq("set_using_as")
47
+ end
176
48
  end
177
- end
178
-
179
- describe "properties", :properties => true do
180
- describe "property", :property => true do
181
- let(:form_class) do
182
- Class.new(Module) do
183
- include FreeForm::Property
184
- declared_model :test_model
185
-
186
- property :attribute_1, :on => :test_model
187
- property :attribute_2, :on => :test_model, :ignore_blank => true
188
- property :attribute_3, :as => :test_attribute_3, :on => :test_model
189
-
190
- def initialize(h={})
191
- h.each {|k,v| send("#{k}=",v)}
192
- end
193
- end
194
- end
195
-
196
- let(:form) do
197
- form_class.new(
198
- :test_model => OpenStruct.new(:attribute_1 => "yes"),
49
+
50
+ describe "property_mappings" do
51
+ it "sets appropriate property mappings" do
52
+ form_class.property_mappings.should eq(
53
+ { :attribute_1 => {:model => :test_model, :field => :attribute_1, :ignore_blank => false},
54
+ :attribute_2 => {:model => :test_model, :field => :attribute_2, :ignore_blank => true},
55
+ :test_attribute_3 => {:model => :test_model, :field => :attribute_3, :ignore_blank => false}
56
+ }
199
57
  )
200
- end
201
-
202
- describe "delegation" do
203
- it "delegates properties to models" do
204
- form.attribute_1.should eq("yes")
205
- form.attribute_2.should be_nil
206
- end
207
-
208
- it "allows properties to be written, and delegates the values" do
209
- form.attribute_1 = "changed"
210
- form.attribute_1.should eq("changed")
211
- form.test_model.attribute_1.should eq("changed")
212
- end
213
-
214
- it "delegates using :as" do
215
- form.test_attribute_3.should be_nil
216
- form.test_attribute_3 = "set_using_as"
217
- form.test_attribute_3.should eq("set_using_as")
218
- form.test_model.attribute_3.should eq("set_using_as")
219
- end
220
- end
221
-
222
- describe "property_mappings" do
223
- it "sets appropriate property mappings" do
224
- form_class.property_mappings.should eq(
225
- { :attribute_1 => {:model => :test_model, :field => :attribute_1, :ignore_blank => false},
226
- :attribute_2 => {:model => :test_model, :field => :attribute_2, :ignore_blank => true},
227
- :test_attribute_3 => {:model => :test_model, :field => :attribute_3, :ignore_blank => false}
228
- }
229
- )
230
- end
231
- end
58
+ end
232
59
  end
233
60
 
234
61
  describe "allow_destroy_on_save", :allow_destroy_on_save => true do
235
62
  let(:form_class) do
236
63
  Class.new(Module) do
64
+ include FreeForm::Model
237
65
  include FreeForm::Property
238
66
  declared_model :test_model
239
67
  allow_destroy_on_save
@@ -302,44 +130,10 @@ describe FreeForm::Property do
302
130
  end
303
131
 
304
132
  describe "instance methods", :instance_methods => true do
305
- describe "parent_form", :parent_form => true do
306
- let(:form_class) do
307
- Class.new(Module) do
308
- include FreeForm::Property
309
- declared_model :first_model
310
- declared_model :second_model
311
-
312
- property :attribute_1, :on => :first_model
313
- property :attribute_2, :on => :first_model, :ignore_blank => true
314
- property :attribute_3, :on => :second_model, :ignore_blank => true
315
- property :attribute_4, :on => :second_model
316
-
317
- def initialize(h)
318
- h.each {|k,v| send("#{k}=",v)}
319
- end
320
- end
321
- end
322
-
323
- let(:form) do
324
- form_class.new(
325
- :first_model => OpenStruct.new(:attribute_1 => "first", :attribute_2 => "second"),
326
- :second_model => OpenStruct.new(:attribute_3 => "third", :attribute_4 => "fourth"),
327
- )
328
- end
329
-
330
- it "has parent_form as nil by default" do
331
- form.parent_form.should be_nil
332
- end
333
-
334
- it "has can assign and reference parent_form" do
335
- parent = Module.new
336
- form.parent_form = parent
337
- form.parent_form.should eq(parent)
338
- end
339
- end
340
133
  describe "assign_attributes", :assign_attributes => true do
341
134
  let(:form_class) do
342
135
  Class.new(Module) do
136
+ include FreeForm::Model
343
137
  include FreeForm::Property
344
138
  declared_model :first_model
345
139
  declared_model :second_model
@@ -394,5 +188,59 @@ describe FreeForm::Property do
394
188
  form.attribute_1.should eq(Date.new(2013, 6, 5))
395
189
  end
396
190
  end
191
+
192
+ describe "model_property_mappings", :model_property_mappings => true do
193
+ let(:form_class) do
194
+ Class.new(Module) do
195
+ include FreeForm::Model
196
+ include FreeForm::Property
197
+ declared_model :first_model
198
+ declared_model :second_model
199
+
200
+ property :attribute_1, :on => :first_model
201
+ property :attribute_2, :on => :first_model, :ignore_blank => true
202
+ property :attribute_3, :on => :second_model, :ignore_blank => true
203
+ property :attribute_4, :on => :second_model
204
+
205
+ def initialize(h)
206
+ h.each {|k,v| send("#{k}=",v)}
207
+ end
208
+ end
209
+ end
210
+
211
+ let(:form) do
212
+ form_class.new(
213
+ :first_model => OpenStruct.new(:attribute_1 => "first", :attribute_2 => "second"),
214
+ :second_model => OpenStruct.new(:attribute_3 => "third", :attribute_4 => "fourth"),
215
+ )
216
+ end
217
+
218
+ it "has correct model_property_mappings hash" do
219
+ form.model_property_mappings.should eq(
220
+ {
221
+ :attribute_1 => {
222
+ :model => :first_model,
223
+ :field => :attribute_1,
224
+ :ignore_blank => false,
225
+ },
226
+ :attribute_2 => {
227
+ :model => :first_model,
228
+ :field => :attribute_2,
229
+ :ignore_blank => true,
230
+ },
231
+ :attribute_3 => {
232
+ :model => :second_model,
233
+ :field => :attribute_3,
234
+ :ignore_blank => true,
235
+ },
236
+ :attribute_4 => {
237
+ :model => :second_model,
238
+ :field => :attribute_4,
239
+ :ignore_blank => false,
240
+ },
241
+ }
242
+ )
243
+ end
244
+ end
397
245
  end
398
246
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe FreeForm::Validation do
4
4
  let(:user) do
5
- model = Class.new(Module) do
5
+ Class.new(Module) do
6
6
  include ActiveModel::Validations
7
7
  def self.model_name; ActiveModel::Name.new(self, nil, "user") end
8
8
 
@@ -12,11 +12,10 @@ describe FreeForm::Validation do
12
12
  def save; return valid? end
13
13
  alias_method :save!, :save
14
14
  end.new
15
- model
16
15
  end
17
16
 
18
17
  let(:address) do
19
- model = Class.new(Module) do
18
+ Class.new(Module) do
20
19
  include ActiveModel::Validations
21
20
  def self.model_name; ActiveModel::Name.new(self, nil, "address") end
22
21
 
@@ -27,155 +26,143 @@ describe FreeForm::Validation do
27
26
  def save; return valid? end
28
27
  alias_method :save!, :save
29
28
  end.new
30
- model
31
29
  end
32
30
 
33
- describe "validation", :validation => true do
34
- context "without model validation", :no_model_validation => true do
35
- let!(:nested_class) do
36
- klass = Class.new(Module) do
37
- include ActiveModel::Validations
38
- include FreeForm::Property
39
- include FreeForm::Nested
40
- include FreeForm::Validation
41
- declared_model :address
42
- property :street, :on => :address
31
+ context "with nested form validation", :nested_form_validation => true do
32
+ let(:form_class) do
33
+ klass = Class.new(Module) do
34
+ include ActiveModel::Validations
35
+ include FreeForm::Model
36
+ include FreeForm::Property
37
+ include FreeForm::Nested
38
+ include FreeForm::Validation
39
+ validate_nested_forms
40
+
41
+ form_model :user
42
+ property :username, :on => :user
43
+ validates :username, :presence => true
44
+
45
+ nested_form :addresses do
46
+ declared_model :address
47
+ allow_destroy_on_save
43
48
 
44
- def initialize(h={})
45
- h.each {|k,v| send("#{k}=",v)}
46
- end
49
+ property :street, :on => :address
50
+ validates :street, :presence => true
47
51
  end
48
- # This wrapper just avoids CONST warnings
49
- v, $VERBOSE = $VERBOSE, nil
50
- Module.const_set("AddressForm", klass)
51
- $VERBOSE = v
52
- klass
53
- end
54
-
55
- let(:form_class) do
56
- klass = Class.new(Module) do
57
- include ActiveModel::Validations
58
- include FreeForm::Property
59
- include FreeForm::Nested
60
- include FreeForm::Validation
61
- form_model :user
62
- property :username, :on => :user
63
- validates :username, :presence => true
64
-
65
- nested_form :addresses, :class => Module::AddressForm, :default_initializer => :address_initializer
66
-
67
- def address_initializer
68
- {:address => OpenStruct.new}
69
- end
70
-
71
- def initialize(h={})
72
- h.each {|k,v| send("#{k}=",v)}
73
- end
52
+
53
+ def address_initializer
54
+ {:address => OpenStruct.new}
74
55
  end
75
- # This wrapper just avoids CONST warnings
76
- v, $VERBOSE = $VERBOSE, nil
77
- Module.const_set("DummyForm", klass)
78
- $VERBOSE = v
79
- klass
80
- end
81
-
82
- let(:form) do
83
- test_form = form_class.new(:user => user)
84
- test_form.build_address(:address => address)
85
- test_form
56
+
57
+ def initialize(h={})
58
+ h.each {|k,v| send("#{k}=",v)}
59
+ end
86
60
  end
61
+ # This wrapper just avoids CONST warnings
62
+ v, $VERBOSE = $VERBOSE, nil
63
+ Module.const_set("DummyForm", klass)
64
+ $VERBOSE = v
65
+ klass
66
+ end
87
67
 
88
- it "is not valid initially" do
89
- form.should_not be_valid
68
+ let(:form) do
69
+ form_class.new(:user => user).tap do |f|
70
+ f.build_address
90
71
  end
72
+ end
73
+
74
+ it "is not valid initially" do
75
+ form.should_not be_valid
76
+ end
77
+
78
+ it "has error on :username" do
79
+ form.valid?
80
+ form.errors[:username].should eq(["can't be blank"])
81
+ end
82
+
83
+ it "does not have errors on address" do
84
+ form.valid?
85
+ form.addresses.first.errors[:street].should eq(["can't be blank"])
86
+ end
91
87
 
92
- it "has error on :username" do
93
- form.valid?
94
- form.errors[:username].should eq(["can't be blank"])
88
+ context "with valid model, invalid nested model" do
89
+ before(:each) do
90
+ form.assign_params(:username => "bob", :addresses_attributes => {
91
+ "0" =>
92
+ {
93
+ :street => nil
94
+ }
95
+ }
96
+ )
95
97
  end
96
98
 
97
- it "does not have errors on address" do
98
- form.valid?
99
- form.addresses.first.errors.should be_empty
99
+ it "should be valid" do
100
+ address_form = form.addresses.first
101
+ form.should_not be_valid
100
102
  end
101
103
  end
104
+ end
102
105
 
103
- context "with model validation", :model_validation => true do
104
- let!(:nested_class) do
105
- klass = Class.new(Module) do
106
- include ActiveModel::Validations
107
- include FreeForm::Property
108
- include FreeForm::Nested
109
- include FreeForm::Validation
106
+ context "with model validation", :model_validation => true do
107
+ let(:form_class) do
108
+ klass = Class.new(Module) do
109
+ include ActiveModel::Validations
110
+ include FreeForm::Model
111
+ include FreeForm::Property
112
+ include FreeForm::Nested
113
+ include FreeForm::Validation
114
+ form_model :user
115
+ validate_models
116
+ validate_nested_forms
117
+ property :username, :on => :user
118
+ property :form_property
119
+
120
+ validates :form_property, :presence => true
121
+
122
+ nested_form :addresses do
110
123
  declared_model :address
111
124
  validate_models
112
125
  property :street, :on => :address
113
-
114
- def initialize(h={})
115
- h.each {|k,v| send("#{k}=",v)}
116
- end
117
126
  end
118
- # This wrapper just avoids CONST warnings
119
- v, $VERBOSE = $VERBOSE, nil
120
- Module.const_set("AddressForm", klass)
121
- $VERBOSE = v
122
- klass
123
- end
124
-
125
- let(:form_class) do
126
- klass = Class.new(Module) do
127
- include ActiveModel::Validations
128
- include FreeForm::Property
129
- include FreeForm::Nested
130
- include FreeForm::Validation
131
- form_model :user
132
- validate_models
133
- property :username, :on => :user
134
- property :form_property
135
-
136
- validates :form_property, :presence => true
137
-
138
- nested_form :addresses, :class => Module::AddressForm, :default_initializer => :address_initializer
139
-
140
- def address_initializer
141
- {:address => OpenStruct.new}
142
- end
143
-
144
- def initialize(h={})
145
- h.each {|k,v| send("#{k}=",v)}
146
- end
127
+
128
+ def address_initializer
129
+ {:address => OpenStruct.new}
147
130
  end
148
- # This wrapper just avoids CONST warnings
149
- v, $VERBOSE = $VERBOSE, nil
150
- Module.const_set("DummyForm", klass)
151
- $VERBOSE = v
152
- klass
153
- end
154
-
155
- let(:form) do
156
- test_form = form_class.new( :user => user )
157
- test_form.build_address( :address => address )
158
- test_form
131
+
132
+ def initialize(h={})
133
+ h.each {|k,v| send("#{k}=",v)}
134
+ end
159
135
  end
136
+ # This wrapper just avoids CONST warnings
137
+ v, $VERBOSE = $VERBOSE, nil
138
+ Module.const_set("DummyForm", klass)
139
+ $VERBOSE = v
140
+ klass
141
+ end
160
142
 
161
- it "is not valid initially" do
162
- form.should_not be_valid
143
+ let(:form) do
144
+ form_class.new( :user => user ).tap do |f|
145
+ f.build_address( :address => address )
163
146
  end
147
+ end
164
148
 
165
- it "has error on :username" do
166
- form.valid?
167
- form.errors[:username].should eq(["can't be blank"])
168
- end
149
+ it "is not valid initially" do
150
+ form.should_not be_valid
151
+ end
169
152
 
170
- it "has error on :form_property" do
171
- form.valid?
172
- form.errors[:form_property].should eq(["can't be blank"])
173
- end
153
+ it "has error on :username" do
154
+ form.valid?
155
+ form.errors[:username].should eq(["can't be blank"])
156
+ end
174
157
 
175
- it "has error on address street" do
176
- form.valid?
177
- form.addresses.first.errors[:street].should eq(["can't be blank"])
178
- end
158
+ it "has error on :form_property" do
159
+ form.valid?
160
+ form.errors[:form_property].should eq(["can't be blank"])
161
+ end
162
+
163
+ it "has error on address street", :failing => true do
164
+ form.valid?
165
+ form.addresses.first.errors[:street].should eq(["can't be blank"])
179
166
  end
180
167
  end
181
168
  end
data/spec/spec_helper.rb CHANGED
@@ -22,3 +22,9 @@ Capybara.javascript_driver = :selenium
22
22
  RSpec.configure do |config|
23
23
  config.mock_with :rspec
24
24
  end
25
+
26
+ RSpec::Matchers.define :exist_in_database do
27
+ match do |actual|
28
+ actual.class.exists?(actual.id)
29
+ end
30
+ end