aform 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 93ff36848be09d703e47acb5b73764f97f235ce5
4
- data.tar.gz: bcbac1680a8d4ced2f1bfcaa74170b4ab0ec0052
3
+ metadata.gz: 2a00654d7e27e53fa05173608a7f0630ebe6b771
4
+ data.tar.gz: c76a97771dd6d28f781b3b49c243b3634eebc0ca
5
5
  SHA512:
6
- metadata.gz: f55367787834ae50febed73521daf8a8642a413d91224ed1bdba7924f189fe072d31021603495fa03b99d2e0c6be6abfa5f90442ba8d529edc3cd66d5c630896
7
- data.tar.gz: adf3e582fbc4a2fee4d7895af355a6180d044f00bd083dfac303e4c8cc636a986204cbf438061d26a21967483266444e1d66f409fae42d059cfaaf5c65c684f7
6
+ metadata.gz: 82eb7f0087485fdc85a79bfdee55c7f1f24ebf0d8169a53c510a1889e36d337c45829d20d3afdca609f6fcfc792b2ba498fe685d2356060aa77d7b43d1a87d90
7
+ data.tar.gz: bc19d70163f31bbf851063a4518b11eec248e509b98ff7ea952214949f32420ca15d556d4b3c376ffeaac6e84713b46978d4ddc1cfd8ce469afff50c4a626291
data/lib/aform.rb CHANGED
@@ -9,8 +9,4 @@ require 'aform/model'
9
9
  require 'aform/builder'
10
10
  require 'aform/errors'
11
11
  require 'aform/form_saver'
12
- require 'aform/uniqueness_validator'
13
-
14
- module Aform
15
- # Your code goes here...
16
- end
12
+ require 'aform/uniqueness_validator'
data/lib/aform/builder.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'active_model'
2
+ #tested within model test
2
3
  class Aform::Builder
3
4
  def initialize(model_klass)
4
5
  @model_klass = model_klass
data/lib/aform/form.rb CHANGED
@@ -2,40 +2,38 @@ module Aform
2
2
  class Form
3
3
  class_attribute :params, :pkey, :validations, :nested_form_klasses
4
4
 
5
- attr_reader :form_model, :attributes, :nested_forms, :model, :nested_models, :parent
5
+ attr_reader :form_model, :attributes, :nested_forms, :record, :parent
6
6
 
7
- def initialize(model, attributes, parent = nil ,model_klass = Aform::Model,
8
- model_builder = Aform::Builder, errors_klass = Aform::Errors,
9
- form_saver = Aform::FormSaver, transaction_klass = ActiveRecord::Base)
10
- @model_klass, @model_builder, @errors_klass = model_klass, model_builder, errors_klass
11
- @model, @attributes, @transaction_klass = model, attributes, transaction_klass
7
+ def initialize(record, attributes, parent = nil, opts = {})
8
+ @opts = opts
9
+ @attributes = attributes
10
+ @record = record
12
11
  @parent = parent
13
- @form_saver = form_saver
14
- creator = @model_builder.new(@model_klass)
15
- @form_model = creator.build_model_klass(self.params, self.validations).new(@model, self, @attributes)
12
+ assign_opts_instances
16
13
  initialize_nested
17
14
  end
18
15
 
19
- def invalid?
20
- !valid?
21
- end
22
-
23
16
  def valid?
24
17
  if @nested_forms
25
18
  main = @form_model.valid?
26
- nested = @nested_forms.values.flatten.map(&:valid?).all? #all? don't invoike method on each element
19
+ #all? don't invoike method on each element
20
+ nested = @nested_forms.values.flatten.map(&:valid?).all?
27
21
  main && nested
28
22
  else
29
23
  @form_model.valid?
30
24
  end
31
25
  end
32
26
 
27
+ def invalid?
28
+ !valid?
29
+ end
30
+
33
31
  def save
34
- self.valid? && @form_saver.new(self, @transaction_klass).save
32
+ self.valid? && @form_saver.save
35
33
  end
36
34
 
37
35
  def errors
38
- @errors_klass.new(self).messages
36
+ @errors.messages
39
37
  end
40
38
 
41
39
  class << self
@@ -80,26 +78,34 @@ module Aform
80
78
 
81
79
  private
82
80
 
81
+ def assign_opts_instances
82
+ @errors = @opts[:errors] || Aform::Errors.new(self)
83
+ @form_saver = @opts[:form_saver] || Aform::FormSaver.new(self)
84
+ @form_model = @opts[:form_model] || Aform::Model.\
85
+ build_klass(self.params, self.validations).\
86
+ new(record, self, attributes)
87
+ end
88
+
83
89
  def initialize_nested
84
- if nested_form_klasses
85
- nested_form_klasses.each do |k,v|
86
- if attributes.has_key?(k) && attributes[k]
87
- attributes[k].each do |attrs|
88
- @nested_forms ||= {}
89
- @nested_forms[k] ||= []
90
- model = nested_ar_model(k, attrs, v.pkey)
91
- @nested_forms[k] << v.new(model, attrs, self, @model_klass, @model_builder,
92
- @errors_klass, @form_saver, @transaction_klass)
90
+ @nested_forms =
91
+ if nested_form_klasses
92
+ nested_form_klasses.inject({}) do |memo, (k, v)|
93
+ if attributes[k]
94
+ nested = attributes[k].map do |attrs|
95
+ v.new(nested_record(k, attrs, v.pkey), attrs, self, @opts)
96
+ end
97
+ memo.merge(k => nested)
98
+ else
99
+ memo
93
100
  end
94
101
  end
95
102
  end
96
- end
97
103
  end
98
104
 
99
- def nested_ar_model(association, attrs, key = :id)
105
+ def nested_record(association, attrs, key)
100
106
  key = key || :id
101
- klass = association.to_s.classify.constantize
102
- klass.find_by(key => attrs[key]) || klass.new
107
+ record.send(association).find_by(key => attrs[key]) || \
108
+ association.to_s.classify.constantize.new
103
109
  end
104
110
  end
105
- end
111
+ end
@@ -1,11 +1,15 @@
1
1
  module Aform
2
2
  class FormSaver
3
- def initialize(form, transaction_klass = ActiveRecord::Base)
3
+ attr_reader :transaction_klass, :rollback_klass
4
+
5
+ def initialize(form, opts = {})
6
+ @transaction_klass = opts[:transaction_klass] ||= ActiveRecord::Base
7
+ @rollback_klass = opts[:rollback_klass] ||= ActiveRecord::Rollback
4
8
  @form = form
5
- @transaction_klass = transaction_klass
6
9
  end
7
10
 
8
11
  def save
12
+ result = false
9
13
  @transaction_klass.transaction do
10
14
  result =
11
15
  if @form.nested_forms
@@ -13,9 +17,9 @@ module Aform
13
17
  else
14
18
  @form.form_model.save
15
19
  end
16
- raise(ActiveRecord::Rollback) unless result
17
- result
20
+ raise(@rollback_klass) unless result
18
21
  end
22
+ result
19
23
  end
20
24
 
21
25
  protected
@@ -23,11 +27,11 @@ module Aform
23
27
  def save_nested(form)
24
28
  form.nested_forms.map do |k, v|
25
29
  v.map do |nf|
26
- result = nf.form_model.save(form.model.send(k))
30
+ result = nf.form_model.save(form.record.send(k))
27
31
  save_nested(nf) if nf.nested_forms
28
32
  result
29
33
  end
30
- end
34
+ end.flatten
31
35
  end
32
36
  end
33
37
  end
data/lib/aform/model.rb CHANGED
@@ -10,6 +10,10 @@ class Aform::Model
10
10
  @attributes.merge! attributes_for_save(attributes)
11
11
  end
12
12
 
13
+ def self.build_klass(params, validations, builder = Aform::Builder.new(Aform::Model))
14
+ builder.build_model_klass(params, validations)
15
+ end
16
+
13
17
  def self.model_name
14
18
  ActiveModel::Name.new(self, nil, "Aform::Model")
15
19
  end
@@ -1,4 +1,5 @@
1
1
  require 'active_record'
2
+ # tested within integration test
2
3
  class UniquenessValidator < ::ActiveRecord::Validations::UniquenessValidator
3
4
  def validate_each(form, attribute, value)
4
5
  object = form.instance_variable_get(:@object)
data/lib/aform/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Aform
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,41 +1,79 @@
1
1
  require 'test_helper'
2
2
 
3
3
  describe Aform::FormSaver do
4
+ class RollbackKlass < Exception
5
+ end
6
+
4
7
  class TransactionKlass
5
8
  def self.transaction
6
9
  yield
10
+ rescue RollbackKlass
11
+ true
7
12
  end
8
13
  end
9
14
 
10
- class TestForm
11
- def form_model
12
-
15
+ def mock_nested_form(save = true)
16
+ form_model = OpenStruct.new
17
+ form_model.define_singleton_method :save do |*args|
18
+ save
13
19
  end
20
+ OpenStruct.new(nested_forms: nil,
21
+ form_model: form_model)
22
+ end
14
23
 
15
- def nested_forms
16
- {comments: }
17
- end
24
+ def mock_form(nested_forms: nil, record: nil, save: true)
25
+ OpenStruct.new(nested_forms: nested_forms,
26
+ form_model: OpenStruct.new(save: save),
27
+ record: record)
18
28
  end
19
29
 
20
- let(:form) do
21
- OpenStruct
22
- form = {
23
- form_model: :form_model,
24
- nested_forms: [
25
- {comments: [
26
- nested_form1: {
27
- form_model: :form_model,
28
- nested_forms: [:nested_form2]
29
- }
30
- ]
31
- }
32
- ]
33
- }
30
+ def form_saver(form)
31
+ Aform::FormSaver.new(form, {transaction_klass: TransactionKlass,
32
+ rollback_klass: RollbackKlass})
34
33
  end
35
34
 
36
- subject { Aform::FormSaver.new(form, TransactionKlass) }
35
+ describe "#save" do
36
+ context "saving form" do
37
+ it "success" do
38
+ form = mock_form
39
+ form_saver(form).save.must_equal(true)
40
+ end
41
+
42
+ it "failure" do
43
+ form = mock_form(save: false)
44
+ form_saver(form).save.must_equal(false)
45
+ end
46
+ end
37
47
 
38
- it "saves form" do
48
+ context "saving nested forms" do
49
+ it "success" do
50
+ nested_forms = [mock_nested_form, mock_nested_form]
51
+ form = mock_form(nested_forms: {comments: nested_forms},
52
+ record: OpenStruct.new(comments: []))
53
+ form_saver(form).save.must_equal(true)
54
+ end
39
55
 
56
+ it "failure" do
57
+ nested_forms = [mock_nested_form(false), mock_nested_form]
58
+ form = mock_form(nested_forms: {comments: nested_forms},
59
+ record: OpenStruct.new(comments: []))
60
+ form_saver(form).save.must_equal(false)
61
+ end
62
+ end
63
+ end
64
+
65
+ context "rollback transaction" do
66
+ class TestTransactionKlass
67
+ def self.transaction
68
+ yield
69
+ end
70
+ end
71
+
72
+ it "raises RollbackKlass" do
73
+ form = mock_form(save: false)
74
+ form_saver = Aform::FormSaver.new(form, {transaction_klass: TestTransactionKlass,
75
+ rollback_klass: RollbackKlass})
76
+ -> { form_saver.save }.must_raise(RollbackKlass)
77
+ end
40
78
  end
41
79
  end
data/test/form_test.rb CHANGED
@@ -2,26 +2,17 @@ require 'test_helper'
2
2
 
3
3
  describe Aform::Form do
4
4
 
5
- let(:ar_model) { mock("ar_model") }
6
-
7
- before do
8
- @mock_model_instance = mock("model_instance")
9
- @mock_model_klass = mock("model_class")
10
- @mock_model_klass.stubs(:new).returns(@mock_model_instance)
11
- @mock_builder_instance = mock("builder_instance")
12
- @mock_builder_instance.stubs(:build_model_klass).returns(@mock_model_klass)
13
- @mock_builder_klass = mock("builder_class")
14
- @mock_builder_klass.stubs(:new).returns(@mock_builder_instance)
15
- @mock_transaction = mock("ar_model")
16
- @mock_errors = mock("mock_errors")
17
- end
5
+ let(:record) { mock("record") }
6
+ let(:form_model) {mock("form_model")}
7
+ let(:form_saver) {mock("form_saver")}
8
+ let(:errors) {mock("errors")}
18
9
 
19
10
  describe ".param" do
20
11
  subject do
21
12
  Class.new(Aform::Form) do
22
13
  param :name, :count
23
14
  param :size, model_field: :count
24
- end.new(ar_model, {}, @mock_model_klass, @mock_builder_klass, @mock_errors, @mock_transaction)
15
+ end
25
16
  end
26
17
 
27
18
  it "stores params" do
@@ -39,7 +30,7 @@ describe Aform::Form do
39
30
  validate do
40
31
  errors.add(:base, "Must be foo to be a bar")
41
32
  end
42
- end.new(ar_model, {}, @mock_model_klass, @mock_builder_klass, @mock_errors, @mock_transaction)
33
+ end
43
34
  end
44
35
 
45
36
  it "saves validations" do
@@ -67,11 +58,11 @@ describe Aform::Form do
67
58
  param :name, :count
68
59
  validates_presence_of :name
69
60
  validates :count, presence: true, inclusion: {in: 1..100}
70
- end.new(ar_model, {}, @mock_model_klass, @mock_builder_klass, @mock_errors, @mock_transaction)
61
+ end.new(record, {}, nil, {form_model: form_model} )
71
62
  end
72
63
 
73
- it "calls valid? on model" do
74
- @mock_model_instance.expects(:valid?)
64
+ it "calls valid? on form_model" do
65
+ form_model.expects(:valid?)
75
66
  subject.valid?
76
67
  end
77
68
  end
@@ -82,29 +73,29 @@ describe Aform::Form do
82
73
  param :name, :count
83
74
  validates_presence_of :name
84
75
  validates :count, presence: true, inclusion: {in: 1..100}
85
- end.new(ar_model, {}, @mock_model_klass, @mock_builder_klass, @mock_errors, @mock_transaction)
76
+ end.new(record, {}, nil, {form_saver: form_saver, form_model: form_model})
86
77
  end
87
78
 
88
79
  it "calls model.save" do
89
80
  subject.stubs(:valid?).returns(true)
90
- @mock_model_instance.expects(:save)
81
+ form_saver.expects(:save)
91
82
  subject.save
92
83
  end
93
84
  end
94
85
 
95
86
  describe "#errors" do
96
- #subject do
97
- # Class.new(Aform::Form) do
98
- # param :name, :count
99
- # validates_presence_of :name
100
- # validates :count, presence: true, inclusion: {in: 1..100}
101
- # end.new(ar_model, {}, @mock_model_klass, @mock_builder_klass)
102
- #end
103
- #
104
- #it "calls model.errors" do
105
- # @mock_model_instance.expects(:errors)
106
- # subject.errors
107
- #end
87
+ subject do
88
+ Class.new(Aform::Form) do
89
+ param :name, :count
90
+ validates_presence_of :name
91
+ validates :count, presence: true, inclusion: {in: 1..100}
92
+ end.new(record, {}, nil, {form_saver: form_saver, form_model: form_model, errors: errors})
93
+ end
94
+
95
+ it "calls model.errors" do
96
+ errors.expects(:messages)
97
+ subject.errors
98
+ end
108
99
  end
109
100
 
110
101
  describe "nested objects" do
@@ -130,81 +121,6 @@ describe Aform::Form do
130
121
  it "defines `nested_forms`" do
131
122
  subject.nested_form_klasses.must_equal({comments: subject.comments})
132
123
  end
133
-
134
- describe "initialization" do
135
- it "initializes nested froms" do
136
- model = mock("ar_model")
137
- relation = mock("relation")
138
- relation.expects(:build).times(2)
139
- model.stubs(comments: relation)
140
- subject.new(model, {name: "name", count: 1,
141
- comments: [{author: "Joe", message: "Message 1"},
142
- {author: "Smith", message: "Message 2"}]},
143
- @mock_model_klass, @mock_builder_klass, @mock_errors, @mock_transaction)
144
- end
145
-
146
- context "when `id` is present" do
147
- it "finds model for nested form" do
148
- model = mock("ar_model")
149
- relation = mock("relation")
150
- relation.stubs(:build)
151
- #TODO: rewirte tests
152
- relation.expects(:select).returns([1])
153
- model.stubs(comments: relation)
154
- subject.new(model, {name: "name", count: 1,
155
- comments: [{author: "Joe", message: "Message 1"},
156
- {id: 21, author: "Smith", message: "Message 2"}]},
157
- @mock_model_klass, @mock_builder_klass, @mock_errors, @mock_transaction)
158
- end
159
- end
160
- end
161
-
162
- #describe "#valid?" do
163
- # it "calls valid? on nested forms" do
164
- # Aform::Model.any_instance.expects(:valid?).returns(true).times(3)
165
- # model = mock("ar_model")
166
- # @mock_model_instance.stubs(:valid?).returns(true)
167
- # model.stubs(comments: stub(build: mock("ar_comment_model")))
168
- # form = subject.new(model, {name: "name", count: 1,
169
- # comments: [{author: "Joe", message: "Message 1"},
170
- # {author: "Smith", message: "Message 2"}]},
171
- # @mock_model_klass, @mock_builder_klass, @mock_errors, @mock_transaction)
172
- # form.valid?
173
- # end
174
- #
175
- # #it "calls valid? on nested forms when main form is not valid" do
176
- # # Aform::Model.any_instance.expects(:valid?).returns(false).times(3)
177
- # # model = mock("ar_model")
178
- # # model.stubs(comments: stub(build: mock("ar_comment_model")))
179
- # # form = subject.new(model, {name: "name", count: 1,
180
- # # comments: [{author: "Joe", message: "Message 1"},
181
- # # {author: "Smith", message: "Message 2"}]},
182
- # # @mock_model_klass, @mock_builder_klass, @mock_errors, @mock_transaction)
183
- # # form.valid?
184
- # #end
185
- #end
186
-
187
- #describe "#save" do
188
- # before do
189
- # model = mock("ar_model")
190
- # model.stubs(comments: stub(build: mock("ar_comment_model")))
191
- # @mock_model_instance.stubs(:valid?).returns(true)
192
- # @form = subject.new(model, {name: "name", count: 1,
193
- # comments: [{author: "Joe", message: "Message 1"},
194
- # {author: "Smith", message: "Message 2"}]},
195
- # @mock_model_klass, @mock_builder_klass, @mock_errors, @mock_transaction)
196
- # end
197
- #
198
- # it "calls save on nested forms" do
199
- # Aform::Model.any_instance.expects(:save).returns(true).times(3)
200
- # @form.save
201
- # end
202
- #
203
- # it "calls valid? on nested forms" do
204
- # Aform::Model.any_instance.expects(:valid?).returns(false).times(3)
205
- # @form.save
206
- # end
207
- #end
208
124
  end
209
125
  end
210
126
  end
data/test/model_test.rb CHANGED
@@ -1,9 +1,19 @@
1
1
  require 'test_helper'
2
2
 
3
3
  describe Aform::Model do
4
- subject { Aform::Builder.new(Aform::Model).build_model_klass(fields, validations) }
4
+ subject { Aform::Model.build_klass(fields, validations) }
5
5
 
6
- let(:ar_model) { mock("ar_model") }
6
+ def mock_ar_model(attributes: {}, save: true)
7
+ model = OpenStruct.new(attributes: attributes, save: save)
8
+ model.define_singleton_method :assign_attributes do |attrs|
9
+ @table[:attributes] = attrs
10
+ end
11
+ model
12
+ end
13
+
14
+ def mock_form
15
+ OpenStruct.new
16
+ end
7
17
 
8
18
  context "validations" do
9
19
  context "by type" do
@@ -11,11 +21,11 @@ describe Aform::Model do
11
21
  let(:validations){ [{method: :validates_presence_of, options: [:name]}] }
12
22
 
13
23
  it "is not valid" do
14
- subject.new(ar_model).wont_be :valid?
24
+ subject.new(mock_ar_model, mock_form).wont_be :valid?
15
25
  end
16
26
 
17
27
  it "is valid" do
18
- subject.new(ar_model, name: "the name").must_be :valid?
28
+ subject.new(mock_ar_model, mock_form, {name: "the name"}).must_be :valid?
19
29
  end
20
30
  end
21
31
 
@@ -24,11 +34,11 @@ describe Aform::Model do
24
34
  let(:validations){ [{method: :validates, options: [:count, {presence: true, inclusion: {in: 1..100}}]}] }
25
35
 
26
36
  it "is not valid" do
27
- subject.new(ar_model, count: -1).wont_be :valid?
37
+ subject.new(mock_ar_model, mock_form, count: -1).wont_be :valid?
28
38
  end
29
39
 
30
40
  it "is valid" do
31
- subject.new(ar_model, name: "the name", count: 3).must_be :valid?
41
+ subject.new(mock_ar_model, mock_form, name: "the name", count: 3).must_be :valid?
32
42
  end
33
43
  end
34
44
 
@@ -42,42 +52,33 @@ describe Aform::Model do
42
52
  skip("not implemented")
43
53
  end
44
54
  end
45
-
46
- context "when marked for destruction" do
47
- let(:fields){ [{field: :name}, {field: :count}] }
48
- let(:validations){ [{method: :validates_presence_of, options: [:name]}] }
49
-
50
- it "is not valid" do
51
- subject.new(ar_model, _destroy: true).must_be :valid?
52
- end
53
- end
54
55
  end
55
56
 
56
57
  describe "#save" do
57
58
  let(:fields){ [{field: :name}, {field: :count}] }
58
59
  let(:validations){ [] }
60
+ let(:model) { mock_ar_model }
61
+ let(:form_model) { subject.new(model, mock_form, name: "name", count: 2, other_attr: "other")}
59
62
 
60
- let(:form_model) { subject.new(ar_model, name: "name", count: 2, other_attr: "other")}
61
-
62
- it "calls `ar_model.assign_attributes`" do
63
- ar_model.expects(:assign_attributes).with(name: "name", count: 2).returns(true)
64
- ar_model.stubs(:save)
63
+ it "assigns attributes to model" do
65
64
  form_model.save
65
+ model.attributes.must_equal({name: "name", count: 2})
66
66
  end
67
67
 
68
- it "calls `ar_model.save`" do
69
- ar_model.stubs(:assign_attributes).returns(true)
70
- ar_model.expects(:save).returns(true)
68
+ it "saves model" do
69
+ model.expects(:save)
71
70
  form_model.save
72
71
  end
73
72
 
74
73
  context "when keys are strings" do
75
- let(:form_model) { subject.new(ar_model, "name" => "name", "count" => 2, "other_attr" => "other")}
74
+ let(:form_model) { subject.new(model, mock_form,
75
+ "name" => "name",
76
+ "count" => 2,
77
+ "other_attr" => "other")}
76
78
 
77
- it "calls `ar_model.assign_attributes`" do
78
- ar_model.expects(:assign_attributes).with(name: "name", count: 2).returns(true)
79
- ar_model.stubs(:save)
79
+ it "calls assigns attributes" do
80
80
  form_model.save
81
+ model.attributes.must_equal(name: "name", count: 2)
81
82
  end
82
83
  end
83
84
 
@@ -85,42 +86,36 @@ describe Aform::Model do
85
86
  let(:fields){ [{field: :name}, {field: :count, options: {model_field: :size}}] }
86
87
 
87
88
  it "convert attributes" do
88
- ar_model.expects(:assign_attributes).with(name: "name", size: 2).returns(true)
89
- ar_model.stubs(:save)
90
89
  form_model.save
90
+ model.attributes.must_equal(name: "name", size: 2)
91
91
  end
92
92
  end
93
- end
94
93
 
95
- describe "#nested_save" do
96
- let(:fields){ [{field: :name}, {field: :count}] }
97
- let(:validations){ [] }
98
- let(:form_model) { subject.new(ar_model, name: "name", count: 2, other_attr: "other")}
94
+ context "when association is given" do
95
+ let(:association) {[]}
96
+ before do
97
+ form_model.save(association)
98
+ end
99
99
 
100
- it "calls `ar_model.assign_attributes`" do
101
- ar_model.expects(:assign_attributes).with(name: "name", count: 2).returns(true)
102
- ar_model.stubs(:persisted?).returns(false)
103
- form_model.nested_save
100
+ it "adds object to association" do
101
+ association.must_equal([model])
102
+ end
104
103
  end
104
+ end
105
105
 
106
- it "calls `ar_model.save` if persisted? is true" do
107
- ar_model.stubs(:assign_attributes).returns(true)
108
- ar_model.stubs(:persisted?).returns(true)
109
- ar_model.expects(:save).returns(true)
110
- form_model.nested_save
111
- end
106
+ context "when object for destroying" do
107
+ let(:fields){ [{field: :name}, {field: :count}] }
108
+ let(:validations){ [{method: :validates_presence_of, options: [:name]}] }
109
+ let(:model) { mock_ar_model }
110
+ let(:form_model) { subject.new(model, mock_form, count: 2, _destroy: true)}
112
111
 
113
- context "when marked for destruction" do
114
- let(:form_model) { subject.new(ar_model, name: "name", count: 2, _destroy: true)}
115
- it "removes element" do
116
- ar_model.expects(:destroy).returns(true)
117
- form_model.nested_save
118
- end
112
+ it "is valid" do
113
+ form_model.must_be :valid?
114
+ end
119
115
 
120
- it "calls `ar_model.assign_attributes`" do
121
- ar_model.expects(:assign_attributes).with(name: "name", count: 2).returns(true)
122
- form_model.nested_save
123
- end
116
+ it "assigns attributes" do
117
+ form_model.save
118
+ model.attributes.must_equal(count: 2)
124
119
  end
125
120
  end
126
121
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aform
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Versal
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-21 00:00:00.000000000 Z
11
+ date: 2014-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport