active_type 0.6.0 → 0.6.1
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.
- checksums.yaml +4 -4
- data/README.md +21 -0
- data/active_type.gemspec +1 -1
- data/gemfiles/Gemfile.3.2.mysql2.lock +1 -1
- data/gemfiles/Gemfile.3.2.sqlite3.lock +1 -1
- data/gemfiles/Gemfile.4.0.sqlite3.lock +1 -1
- data/gemfiles/Gemfile.4.1.sqlite3.lock +1 -1
- data/gemfiles/Gemfile.4.2.1.mysql2.lock +1 -1
- data/gemfiles/Gemfile.4.2.1.pg.lock +1 -1
- data/gemfiles/Gemfile.4.2.1.sqlite3.lock +1 -1
- data/gemfiles/Gemfile.5.0.0.mysql2.lock +1 -1
- data/gemfiles/Gemfile.5.0.0.pg.lock +1 -1
- data/gemfiles/Gemfile.5.0.0.sqlite3.lock +1 -1
- data/lib/active_type/version.rb +1 -1
- metadata +3 -53
- data/spec/active_type/extended_record/single_table_inheritance_spec.rb +0 -62
- data/spec/active_type/extended_record_spec.rb +0 -233
- data/spec/active_type/nested_attributes_spec.rb +0 -704
- data/spec/active_type/object_spec.rb +0 -463
- data/spec/active_type/record_spec.rb +0 -274
- data/spec/active_type/util_spec.rb +0 -142
- data/spec/integration/holidays_spec.rb +0 -102
- data/spec/integration/shape_spec.rb +0 -110
- data/spec/integration/sign_in_spec.rb +0 -101
- data/spec/integration/sign_up_spec.rb +0 -102
- data/spec/shared_examples/accessors.rb +0 -24
- data/spec/shared_examples/belongs_to.rb +0 -17
- data/spec/shared_examples/coercible_columns.rb +0 -248
- data/spec/shared_examples/constructor.rb +0 -30
- data/spec/shared_examples/defaults.rb +0 -60
- data/spec/shared_examples/dirty_tracking.rb +0 -40
- data/spec/shared_examples/dupable.rb +0 -31
- data/spec/shared_examples/mass_assignment.rb +0 -26
- data/spec/spec_helper.rb +0 -28
- data/spec/support/database.rb +0 -53
- data/spec/support/database.sample.yml +0 -3
- data/spec/support/error_on.rb +0 -12
- data/spec/support/i18n.rb +0 -1
- data/spec/support/protected_params.rb +0 -20
- data/spec/support/time_zone.rb +0 -1
@@ -1,101 +0,0 @@
|
|
1
|
-
# Usecase: implement a sign in form
|
2
|
-
# The sign in is not tied to a database record
|
3
|
-
|
4
|
-
require 'spec_helper'
|
5
|
-
|
6
|
-
module SignInSpec
|
7
|
-
|
8
|
-
class SignIn < ActiveType::Object
|
9
|
-
attribute :email, :string
|
10
|
-
attribute :password, :string
|
11
|
-
|
12
|
-
validates :email, :presence => true
|
13
|
-
validates :password, :presence => true
|
14
|
-
|
15
|
-
validate :if => :password do |sign_in|
|
16
|
-
errors.add(:password, 'is not correct') unless sign_in.password == "correct password"
|
17
|
-
end
|
18
|
-
|
19
|
-
after_save :set_session
|
20
|
-
|
21
|
-
def set_session
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
describe SignInSpec::SignIn do
|
28
|
-
|
29
|
-
describe 'with missing credentials' do
|
30
|
-
|
31
|
-
it 'is invalid' do
|
32
|
-
expect(subject).not_to be_valid
|
33
|
-
end
|
34
|
-
|
35
|
-
it 'has errors' do
|
36
|
-
subject.valid?
|
37
|
-
expect(subject.errors[:email]).to eq(["can't be blank"])
|
38
|
-
expect(subject.errors[:password]).to eq(["can't be blank"])
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'does not save' do
|
42
|
-
expect(subject.save).to be_falsey
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'does not set the session' do
|
46
|
-
expect(subject).not_to receive :set_session
|
47
|
-
subject.save
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
describe 'with invalid credentials' do
|
53
|
-
|
54
|
-
before do
|
55
|
-
subject.email = "email"
|
56
|
-
subject.password = "incorrect password"
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'is invalid' do
|
60
|
-
expect(subject).not_to be_valid
|
61
|
-
end
|
62
|
-
|
63
|
-
it 'has errors' do
|
64
|
-
subject.valid?
|
65
|
-
expect(subject.errors[:password]).to eq(["is not correct"])
|
66
|
-
end
|
67
|
-
|
68
|
-
it 'does not save' do
|
69
|
-
expect(subject.save).to be_falsey
|
70
|
-
end
|
71
|
-
|
72
|
-
it 'does not set the session' do
|
73
|
-
expect(subject).not_to receive :set_session
|
74
|
-
subject.save
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
describe 'with valid credentials' do
|
80
|
-
|
81
|
-
before do
|
82
|
-
subject.email = "email"
|
83
|
-
subject.password = "correct password"
|
84
|
-
end
|
85
|
-
|
86
|
-
it 'is invalid' do
|
87
|
-
expect(subject).to be_valid
|
88
|
-
end
|
89
|
-
|
90
|
-
it 'does save' do
|
91
|
-
expect(subject.save).to eq(true)
|
92
|
-
end
|
93
|
-
|
94
|
-
it 'sets the session' do
|
95
|
-
expect(subject).to receive :set_session
|
96
|
-
subject.save
|
97
|
-
end
|
98
|
-
|
99
|
-
end
|
100
|
-
|
101
|
-
end
|
@@ -1,102 +0,0 @@
|
|
1
|
-
# Usecase: implement a sign up form
|
2
|
-
# The sign up is tied to a user model
|
3
|
-
|
4
|
-
|
5
|
-
require 'spec_helper'
|
6
|
-
|
7
|
-
ActiveRecord::Migration.class_eval do
|
8
|
-
create_table :users do |t|
|
9
|
-
t.string :email
|
10
|
-
t.string :password
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
|
15
|
-
module SignUpSpec
|
16
|
-
|
17
|
-
class User < ActiveType::Record
|
18
|
-
validates :email, :presence => true
|
19
|
-
validates :password, :presence => true
|
20
|
-
end
|
21
|
-
|
22
|
-
|
23
|
-
class SignUp < ActiveType::Record[User]
|
24
|
-
attribute :terms, :boolean
|
25
|
-
|
26
|
-
validates :terms, :acceptance => {:allow_nil => false, :accept => true}
|
27
|
-
|
28
|
-
after_create :send_welcome_email
|
29
|
-
|
30
|
-
def send_welcome_email
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
end
|
35
|
-
|
36
|
-
|
37
|
-
describe SignUpSpec::User do
|
38
|
-
|
39
|
-
it 'is valid without a password confirmation' do
|
40
|
-
subject.email = "email"
|
41
|
-
subject.password = "password"
|
42
|
-
|
43
|
-
expect(subject).to be_valid
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
|
49
|
-
describe SignUpSpec::SignUp do
|
50
|
-
|
51
|
-
it 'is invalid without an email' do
|
52
|
-
subject.password = "password"
|
53
|
-
subject.terms = true
|
54
|
-
|
55
|
-
expect(subject).not_to be_valid
|
56
|
-
expect(subject.errors['email']).to eq(["can't be blank"])
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'is invalid without accepted terms' do
|
60
|
-
subject.email = "email"
|
61
|
-
subject.password = "password"
|
62
|
-
|
63
|
-
expect(subject).not_to be_valid
|
64
|
-
expect(subject.errors['terms']).to eq(["must be accepted"])
|
65
|
-
end
|
66
|
-
|
67
|
-
context 'with invalid data' do
|
68
|
-
|
69
|
-
it 'does not save' do
|
70
|
-
expect(subject.save).to be_falsey
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'does not send an email' do
|
74
|
-
expect(subject).not_to receive :send_welcome_email
|
75
|
-
subject.save
|
76
|
-
end
|
77
|
-
|
78
|
-
end
|
79
|
-
|
80
|
-
context 'with valid data' do
|
81
|
-
|
82
|
-
before do
|
83
|
-
subject.email = "email"
|
84
|
-
subject.password = "password"
|
85
|
-
subject.terms = "1"
|
86
|
-
end
|
87
|
-
|
88
|
-
it 'does save' do
|
89
|
-
subject.valid?
|
90
|
-
expect(subject.save).to eq(true)
|
91
|
-
end
|
92
|
-
|
93
|
-
it 'sends the email' do
|
94
|
-
expect(subject).to receive :send_welcome_email
|
95
|
-
|
96
|
-
subject.save
|
97
|
-
end
|
98
|
-
|
99
|
-
end
|
100
|
-
|
101
|
-
|
102
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
shared_examples_for "ActiveRecord-like accessors" do |attributes|
|
2
|
-
|
3
|
-
it 'allows to read and write' do
|
4
|
-
attributes.each do |key, value|
|
5
|
-
subject.send("#{key}=", value)
|
6
|
-
expect(subject.send(key)).to eq(value)
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
it 'allows to read via []' do
|
11
|
-
attributes.each do |key, value|
|
12
|
-
subject.send("#{key}=", value)
|
13
|
-
expect(subject[key]).to eq(value)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'allows to write via []=' do
|
18
|
-
attributes.each do |key, value|
|
19
|
-
subject[key] = value
|
20
|
-
expect(subject.send(key)).to eq(value)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
shared_examples_for 'a belongs_to association' do |association, klass|
|
2
|
-
|
3
|
-
let(:record) { klass.create }
|
4
|
-
|
5
|
-
it 'sets the id when assigning a record' do
|
6
|
-
subject.send("#{association}=", record)
|
7
|
-
|
8
|
-
expect(subject.send("#{association}_id")).to eq(record.id)
|
9
|
-
end
|
10
|
-
|
11
|
-
it 'sets the record when assigning an id' do
|
12
|
-
subject.send("#{association}_id=", record.id)
|
13
|
-
|
14
|
-
expect(subject.send("#{association}")).to eq(record)
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
@@ -1,248 +0,0 @@
|
|
1
|
-
module TimeConversionSpec
|
2
|
-
class Record < ActiveRecord::Base
|
3
|
-
end
|
4
|
-
end
|
5
|
-
|
6
|
-
shared_examples_for 'a coercible string column' do |column|
|
7
|
-
|
8
|
-
it 'is nil by default' do
|
9
|
-
expect(subject.send(column)).to be_nil
|
10
|
-
end
|
11
|
-
|
12
|
-
it 'leaves strings alone' do
|
13
|
-
subject.send(:"#{column}=", "string")
|
14
|
-
|
15
|
-
expect(subject.send(column)).to eq("string")
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'does not convert blank' do
|
19
|
-
subject.send(:"#{column}=", "")
|
20
|
-
|
21
|
-
expect(subject.send(column)).to eq("")
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
|
27
|
-
shared_examples_for 'a coercible integer column' do |column|
|
28
|
-
|
29
|
-
it 'is nil by default' do
|
30
|
-
expect(subject.send(column)).to be_nil
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'leaves integers alone' do
|
34
|
-
subject.send(:"#{column}=", 10)
|
35
|
-
|
36
|
-
expect(subject.send(column)).to eq(10)
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'converts strings to integers' do
|
40
|
-
subject.send(:"#{column}=", "10")
|
41
|
-
|
42
|
-
expect(subject.send(column)).to eq(10)
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'converts blank to nil' do
|
46
|
-
subject.send(:"#{column}=", "")
|
47
|
-
|
48
|
-
expect(subject.send(column)).to be_nil
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
|
54
|
-
shared_examples_for 'a coercible date column' do |column|
|
55
|
-
|
56
|
-
it 'is nil by default' do
|
57
|
-
expect(subject.send(column)).to be_nil
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'leaves dates alone' do
|
61
|
-
date = Date.today
|
62
|
-
subject.send(:"#{column}=", date)
|
63
|
-
|
64
|
-
expect(subject.send(column)).to eq(date)
|
65
|
-
end
|
66
|
-
|
67
|
-
it 'converts strings to dates' do
|
68
|
-
subject.send(:"#{column}=", "2010-10-01")
|
69
|
-
|
70
|
-
expect(subject.send(column)).to eq(Date.new(2010, 10, 1))
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'converts blank to nil' do
|
74
|
-
subject.send(:"#{column}=", "")
|
75
|
-
|
76
|
-
expect(subject.send(column)).to be_nil
|
77
|
-
end
|
78
|
-
|
79
|
-
end
|
80
|
-
|
81
|
-
|
82
|
-
shared_examples_for 'a coercible time column' do |column|
|
83
|
-
|
84
|
-
around do |example|
|
85
|
-
begin
|
86
|
-
old_time_zone = Time.zone
|
87
|
-
old_time_zone_aware_attributes = ActiveRecord::Base.time_zone_aware_attributes
|
88
|
-
old_default_timezone = ActiveRecord::Base.default_timezone
|
89
|
-
example.run
|
90
|
-
ensure
|
91
|
-
Time.zone = old_time_zone
|
92
|
-
ActiveRecord::Base.time_zone_aware_attributes = old_time_zone_aware_attributes
|
93
|
-
ActiveRecord::Base.default_timezone = old_default_timezone
|
94
|
-
subject.class.reset_column_information
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
def it_should_convert_like_active_record(column)
|
99
|
-
time = "2010-10-01 12:15"
|
100
|
-
TimeConversionSpec::Record.reset_column_information
|
101
|
-
subject.class.reset_column_information
|
102
|
-
|
103
|
-
comparison = TimeConversionSpec::Record.new
|
104
|
-
subject.send(:"#{column}=", time)
|
105
|
-
comparison.persisted_time = time
|
106
|
-
|
107
|
-
result = subject.send(column)
|
108
|
-
expect(result).to eq(comparison.persisted_time)
|
109
|
-
expect(result.zone).to eq(comparison.persisted_time.zone)
|
110
|
-
end
|
111
|
-
|
112
|
-
|
113
|
-
it 'is nil by default' do
|
114
|
-
expect(subject.send(column)).to be_nil
|
115
|
-
end
|
116
|
-
|
117
|
-
it 'leaves times alone' do
|
118
|
-
time = Time.at(Time.now.to_i)
|
119
|
-
subject.send(:"#{column}=", time)
|
120
|
-
|
121
|
-
expect(subject.send(column)).to eq(time)
|
122
|
-
end
|
123
|
-
|
124
|
-
it 'converts strings to times' do
|
125
|
-
subject.send(:"#{column}=", "2010-10-01 12:15")
|
126
|
-
|
127
|
-
expect(subject.send(column)).to eq(Time.local(2010, 10, 1, 12, 15))
|
128
|
-
end
|
129
|
-
|
130
|
-
it 'behaves consistently with ActiveRecord' do
|
131
|
-
Time.zone = 'Hawaii'
|
132
|
-
|
133
|
-
it_should_convert_like_active_record(column)
|
134
|
-
end
|
135
|
-
|
136
|
-
it 'behaves consistently with ActiveRecord if time_zone_aware_attributes is set' do
|
137
|
-
Time.zone = 'Hawaii'
|
138
|
-
ActiveRecord::Base.time_zone_aware_attributes = true
|
139
|
-
|
140
|
-
it_should_convert_like_active_record(column)
|
141
|
-
end
|
142
|
-
|
143
|
-
it 'behaves consistently with ActiveRecord if default_timezone is :utc' do
|
144
|
-
Time.zone = 'Hawaii'
|
145
|
-
ActiveRecord::Base.default_timezone = :utc
|
146
|
-
|
147
|
-
it_should_convert_like_active_record(column)
|
148
|
-
end
|
149
|
-
|
150
|
-
it 'behaves consistently with ActiveRecord if time_zone_aware_attributes is set, default_timezone is :utc' do
|
151
|
-
Time.zone = 'Hawaii'
|
152
|
-
ActiveRecord::Base.default_timezone = :utc
|
153
|
-
ActiveRecord::Base.time_zone_aware_attributes = true
|
154
|
-
|
155
|
-
it_should_convert_like_active_record(column)
|
156
|
-
end
|
157
|
-
|
158
|
-
end
|
159
|
-
|
160
|
-
|
161
|
-
shared_examples_for 'a coercible boolean column' do |column|
|
162
|
-
|
163
|
-
it 'is nil by default' do
|
164
|
-
expect(subject.send(column)).to be_nil
|
165
|
-
end
|
166
|
-
|
167
|
-
it 'leaves booleans alone' do
|
168
|
-
subject.send(:"#{column}=", true)
|
169
|
-
|
170
|
-
expect(subject.send(column)).to eq(true)
|
171
|
-
end
|
172
|
-
|
173
|
-
it 'converts 1 to true' do
|
174
|
-
subject.send(:"#{column}=", "1")
|
175
|
-
|
176
|
-
expect(subject.send(column)).to eq(true)
|
177
|
-
end
|
178
|
-
|
179
|
-
it 'converts 0 to false' do
|
180
|
-
subject.send(:"#{column}=", "0")
|
181
|
-
|
182
|
-
expect(subject.send(column)).to eq(false)
|
183
|
-
end
|
184
|
-
|
185
|
-
it 'converts "" to nil' do
|
186
|
-
subject.send(:"#{column}=", "")
|
187
|
-
|
188
|
-
expect(subject.send(column)).to be_nil
|
189
|
-
end
|
190
|
-
|
191
|
-
it 'converts "true" to true' do
|
192
|
-
subject.send(:"#{column}=", "true")
|
193
|
-
|
194
|
-
expect(subject.send(column)).to eq(true)
|
195
|
-
end
|
196
|
-
|
197
|
-
it 'converts "false" to false' do
|
198
|
-
subject.send(:"#{column}=", "false")
|
199
|
-
|
200
|
-
expect(subject.send(column)).to eq(false)
|
201
|
-
end
|
202
|
-
|
203
|
-
end
|
204
|
-
|
205
|
-
shared_examples_for 'an untyped column' do |column|
|
206
|
-
it 'is nil by default' do
|
207
|
-
expect(subject.send(column)).to be_nil
|
208
|
-
end
|
209
|
-
|
210
|
-
it 'leaves strings alone' do
|
211
|
-
subject.send(:"#{column}=", "string")
|
212
|
-
|
213
|
-
expect(subject.send(column)).to eq("string")
|
214
|
-
end
|
215
|
-
|
216
|
-
it 'leaves integers alone' do
|
217
|
-
subject.send(:"#{column}=", 17)
|
218
|
-
|
219
|
-
expect(subject.send(column)).to eq(17)
|
220
|
-
end
|
221
|
-
|
222
|
-
it 'leaves objects alone' do
|
223
|
-
object = Object.new
|
224
|
-
subject.send(:"#{column}=", object)
|
225
|
-
|
226
|
-
expect(subject.send(column)).to eq(object)
|
227
|
-
end
|
228
|
-
end
|
229
|
-
|
230
|
-
|
231
|
-
shared_examples_for 'a coercible type column' do |column, type|
|
232
|
-
|
233
|
-
if type
|
234
|
-
|
235
|
-
it 'is nil by default' do
|
236
|
-
expect(subject.send(column)).to be_nil
|
237
|
-
end
|
238
|
-
|
239
|
-
it 'leaves strings alone' do
|
240
|
-
expect(type).to receive(:cast).with('input').and_return('output')
|
241
|
-
subject.send(:"#{column}=", 'input')
|
242
|
-
|
243
|
-
expect(subject.send(column)).to eq('output')
|
244
|
-
end
|
245
|
-
|
246
|
-
end
|
247
|
-
|
248
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
shared_examples_for 'ActiveRecord-like constructors' do |attributes|
|
2
|
-
|
3
|
-
it 'return a new record' do
|
4
|
-
expect(subject.new).to be_new_record
|
5
|
-
end
|
6
|
-
|
7
|
-
it 'assigns given attributes' do
|
8
|
-
record = subject.new(attributes)
|
9
|
-
|
10
|
-
attributes.each do |key, value|
|
11
|
-
expect(record.send(key)).to eq(value)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
if ActiveRecord::VERSION::MAJOR >= 4
|
16
|
-
|
17
|
-
it 'raises on unpermitted parameters' do
|
18
|
-
params = ProtectedParams.new(attributes)
|
19
|
-
expect { subject.new(params) }.to raise_error(ActiveModel::ForbiddenAttributesError)
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'accepts permitted parameters' do
|
23
|
-
params = ProtectedParams.new(attributes)
|
24
|
-
params.permit!
|
25
|
-
expect { subject.new(params) }.to_not raise_error
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
shared_examples_for "a class accepting attribute defaults" do |klass|
|
2
|
-
|
3
|
-
subject do
|
4
|
-
Class.new(klass) do
|
5
|
-
attribute :static_string, :string, :default => "static string"
|
6
|
-
attribute :dynamic_string, :string, :default => proc { "dynamic string" }
|
7
|
-
attribute :referential_string, :string, :default => proc { value }
|
8
|
-
attribute :number, :integer, :default => "10"
|
9
|
-
attribute :computed, :default => proc { compute }
|
10
|
-
|
11
|
-
def value
|
12
|
-
"value"
|
13
|
-
end
|
14
|
-
|
15
|
-
end.new
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'can have static defaults' do
|
19
|
-
expect(subject.static_string).to eq("static string")
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'can have dynamic defaults' do
|
23
|
-
expect(subject.dynamic_string).to eq("dynamic string")
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'can have defaults refering to instance methods' do
|
27
|
-
expect(subject.referential_string).to eq("value")
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'typecasts defaults' do
|
31
|
-
expect(subject.number).to eq(10)
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'computes defaults lazily' do
|
35
|
-
expect(subject).to receive(:compute).and_return("computed")
|
36
|
-
expect(subject.computed).to eq("computed")
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'does not compute defaults more than once' do
|
40
|
-
expect(subject).to receive(:compute).exactly(:once).and_return(nil)
|
41
|
-
subject.computed
|
42
|
-
subject.computed
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'does not compute defaults when overriden' do
|
46
|
-
subject.computed = 'not computed'
|
47
|
-
expect(subject.computed).to eq('not computed')
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'does not use defaults when overriden' do
|
51
|
-
subject.static_string = "my string"
|
52
|
-
expect(subject.static_string).to eq("my string")
|
53
|
-
end
|
54
|
-
|
55
|
-
it 'does not use defaults when overriden with nil' do
|
56
|
-
subject.static_string = nil
|
57
|
-
expect(subject.static_string).to eq(nil)
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
shared_examples_for "a class supporting dirty tracking for virtual attributes" do |klass|
|
2
|
-
|
3
|
-
subject do
|
4
|
-
Class.new(klass) do
|
5
|
-
attribute :virtual_attribute
|
6
|
-
end.new
|
7
|
-
end
|
8
|
-
|
9
|
-
describe '#virtual_attribute_was' do
|
10
|
-
|
11
|
-
it 'always returns nil, since there can be no previously saved value' do
|
12
|
-
expect(subject.virtual_attribute_was).to be_nil
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
describe '#virtual_attribute_changed?' do
|
18
|
-
|
19
|
-
it 'returns true if the attribute is not nil' do
|
20
|
-
subject.virtual_attribute = 'foo'
|
21
|
-
expect(subject.virtual_attribute_changed?).to eq(true)
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'returns false if the attribute is nil' do
|
25
|
-
subject.virtual_attribute = nil
|
26
|
-
expect(subject.virtual_attribute_changed?).to be_falsey
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
describe '#virtual_attribute_will_change!' do
|
32
|
-
|
33
|
-
it 'is implemented for compatibility with ActiveModel::Dirty, but does nothing' do
|
34
|
-
expect(subject).to respond_to(:virtual_attribute_will_change!)
|
35
|
-
expect { subject.virtual_attribute_will_change! }.to_not raise_error
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
shared_examples_for "a class supporting dup for attributes" do |klass|
|
2
|
-
|
3
|
-
subject do
|
4
|
-
Class.new(klass) do
|
5
|
-
attribute :attribute
|
6
|
-
end.new
|
7
|
-
end
|
8
|
-
|
9
|
-
describe '#dup' do
|
10
|
-
|
11
|
-
it 'returns an object with independent attributes' do
|
12
|
-
subject.attribute = "foo"
|
13
|
-
duped = subject.dup
|
14
|
-
duped.attribute = "bar"
|
15
|
-
|
16
|
-
expect(subject.attribute).to eq("foo")
|
17
|
-
expect(duped.attribute).to eq("bar")
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'does a deep copy' do
|
21
|
-
subject.attribute = { :foo => "bar" }
|
22
|
-
duped = subject.dup
|
23
|
-
duped.attribute.merge!(:foo => "baz")
|
24
|
-
|
25
|
-
expect(subject.attribute).to eq({ :foo => "bar" })
|
26
|
-
expect(duped.attribute).to eq({ :foo => "baz" })
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
shared_examples_for 'ActiveRecord-like mass assignment' do |attributes|
|
2
|
-
|
3
|
-
it 'assigns all given attributes' do
|
4
|
-
subject.attributes = attributes
|
5
|
-
|
6
|
-
attributes.each do |key, value|
|
7
|
-
expect(subject.send(key)).to eq(value)
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
if ActiveRecord::VERSION::MAJOR >= 4
|
12
|
-
|
13
|
-
it 'raises on unpermitted parameters' do
|
14
|
-
params = ProtectedParams.new(attributes)
|
15
|
-
expect { subject.attributes = params }.to raise_error(ActiveModel::ForbiddenAttributesError)
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'accepts permitted parameters' do
|
19
|
-
params = ProtectedParams.new(attributes)
|
20
|
-
params.permit!
|
21
|
-
expect { subject.attributes = params }.to_not raise_error
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|