default_value_for 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{default_value_for}
3
- s.version = "1.0.6"
3
+ s.version = "1.0.7"
4
4
  s.summary = %q{Provides a way to specify default values for ActiveRecord models}
5
5
  s.description = %q{The default_value_for plugin allows one to define default values for ActiveRecord models in a declarative manner}
6
6
  s.email = %q{info@phusion.nl}
@@ -57,6 +57,10 @@ module DefaultValueFor
57
57
  else
58
58
  class_inheritable_accessor :_default_attribute_values
59
59
  end
60
+ extend(DelayedClassMethods)
61
+ init_hash = true
62
+ end
63
+ if init_hash || !singleton_methods(false).to_s.include?("_default_attribute_values")
60
64
  self._default_attribute_values = ActiveSupport::OrderedHash.new
61
65
  end
62
66
  if block_given?
@@ -78,6 +82,13 @@ module DefaultValueFor
78
82
  end
79
83
  end
80
84
 
85
+ module DelayedClassMethods
86
+ def _all_default_attribute_values
87
+ return _default_attribute_values unless superclass.respond_to?(:_default_attribute_values)
88
+ superclass._all_default_attribute_values.merge(_default_attribute_values)
89
+ end
90
+ end
91
+
81
92
  module InstanceMethods
82
93
  def initialize_with_defaults(attrs = nil, *args, &block)
83
94
  initialize_without_defaults(attrs, *args, &block)
@@ -92,7 +103,7 @@ module DefaultValueFor
92
103
  x.to_s
93
104
  end
94
105
  end
95
- self.class._default_attribute_values.each do |attribute, container|
106
+ self.class._all_default_attribute_values.each do |attribute, container|
96
107
  if safe_attribute_names.nil? || !safe_attribute_names.any? { |attr_name| attr_name =~ /^#{attribute}($|\()/ }
97
108
  __send__("#{attribute}=", container.evaluate(self))
98
109
  changed_attributes.delete(attribute)
data/test.rb CHANGED
@@ -168,6 +168,68 @@ class DefaultValuePluginTest < Test::Unit::TestCase
168
168
  assert_equal 1234, object.number
169
169
  end
170
170
 
171
+ def test_default_values_in_subclass
172
+ define_model_class("TestSuperClass") do
173
+ end
174
+ define_model_class("TestClass", "TestSuperClass") do
175
+ default_value_for :number, 5678
176
+ end
177
+
178
+ object = TestClass.new
179
+ assert_equal 5678, object.number
180
+
181
+ object = TestSuperClass.new
182
+ assert_nil object.number
183
+ end
184
+
185
+ def test_multiple_default_values_in_subclass_with_default_values_in_parent_class
186
+ define_model_class("TestSuperClass") do
187
+ default_value_for :other_number, nil
188
+ attr_accessor :other_number
189
+ end
190
+ define_model_class("TestClass", "TestSuperClass") do
191
+ default_value_for :number, 5678
192
+
193
+ # Ensure second call in this class doesn't reset _default_attribute_values,
194
+ # and also doesn't consider the parent class' _default_attribute_values when
195
+ # making that check.
196
+ default_value_for :user_id, 9999
197
+ end
198
+
199
+ object = TestClass.new
200
+ assert_nil object.other_number
201
+ assert_equal 5678, object.number
202
+ assert_equal 9999, object.user_id
203
+ end
204
+
205
+ def test_override_default_values_in_subclass
206
+ define_model_class("TestSuperClass") do
207
+ default_value_for :number, 1234
208
+ end
209
+ define_model_class("TestClass", "TestSuperClass") do
210
+ default_value_for :number, 5678
211
+ end
212
+
213
+ object = TestClass.new
214
+ assert_equal 5678, object.number
215
+
216
+ object = TestSuperClass.new
217
+ assert_equal 1234, object.number
218
+ end
219
+
220
+ def test_default_values_in_subclass_do_not_affect_parent_class
221
+ define_model_class("TestSuperClass") do
222
+ default_value_for :number, 1234
223
+ end
224
+ define_model_class("TestClass", "TestSuperClass") do
225
+ default_value_for :hello, "hi"
226
+ attr_accessor :hello
227
+ end
228
+
229
+ assert_nothing_raised { TestSuperClass.new }
230
+ assert !TestSuperClass._default_attribute_values.include?(:hello)
231
+ end
232
+
171
233
  def test_doesnt_set_default_on_saved_records
172
234
  define_model_class do
173
235
  default_value_for :number, 1234
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: default_value_for
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 6
10
- version: 1.0.6
9
+ - 7
10
+ version: 1.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Hongli Lai
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-08 00:00:00 +02:00
18
+ date: 2011-12-02 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21