attr_default 0.5.3 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/attr_default/version.rb +1 -1
- data/lib/attr_default.rb +1 -1
- data/test/test_attr_default.rb +60 -49
- metadata +3 -3
data/lib/attr_default/version.rb
CHANGED
data/lib/attr_default.rb
CHANGED
data/test/test_attr_default.rb
CHANGED
@@ -28,15 +28,15 @@ ActiveRecord::Base.establish_connection(
|
|
28
28
|
ActiveRecord::Base.connection.create_table(:test_users, :force => true) do |t|
|
29
29
|
t.string :first_name, :default => ''
|
30
30
|
t.string :last_name
|
31
|
-
t.
|
31
|
+
t.string :domain, :default => 'example.com'
|
32
32
|
t.timestamp :timestamp
|
33
33
|
end
|
34
34
|
|
35
|
-
ActiveRecord::Base.connection.create_table(:
|
35
|
+
ActiveRecord::Base.connection.create_table(:test_domains, :force => true) do |t|
|
36
36
|
t.string :type
|
37
37
|
t.integer :test_user_id
|
38
|
-
t.
|
39
|
-
t.
|
38
|
+
t.string :domain, :default => 'domain.com'
|
39
|
+
t.string :path
|
40
40
|
t.timestamp :created_at
|
41
41
|
end
|
42
42
|
|
@@ -53,37 +53,41 @@ class TestUser < ActiveRecord::Base
|
|
53
53
|
fields do
|
54
54
|
first_name :string, :default => '', :ruby_default => lambda { 'John' }
|
55
55
|
last_name :string, :ruby_default => 'Doe'
|
56
|
+
domain :string, :ruby_default => 'default.com'
|
56
57
|
timestamp :timestamp, :default => lambda { (Time.zone || ActiveSupport::TimeZone['Pacific Time (US & Canada)']).now }
|
57
58
|
end
|
58
59
|
else
|
59
60
|
attr_default :first_name, 'John'
|
60
61
|
attr_default :last_name, 'Doe'
|
62
|
+
attr_default :domain, 'default.com'
|
61
63
|
attr_default :timestamp, lambda { (Time.zone || ActiveSupport::TimeZone['Pacific Time (US & Canada)']).now }
|
62
64
|
end
|
63
65
|
|
64
|
-
has_many :
|
65
|
-
has_many :
|
66
|
+
has_many :test_domains
|
67
|
+
has_many :test_domains_subclass, :class_name => 'TestDomainSubclass'
|
66
68
|
end
|
67
69
|
|
68
|
-
class
|
70
|
+
class TestDomain < ActiveRecord::Base
|
69
71
|
if ENV['INCLUDE_HOBO']
|
70
72
|
fields do
|
71
|
-
|
73
|
+
domain :string, :default => lambda { test_user.domain }
|
74
|
+
path :string, :ruby_default => "/path"
|
72
75
|
end
|
73
76
|
else
|
74
|
-
attr_default :
|
77
|
+
attr_default :domain, lambda { test_user.domain }
|
78
|
+
attr_default :path, "/path"
|
75
79
|
end
|
76
80
|
|
77
81
|
belongs_to :test_user
|
78
82
|
end
|
79
83
|
|
80
|
-
class
|
84
|
+
class TestDomainSubclass < TestDomain
|
81
85
|
if ENV['INCLUDE_HOBO']
|
82
86
|
fields do
|
83
|
-
|
87
|
+
domain :string, :default => lambda { "sub_#{test_user.domain}" }
|
84
88
|
end
|
85
89
|
else
|
86
|
-
attr_default :
|
90
|
+
attr_default :domain, lambda { "sub_#{test_user.domain}" }
|
87
91
|
end
|
88
92
|
end
|
89
93
|
|
@@ -137,11 +141,11 @@ class AttrDefaultTest < Test::Unit::TestCase
|
|
137
141
|
end
|
138
142
|
|
139
143
|
def test_nonproc_default_value_for_string_and_symbol
|
140
|
-
u = TestUser.new(:
|
144
|
+
u = TestUser.new(:domain => "initial.com")
|
141
145
|
u.save!
|
142
|
-
assert_equal
|
143
|
-
assert_equal
|
144
|
-
assert_equal
|
146
|
+
assert_equal "initial.com", u.domain
|
147
|
+
assert_equal "default.com", u.default_value_for("domain")
|
148
|
+
assert_equal "default.com", u.default_value_for(:domain)
|
145
149
|
end
|
146
150
|
|
147
151
|
def test_reset_to_default_value_string
|
@@ -185,13 +189,13 @@ class AttrDefaultTest < Test::Unit::TestCase
|
|
185
189
|
end
|
186
190
|
|
187
191
|
def test_use_default_when_saved_if_not_touched
|
188
|
-
user = TestUser.create! :
|
189
|
-
|
192
|
+
user = TestUser.create! :domain => "initial.com"
|
193
|
+
domain = user.test_domains.build
|
190
194
|
|
191
|
-
|
192
|
-
|
193
|
-
assert_equal
|
194
|
-
assert_equal
|
195
|
+
domain.save!
|
196
|
+
domain.reload
|
197
|
+
assert_equal "initial.com", domain.read_attribute(:domain)
|
198
|
+
assert_equal "initial.com", domain.domain
|
195
199
|
end
|
196
200
|
|
197
201
|
def test_clone_touched_state_when_cloned_before_save_new_record_true
|
@@ -222,46 +226,54 @@ class AttrDefaultTest < Test::Unit::TestCase
|
|
222
226
|
end
|
223
227
|
|
224
228
|
def test_use_default_when_saved_if_not_touched_and_validation_turned_off
|
225
|
-
user = TestUser.create! :
|
226
|
-
|
229
|
+
user = TestUser.create! :domain => "initial.com"
|
230
|
+
domain = user.test_domains.build
|
227
231
|
|
228
|
-
# not touched or saved yet, still
|
229
|
-
assert_equal
|
232
|
+
# not touched or saved yet, still SQL default
|
233
|
+
assert_equal "domain.com", domain.read_attribute(:domain)
|
230
234
|
|
231
|
-
|
235
|
+
domain.save(false)
|
232
236
|
|
233
|
-
# now it should be
|
234
|
-
assert_equal
|
235
|
-
assert_equal
|
237
|
+
# now it should be set to Ruby default
|
238
|
+
assert_equal "initial.com", domain.read_attribute(:domain)
|
239
|
+
assert_equal "initial.com", domain.domain
|
236
240
|
end
|
237
241
|
|
238
242
|
def test_use_value_set_on_object_even_when_first_loaded_from_db
|
239
|
-
user = TestUser.create! :
|
240
|
-
|
241
|
-
number_find =
|
242
|
-
assert_equal
|
243
|
+
user = TestUser.create! :domain => "initial.com"
|
244
|
+
domain = user.test_domains.create! :domain => "domain.initial.com"
|
245
|
+
number_find = TestDomain.find(domain.id)
|
246
|
+
assert_equal "domain.initial.com", number_find.domain
|
243
247
|
end
|
244
248
|
|
245
|
-
[
|
246
|
-
define_method "test_default_#{
|
247
|
-
user = TestUser.create! :
|
248
|
-
|
249
|
-
assert_equal
|
249
|
+
['example.com', 'domain.com', 'default.com'].each do |user_domain|
|
250
|
+
define_method "test_default_#{user_domain}_and_param_not_specified" do
|
251
|
+
user = TestUser.create! :domain => user_domain
|
252
|
+
domain = user.test_domains.build
|
253
|
+
assert_equal user_domain, domain.domain
|
250
254
|
end
|
251
255
|
|
252
|
-
define_method "test_default_#{
|
253
|
-
user = TestUser.create! :
|
254
|
-
|
255
|
-
assert_equal
|
256
|
+
define_method "test_default_#{user_domain}_and_other_specified" do
|
257
|
+
user = TestUser.create! :domain => user_domain
|
258
|
+
domain = user.test_domains.build :domain => "override.com"
|
259
|
+
assert_equal "override.com", domain.domain
|
256
260
|
end
|
257
261
|
end
|
258
262
|
|
259
263
|
def test_allow_subclass_to_override_the_default
|
260
|
-
user = TestUser.create! :
|
261
|
-
|
262
|
-
assert_equal
|
263
|
-
|
264
|
-
assert_equal
|
264
|
+
user = TestUser.create! :domain => 'initial.com'
|
265
|
+
domain = user.test_domains.new
|
266
|
+
assert_equal 'initial.com', domain.domain
|
267
|
+
domain_subclass = user.test_domains_subclass.new
|
268
|
+
assert_equal 'sub_initial.com', domain_subclass.domain
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_subclass_uses_base_class_default
|
272
|
+
user = TestUser.create! :domain => 'initial.com'
|
273
|
+
domain = user.test_domains.new
|
274
|
+
assert_equal '/path', domain.path
|
275
|
+
domain_subclass = user.test_domains_subclass.new
|
276
|
+
assert_equal '/path', domain_subclass.path
|
265
277
|
end
|
266
278
|
|
267
279
|
def test_non_persistent_use_default_if_not_set
|
@@ -280,4 +292,3 @@ class AttrDefaultTest < Test::Unit::TestCase
|
|
280
292
|
assert_equal "supersecret", user.password
|
281
293
|
end
|
282
294
|
end
|
283
|
-
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attr_default
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 5
|
9
|
-
-
|
10
|
-
version: 0.5.
|
9
|
+
- 4
|
10
|
+
version: 0.5.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Colin Kelley
|