forminate 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +7 -2
- data/Gemfile +2 -0
- data/README.md +210 -4
- data/Rakefile +1 -1
- data/forminate.gemspec +21 -15
- data/lib/forminate/association_builder.rb +44 -0
- data/lib/forminate/association_definition.rb +22 -0
- data/lib/forminate/client_side_validations.rb +1 -1
- data/lib/forminate/version.rb +1 -1
- data/lib/forminate.rb +37 -77
- data/spec/forminate/association_builder_spec.rb +56 -0
- data/spec/forminate/association_definition_spec.rb +29 -0
- data/spec/{lib/forminate → forminate}/client_side_validations.rb +1 -1
- data/spec/{lib/forminate → forminate}/client_side_validations_spec.rb +10 -10
- data/spec/forminate_spec.rb +250 -0
- data/spec/spec_helper.rb +18 -0
- data/spec/support/dummy_book.rb +0 -2
- data/spec/support/dummy_credit_card.rb +0 -2
- data/spec/support/dummy_user.rb +2 -14
- metadata +62 -28
- data/spec/lib/forminate_spec.rb +0 -179
data/spec/lib/forminate_spec.rb
DELETED
@@ -1,179 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Forminate do
|
4
|
-
subject(:model) { model_class.new }
|
5
|
-
|
6
|
-
let :model_class do
|
7
|
-
Class.new do
|
8
|
-
include Forminate
|
9
|
-
|
10
|
-
attribute :total
|
11
|
-
attribute :tax
|
12
|
-
|
13
|
-
attributes_for :dummy_user
|
14
|
-
attributes_for :dummy_book, :validate => false
|
15
|
-
attributes_for :dummy_credit_card, :validate => :require_credit_card?
|
16
|
-
|
17
|
-
validates_numericality_of :total
|
18
|
-
|
19
|
-
def self.name
|
20
|
-
"Cart"
|
21
|
-
end
|
22
|
-
|
23
|
-
def calculate_total
|
24
|
-
self.total = dummy_book.price || 0.0
|
25
|
-
end
|
26
|
-
|
27
|
-
def require_credit_card?
|
28
|
-
dummy_book.price && dummy_book.price > 0.0
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
describe ".attributes_for" do
|
34
|
-
it "adds a reader method for each attribute of the associated model" do
|
35
|
-
expect(model.respond_to?(:dummy_user_first_name)).to be_true
|
36
|
-
end
|
37
|
-
|
38
|
-
it "adds reader and writer methods for each attribute of the associated model" do
|
39
|
-
model.dummy_user_first_name = 'Mo'
|
40
|
-
expect(model.dummy_user_first_name).to eq('Mo')
|
41
|
-
end
|
42
|
-
|
43
|
-
it "adds reader methods for each associated model" do
|
44
|
-
expect(model.dummy_user).to be_an_instance_of(DummyUser)
|
45
|
-
end
|
46
|
-
|
47
|
-
it "adds the association to the list of association names" do
|
48
|
-
expect(model_class.association_names).to include(:dummy_user)
|
49
|
-
end
|
50
|
-
|
51
|
-
describe ":validate option" do
|
52
|
-
it "validates associated object based on true or false" do
|
53
|
-
model.calculate_total
|
54
|
-
expect(model.valid?).to be_false
|
55
|
-
model.dummy_user_email = 'bob@example.com'
|
56
|
-
expect(model.valid?).to be_true
|
57
|
-
end
|
58
|
-
|
59
|
-
it "validates associated objects based on a method name (as a symbol) that evaluates to true or false" do
|
60
|
-
model.calculate_total
|
61
|
-
model.dummy_user_email = 'bob@example.com'
|
62
|
-
expect(model.valid?).to be_true
|
63
|
-
model.dummy_book_price = 12.95
|
64
|
-
expect(model.valid?).to be_false
|
65
|
-
model.dummy_credit_card_number = 4242424242424242
|
66
|
-
expect(model.valid?).to be_true
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
describe ".attribute_names" do
|
72
|
-
it "includes the names of its own attributes and the attributes of associated models" do
|
73
|
-
expected_attributes = [
|
74
|
-
"total",
|
75
|
-
"tax",
|
76
|
-
"dummy_user_first_name",
|
77
|
-
"dummy_user_last_name",
|
78
|
-
"dummy_user_email",
|
79
|
-
"dummy_book_title",
|
80
|
-
"dummy_book_price",
|
81
|
-
"dummy_credit_card_number",
|
82
|
-
"dummy_credit_card_expiration",
|
83
|
-
"dummy_credit_card_cvv",
|
84
|
-
]
|
85
|
-
expect(model_class.attribute_names).to eq(expected_attributes)
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
describe ".association_names" do
|
90
|
-
it "includes the names of associated models" do
|
91
|
-
expect(model_class.association_names).to eq([:dummy_user, :dummy_book, :dummy_credit_card])
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
describe ".association_validations" do
|
96
|
-
it "includes the names and conditions of association validations" do
|
97
|
-
expect(model_class.association_validations).to eq({ :dummy_user => true, :dummy_book => false, :dummy_credit_card => :require_credit_card? })
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
describe "#initialize" do
|
102
|
-
it "builds associated objects and creates reader methods" do
|
103
|
-
expect(model.dummy_user).to be_an_instance_of(DummyUser)
|
104
|
-
end
|
105
|
-
|
106
|
-
it "creates writer methods for associated objects" do
|
107
|
-
new_dummy_user = DummyUser.new(first_name: 'Mo')
|
108
|
-
expect(model.dummy_user).to_not be(new_dummy_user)
|
109
|
-
model.dummy_user = new_dummy_user
|
110
|
-
expect(model.dummy_user).to be(new_dummy_user)
|
111
|
-
end
|
112
|
-
|
113
|
-
it "sets association attributes based on an options hash" do
|
114
|
-
new_model = model_class.new(dummy_user_first_name: 'Mo', dummy_user_last_name: 'Lawson')
|
115
|
-
expect(new_model.dummy_user_first_name).to eq('Mo')
|
116
|
-
expect(new_model.dummy_user_last_name).to eq('Lawson')
|
117
|
-
end
|
118
|
-
|
119
|
-
it "sets attributes based on an options hash" do
|
120
|
-
new_model = model_class.new(total: 21.49)
|
121
|
-
expect(new_model.total).to eq(21.49)
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
describe "#association_names" do
|
126
|
-
it "delegates to self.association_names" do
|
127
|
-
expect(model.association_names).to eq(model_class.association_names)
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
describe "#associations" do
|
132
|
-
it "returns a hash of association names and associated objects" do
|
133
|
-
expect(model.associations[:dummy_user]).to be_an_instance_of(DummyUser)
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
describe "#save" do
|
138
|
-
context "object is valid" do
|
139
|
-
it "saves associations and returns self" do
|
140
|
-
model.dummy_user_email = 'bob@example.com'
|
141
|
-
model.calculate_total
|
142
|
-
DummyUser.any_instance.should_receive(:save)
|
143
|
-
expect(model.save).to eq(model)
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
context "object is not valid" do
|
148
|
-
it "does not save associations and returns false" do
|
149
|
-
DummyUser.any_instance.should_not_receive(:save)
|
150
|
-
expect(model.save).to be_false
|
151
|
-
end
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
context "setting an attribute using the attribute name" do
|
156
|
-
it "reflects the change on the associated object" do
|
157
|
-
model.dummy_user_first_name = 'Mo'
|
158
|
-
expect(model.dummy_user.first_name).to eq('Mo')
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
context "setting an attribute using the association's attribute" do
|
163
|
-
it "reflects the change on the attribute name" do
|
164
|
-
model.dummy_user.last_name = 'Lawson'
|
165
|
-
expect(model.dummy_user_last_name).to eq('Lawson')
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
it "delegates to attr_accessors of associated objects" do
|
170
|
-
model.dummy_user_full_name = 'Mo Lawson'
|
171
|
-
expect(model.dummy_user.full_name).to eq('Mo Lawson')
|
172
|
-
expect(model.dummy_user_full_name).to eq('Mo Lawson')
|
173
|
-
end
|
174
|
-
|
175
|
-
it "inherits the validations of its associated objects" do
|
176
|
-
model.valid?
|
177
|
-
expect(model.errors.full_messages).to eq(["Dummy user email can't be blank", "Total is not a number"])
|
178
|
-
end
|
179
|
-
end
|