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.
@@ -1,3 +1,3 @@
1
1
  module AttrDefault
2
- VERSION = "0.5.3"
2
+ VERSION = "0.5.4"
3
3
  end
data/lib/attr_default.rb CHANGED
@@ -38,7 +38,7 @@ module AttrDefault
38
38
  end
39
39
 
40
40
  def _attr_defaults
41
- @_attr_defaults ||= {}
41
+ @_attr_defaults ||= (ancestors[1]._attr_defaults.dup rescue nil) || {}
42
42
  end
43
43
  end
44
44
 
@@ -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.boolean :managed, :default => false
31
+ t.string :domain, :default => 'example.com'
32
32
  t.timestamp :timestamp
33
33
  end
34
34
 
35
- ActiveRecord::Base.connection.create_table(:test_numbers, :force => true) do |t|
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.integer :number
39
- t.boolean :managed
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 :test_numbers
65
- has_many :test_numbers_subclass, :class_name => 'TestNumberSubclass'
66
+ has_many :test_domains
67
+ has_many :test_domains_subclass, :class_name => 'TestDomainSubclass'
66
68
  end
67
69
 
68
- class TestNumber < ActiveRecord::Base
70
+ class TestDomain < ActiveRecord::Base
69
71
  if ENV['INCLUDE_HOBO']
70
72
  fields do
71
- managed :boolean, :default => lambda { test_user.managed }
73
+ domain :string, :default => lambda { test_user.domain }
74
+ path :string, :ruby_default => "/path"
72
75
  end
73
76
  else
74
- attr_default :managed, lambda { test_user.managed }
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 TestNumberSubclass < TestNumber
84
+ class TestDomainSubclass < TestDomain
81
85
  if ENV['INCLUDE_HOBO']
82
86
  fields do
83
- managed :boolean, :default => lambda { false }
87
+ domain :string, :default => lambda { "sub_#{test_user.domain}" }
84
88
  end
85
89
  else
86
- attr_default :managed, lambda { false }
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(:managed => true)
144
+ u = TestUser.new(:domain => "initial.com")
141
145
  u.save!
142
- assert_equal true, u.managed
143
- assert_equal false, u.default_value_for("managed")
144
- assert_equal false, u.default_value_for(:managed)
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! :managed => true
189
- number = user.test_numbers.build
192
+ user = TestUser.create! :domain => "initial.com"
193
+ domain = user.test_domains.build
190
194
 
191
- number.save!
192
- number.reload
193
- assert_equal true, number.read_attribute(:managed)
194
- assert_equal true, number.managed
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! :managed => true
226
- number = user.test_numbers.build :number => 42
229
+ user = TestUser.create! :domain => "initial.com"
230
+ domain = user.test_domains.build
227
231
 
228
- # not touched or saved yet, still empty
229
- assert_equal nil, number.read_attribute(:managed)
232
+ # not touched or saved yet, still SQL default
233
+ assert_equal "domain.com", domain.read_attribute(:domain)
230
234
 
231
- number.save(false)
235
+ domain.save(false)
232
236
 
233
- # now it should be true
234
- assert_equal true, number.read_attribute(:managed)
235
- assert_equal true, number.managed
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! :managed => true
240
- number = user.test_numbers.create! :number => 42, :managed => false
241
- number_find = TestNumber.find(number.id)
242
- assert_equal false, number_find.managed
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
- [false, true].each do |user_managed|
246
- define_method "test_default_#{user_managed}_and_param_not_specified" do
247
- user = TestUser.create! :managed => user_managed
248
- number = user.test_numbers.build
249
- assert_equal user_managed, number.managed
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_#{user_managed}_and_#{!user_managed}_specified" do
253
- user = TestUser.create! :managed => true
254
- number = user.test_numbers.build :managed => !user_managed
255
- assert_equal !user_managed, number.managed
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! :managed => true
261
- number = user.test_numbers.new
262
- assert_equal true, number.managed
263
- number_subclass = user.test_numbers_subclass.new
264
- assert_equal false, number_subclass.managed
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: 13
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 3
10
- version: 0.5.3
9
+ - 4
10
+ version: 0.5.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Colin Kelley