default_value_for 3.0.0.1 → 3.0.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 +8 -8
- data/README.md +1 -1
- data/Rakefile +1 -1
- data/default_value_for.gemspec +3 -1
- data/lib/default_value_for.rb +18 -4
- data/lib/default_value_for/railtie.rb +0 -2
- data/test.rb +168 -242
- metadata +30 -2
- checksums.yaml.gz.asc +0 -12
- data.tar.gz.asc +0 -12
- metadata.gz.asc +0 -12
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZmIyZjZlMTY4ZmZkYjhkMmQ4NDBjYmVjNjU5Mjc1YzE4N2U1NThlMw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NTU2M2Y3MTM4OGYwOTMzZTUwNWM1NmVlMmYxNzRlMTAwOTY5MWJhMw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YTMxYWQ0MzBlZGVkNGExNzI0NGY0ODBmOGQyNzI1OWUyOTZhYzA3MzBiMWRi
|
10
|
+
YTIzYTZlMzNkNWFjZmJmMjA3ZWI3ODg0MGY2NWRiNzNkMGM2NGUxOTU5ODU0
|
11
|
+
MjVjYzAzZjQ0MWViYWM5OTA3MWZmOTU4YjdhMTZlNDZkMzA1ZGM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZjQwNDIwOTJmNTFhMmQ2YjljYTdjMmZkNjk0MjAzY2JhZTI0MDkxY2JiM2Q3
|
14
|
+
Y2ExOTA2MjM4ZWE4YjQ4NzMzZWM5OGU1ZGVjYjJmMjMwZDkxY2Q2ODFkNTFj
|
15
|
+
NzcyNDAxMWI0ZTE1M2NkNjEwYmI5OWRiY2FmODhiZWI1Y2M4MTQ=
|
data/README.md
CHANGED
@@ -20,7 +20,7 @@ u.last_seen # => Mon Sep 22 17:28:38 +0200 2008
|
|
20
20
|
|
21
21
|
## Installation
|
22
22
|
|
23
|
-
### Rails 3.2 - 4.
|
23
|
+
### Rails 3.2 - 4.2 / Ruby 1.9.3 and higher
|
24
24
|
|
25
25
|
The current version of default_value_for (3.0.x) is compatible with Rails 3.2 or higher, and Ruby 1.9.3 and higher.
|
26
26
|
|
data/Rakefile
CHANGED
data/default_value_for.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = %q{default_value_for}
|
3
|
-
s.version = "3.0.
|
3
|
+
s.version = "3.0.1"
|
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{software-signing@phusion.nl}
|
@@ -17,4 +17,6 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.add_dependency 'activerecord', '>= 3.2.0', '< 5.0'
|
18
18
|
s.add_development_dependency 'railties', '>= 3.2.0', '< 5.0'
|
19
19
|
s.add_development_dependency 'minitest', '>= 4.2'
|
20
|
+
s.add_development_dependency 'minitest-around'
|
21
|
+
s.add_development_dependency 'appraisal'
|
20
22
|
end
|
data/lib/default_value_for.rb
CHANGED
@@ -144,7 +144,9 @@ module DefaultValueFor
|
|
144
144
|
end
|
145
145
|
|
146
146
|
def attributes_for_create(attribute_names)
|
147
|
-
attribute_names +=
|
147
|
+
attribute_names += self.class._all_default_attribute_values.keys.map(&:to_s).find_all { |name|
|
148
|
+
self.class.columns_hash.key?(name)
|
149
|
+
}
|
148
150
|
super
|
149
151
|
end
|
150
152
|
|
@@ -163,10 +165,22 @@ module DefaultValueFor
|
|
163
165
|
next unless connection_default_value_defined || attribute_blank
|
164
166
|
|
165
167
|
# allow explicitly setting nil through allow nil option
|
166
|
-
next if @initialization_attributes.is_a?(Hash) &&
|
168
|
+
next if @initialization_attributes.is_a?(Hash) &&
|
169
|
+
(
|
170
|
+
@initialization_attributes.has_key?(attribute) ||
|
171
|
+
(
|
172
|
+
@initialization_attributes.has_key?("#{attribute}_attributes") &&
|
173
|
+
nested_attributes_options.stringify_keys[attribute]
|
174
|
+
)
|
175
|
+
) &&
|
176
|
+
!self.class._all_default_attribute_values_not_allowing_nil.include?(attribute)
|
167
177
|
|
168
178
|
__send__("#{attribute}=", container.evaluate(self))
|
169
|
-
|
179
|
+
if self.class.private_instance_methods.include? :clear_attribute_changes
|
180
|
+
clear_attribute_changes attribute
|
181
|
+
else
|
182
|
+
changed_attributes.delete(attribute)
|
183
|
+
end
|
170
184
|
end
|
171
185
|
end
|
172
186
|
end
|
@@ -175,6 +189,6 @@ end
|
|
175
189
|
if defined?(Rails::Railtie)
|
176
190
|
require 'default_value_for/railtie'
|
177
191
|
else
|
178
|
-
#
|
192
|
+
# For anybody is using AS and AR without Railties, i.e. Padrino.
|
179
193
|
ActiveRecord::Base.extend(DefaultValueFor::ClassMethods)
|
180
194
|
end
|
data/test.rb
CHANGED
@@ -20,10 +20,10 @@
|
|
20
20
|
|
21
21
|
require 'bundler/setup'
|
22
22
|
require 'minitest/autorun'
|
23
|
+
require 'minitest/around/unit'
|
23
24
|
require 'active_record'
|
24
|
-
require 'active_support/dependencies'
|
25
25
|
|
26
|
-
if ActiveSupport::VERSION::MAJOR
|
26
|
+
if ActiveSupport::VERSION::MAJOR == 3
|
27
27
|
require 'active_support/core_ext/logger'
|
28
28
|
end
|
29
29
|
|
@@ -37,28 +37,27 @@ require 'default_value_for'
|
|
37
37
|
|
38
38
|
puts "\nTesting with Active Record version #{ActiveRecord::VERSION::STRING}\n\n"
|
39
39
|
|
40
|
-
if RUBY_PLATFORM == "java"
|
41
|
-
database_adapter = "jdbcsqlite3"
|
42
|
-
else
|
43
|
-
database_adapter = "sqlite3"
|
44
|
-
end
|
45
40
|
ActiveRecord::Base.default_timezone = :local
|
46
|
-
ActiveRecord::Base.logger
|
47
|
-
ActiveRecord::Base.logger.level
|
41
|
+
ActiveRecord::Base.logger = Logger.new(STDERR)
|
42
|
+
ActiveRecord::Base.logger.level = Logger::WARN
|
43
|
+
|
48
44
|
ActiveRecord::Base.establish_connection(
|
49
|
-
:adapter =>
|
45
|
+
:adapter => RUBY_PLATFORM == 'java' ? 'jdbcsqlite3' : 'sqlite3',
|
50
46
|
:database => ':memory:'
|
51
47
|
)
|
48
|
+
|
52
49
|
ActiveRecord::Base.connection.create_table(:users, :force => true) do |t|
|
53
50
|
t.string :username
|
54
51
|
t.integer :default_number
|
55
52
|
end
|
56
|
-
|
53
|
+
|
54
|
+
ActiveRecord::Base.connection.create_table(:books, :force => true) do |t|
|
57
55
|
t.string :type
|
58
56
|
t.integer :number
|
59
57
|
t.integer :count, :null => false, :default => 1
|
60
58
|
t.integer :user_id
|
61
59
|
t.timestamp :timestamp
|
60
|
+
t.text :stuff
|
62
61
|
t.boolean :flag
|
63
62
|
end
|
64
63
|
|
@@ -67,250 +66,175 @@ if defined?(Rails::Railtie)
|
|
67
66
|
DefaultValueFor.initialize_active_record_extensions
|
68
67
|
end
|
69
68
|
|
70
|
-
class User < ActiveRecord::Base
|
71
|
-
has_many :numbers, :class_name => 'TestClass'
|
72
|
-
end
|
73
|
-
|
74
|
-
class Number < ActiveRecord::Base
|
75
|
-
end
|
76
|
-
|
77
69
|
class DefaultValuePluginTest < TestCaseClass
|
78
|
-
def
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
70
|
+
def around
|
71
|
+
Object.const_set(:User, Class.new(ActiveRecord::Base))
|
72
|
+
Object.const_set(:Book, Class.new(ActiveRecord::Base))
|
73
|
+
Object.const_set(:Novel, Class.new(Book))
|
74
|
+
User.has_many :books
|
75
|
+
Book.belongs_to :user
|
76
|
+
|
77
|
+
ActiveRecord::Base.transaction do
|
78
|
+
yield
|
79
|
+
raise ActiveRecord::Rollback
|
80
|
+
end
|
81
|
+
ensure
|
82
|
+
Object.send(:remove_const, :User)
|
83
|
+
Object.send(:remove_const, :Book)
|
84
|
+
Object.send(:remove_const, :Novel)
|
85
|
+
ActiveSupport::Dependencies.clear
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_default_value_on_attribute_methods
|
89
|
+
Book.class_eval do
|
90
|
+
serialize :stuff
|
91
|
+
default_value_for :color, :green
|
92
|
+
def color; (self.stuff || {})[:color]; end
|
93
|
+
def color=(val)
|
94
|
+
self.stuff ||= {}
|
95
|
+
self.stuff[:color] = val
|
95
96
|
end
|
96
97
|
end
|
97
|
-
|
98
|
+
assert_equal :green, Book.create.color
|
98
99
|
end
|
99
100
|
|
100
101
|
def test_default_value_can_be_passed_as_argument
|
101
|
-
|
102
|
-
|
103
|
-
end
|
104
|
-
object = TestClass.new
|
105
|
-
assert_equal 1234, object.number
|
102
|
+
Book.default_value_for(:number, 1234)
|
103
|
+
assert_equal 1234, Book.new.number
|
106
104
|
end
|
107
105
|
|
108
106
|
def test_default_value_can_be_passed_as_block
|
109
|
-
|
110
|
-
|
111
|
-
end
|
112
|
-
object = TestClass.new
|
113
|
-
assert_equal 1234, object.number
|
107
|
+
Book.default_value_for(:number) { 1234 }
|
108
|
+
assert_equal 1234, Book.new.number
|
114
109
|
end
|
115
110
|
|
116
111
|
def test_works_with_create
|
117
|
-
|
118
|
-
default_value_for :number, 1234
|
119
|
-
end
|
112
|
+
Book.default_value_for :number, 1234
|
120
113
|
|
121
|
-
object =
|
122
|
-
refute_nil
|
114
|
+
object = Book.create
|
115
|
+
refute_nil Book.find_by_number(1234)
|
123
116
|
|
124
117
|
# allows nil for existing records
|
125
118
|
object.update_attribute(:number, nil)
|
126
|
-
assert_nil
|
127
|
-
assert_nil
|
119
|
+
assert_nil Book.find_by_number(1234)
|
120
|
+
assert_nil Book.find(object.id).number
|
128
121
|
end
|
129
122
|
|
130
|
-
def
|
131
|
-
|
132
|
-
|
133
|
-
end
|
134
|
-
|
135
|
-
object = TestClass.create
|
136
|
-
|
137
|
-
# allows nil for existing records
|
123
|
+
def test_does_not_allow_nil_sets_default_value_on_existing_nils
|
124
|
+
Book.default_value_for(:number, :allows_nil => false) { 1234 }
|
125
|
+
object = Book.create
|
138
126
|
object.update_attribute(:number, nil)
|
139
|
-
assert_nil
|
140
|
-
assert_equal 1234,
|
127
|
+
assert_nil Book.find_by_number(1234)
|
128
|
+
assert_equal 1234, Book.find(object.id).number
|
141
129
|
end
|
142
130
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
end
|
147
|
-
object = TestClass.new
|
148
|
-
assert_equal 1234, object.count
|
131
|
+
def test_overwrites_db_default
|
132
|
+
Book.default_value_for :count, 1234
|
133
|
+
assert_equal 1234, Book.new.count
|
149
134
|
end
|
150
135
|
|
151
136
|
def test_doesnt_overwrite_values_provided_by_mass_assignment
|
152
|
-
|
153
|
-
|
154
|
-
end
|
155
|
-
object = TestClass.new(:number => 1, :count => 2)
|
156
|
-
assert_equal 1, object.number
|
137
|
+
Book.default_value_for :number, 1234
|
138
|
+
assert_equal 1, Book.new(:number => 1, :count => 2).number
|
157
139
|
end
|
158
140
|
|
159
141
|
def test_doesnt_overwrite_values_provided_by_multiparameter_assignment
|
160
|
-
|
161
|
-
default_value_for :timestamp, Time.mktime(2000, 1, 1)
|
162
|
-
end
|
142
|
+
Book.default_value_for :timestamp, Time.mktime(2000, 1, 1)
|
163
143
|
timestamp = Time.mktime(2009, 1, 1)
|
164
|
-
object =
|
144
|
+
object = Book.new('timestamp(1i)' => '2009', 'timestamp(2i)' => '1', 'timestamp(3i)' => '1')
|
165
145
|
assert_equal timestamp, object.timestamp
|
166
146
|
end
|
167
147
|
|
168
148
|
def test_doesnt_overwrite_values_provided_by_constructor_block
|
169
|
-
|
170
|
-
|
171
|
-
end
|
172
|
-
object = TestClass.new do |x|
|
149
|
+
Book.default_value_for :number, 1234
|
150
|
+
object = Book.new do |x|
|
173
151
|
x.number = 1
|
174
|
-
x.count
|
152
|
+
x.count = 2
|
175
153
|
end
|
176
154
|
assert_equal 1, object.number
|
177
155
|
end
|
178
156
|
|
179
157
|
def test_doesnt_overwrite_explicitly_provided_nil_values_in_mass_assignment
|
180
|
-
|
181
|
-
|
182
|
-
end
|
183
|
-
object = TestClass.new(:number => nil)
|
184
|
-
assert_equal nil, object.number
|
158
|
+
Book.default_value_for :number, 1234
|
159
|
+
assert_equal nil, Book.new(:number => nil).number
|
185
160
|
end
|
186
161
|
|
187
162
|
def test_overwrites_explicitly_provided_nil_values_in_mass_assignment
|
188
|
-
|
189
|
-
|
190
|
-
end
|
191
|
-
object = TestClass.new(:number => nil)
|
192
|
-
assert_equal 1234, object.number
|
163
|
+
Book.default_value_for :number, :value => 1234, :allows_nil => false
|
164
|
+
assert_equal 1234, Book.new(:number => nil).number
|
193
165
|
end
|
194
166
|
|
195
167
|
def test_default_values_are_inherited
|
196
|
-
|
197
|
-
|
198
|
-
end
|
199
|
-
define_model_class("TestClass", "TestSuperClass")
|
200
|
-
object = TestClass.new
|
201
|
-
assert_equal 1234, object.number
|
168
|
+
Book.default_value_for :number, 1234
|
169
|
+
assert_equal 1234, Novel.new.number
|
202
170
|
end
|
203
171
|
|
204
|
-
def
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
object = TestClass.new
|
212
|
-
assert_equal 5678, object.number
|
172
|
+
def test_default_values_in_superclass_are_saved_in_subclass
|
173
|
+
Book.default_value_for :number, 1234
|
174
|
+
Novel.default_value_for :flag, true
|
175
|
+
object = Novel.create!
|
176
|
+
assert_equal object.id, Novel.find_by_number(1234).id
|
177
|
+
assert_equal object.id, Novel.find_by_flag(true).id
|
178
|
+
end
|
213
179
|
|
214
|
-
|
215
|
-
|
180
|
+
def test_default_values_in_subclass
|
181
|
+
Novel.default_value_for :number, 5678
|
182
|
+
assert_equal 5678, Novel.new.number
|
183
|
+
assert_nil Book.new.number
|
216
184
|
end
|
217
185
|
|
218
186
|
def test_multiple_default_values_in_subclass_with_default_values_in_parent_class
|
219
|
-
|
187
|
+
Book.class_eval do
|
220
188
|
default_value_for :other_number, nil
|
221
189
|
attr_accessor :other_number
|
222
190
|
end
|
223
|
-
|
224
|
-
default_value_for :number, 5678
|
191
|
+
Novel.default_value_for :number, 5678
|
225
192
|
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
end
|
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
|
+
Novel.default_value_for :user_id, 9999
|
231
197
|
|
232
|
-
object =
|
198
|
+
object = Novel.new
|
233
199
|
assert_nil object.other_number
|
234
200
|
assert_equal 5678, object.number
|
235
201
|
assert_equal 9999, object.user_id
|
236
202
|
end
|
237
203
|
|
238
204
|
def test_override_default_values_in_subclass
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
default_value_for :number, 5678
|
244
|
-
end
|
245
|
-
|
246
|
-
object = TestClass.new
|
247
|
-
assert_equal 5678, object.number
|
248
|
-
|
249
|
-
object = TestSuperClass.new
|
250
|
-
assert_equal 1234, object.number
|
205
|
+
Book.default_value_for :number, 1234
|
206
|
+
Novel.default_value_for :number, 5678
|
207
|
+
assert_equal 5678, Novel.new.number
|
208
|
+
assert_equal 1234, Book.new.number
|
251
209
|
end
|
252
210
|
|
253
211
|
def test_default_values_in_subclass_do_not_affect_parent_class
|
254
|
-
|
255
|
-
|
256
|
-
end
|
257
|
-
define_model_class("TestClass", "TestSuperClass") do
|
212
|
+
Book.default_value_for :number, 1234
|
213
|
+
Novel.class_eval do
|
258
214
|
default_value_for :hello, "hi"
|
259
215
|
attr_accessor :hello
|
260
216
|
end
|
261
217
|
|
262
|
-
assert
|
263
|
-
assert !
|
218
|
+
assert Book.new
|
219
|
+
assert !Book._default_attribute_values.include?(:hello)
|
264
220
|
end
|
265
221
|
|
266
222
|
def test_doesnt_set_default_on_saved_records
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
assert_equal 9876, TestClass.first.number
|
223
|
+
Book.create(:number => 9876)
|
224
|
+
Book.default_value_for :number, 1234
|
225
|
+
assert_equal 9876, Book.first.number
|
271
226
|
end
|
272
227
|
|
273
228
|
def test_also_works_on_attributes_that_arent_database_columns
|
274
|
-
|
229
|
+
Book.class_eval do
|
275
230
|
default_value_for :hello, "hi"
|
276
231
|
attr_accessor :hello
|
277
232
|
end
|
278
|
-
|
279
|
-
assert_equal 'hi', object.hello
|
280
|
-
end
|
281
|
-
|
282
|
-
if ActiveRecord::VERSION::MAJOR < 4
|
283
|
-
def test_constructor_ignores_forbidden_mass_assignment_attributes
|
284
|
-
define_model_class do
|
285
|
-
default_value_for :number, 1234
|
286
|
-
attr_protected :number
|
287
|
-
end
|
288
|
-
object = TestClass.new(:number => 5678, :count => 987)
|
289
|
-
assert_equal 1234, object.number
|
290
|
-
assert_equal 987, object.count
|
291
|
-
end
|
292
|
-
|
293
|
-
def test_constructor_respects_without_protection_option
|
294
|
-
define_model_class do
|
295
|
-
default_value_for :number, 1234
|
296
|
-
attr_protected :number
|
297
|
-
|
298
|
-
def respond_to_mass_assignment_options?
|
299
|
-
respond_to? :mass_assignment_options
|
300
|
-
end
|
301
|
-
end
|
302
|
-
|
303
|
-
if TestClass.new.respond_to_mass_assignment_options?
|
304
|
-
# test without protection feature if available in current ActiveRecord version
|
305
|
-
object = TestClass.create!({:number => 5678, :count => 987}, :without_protection => true)
|
306
|
-
assert_equal 5678, object.number
|
307
|
-
assert_equal 987, object.count
|
308
|
-
end
|
309
|
-
end
|
233
|
+
assert_equal 'hi', Book.new.hello
|
310
234
|
end
|
311
235
|
|
312
236
|
def test_doesnt_conflict_with_overrided_initialize_method_in_model_class
|
313
|
-
|
237
|
+
Book.class_eval do
|
314
238
|
def initialize(attrs = {})
|
315
239
|
@initialized = true
|
316
240
|
super(:count => 5678)
|
@@ -318,67 +242,56 @@ class DefaultValuePluginTest < TestCaseClass
|
|
318
242
|
|
319
243
|
default_value_for :number, 1234
|
320
244
|
end
|
321
|
-
object =
|
245
|
+
object = Book.new
|
322
246
|
assert_equal 1234, object.number
|
323
247
|
assert_equal 5678, object.count
|
324
248
|
assert object.instance_variable_get('@initialized')
|
325
249
|
end
|
326
250
|
|
327
251
|
def test_model_instance_is_passed_to_the_given_block
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
$instance = n
|
332
|
-
end
|
252
|
+
instance = nil
|
253
|
+
Book.default_value_for :number do |n|
|
254
|
+
instance = n
|
333
255
|
end
|
334
|
-
object =
|
335
|
-
assert_same object,
|
256
|
+
object = Book.new
|
257
|
+
assert_same object.object_id, instance.object_id
|
336
258
|
end
|
337
259
|
|
338
260
|
def test_can_specify_default_value_via_association
|
339
261
|
user = User.create(:username => 'Kanako', :default_number => 123)
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
default_value_for :number do |n|
|
344
|
-
n.user.default_number
|
345
|
-
end
|
262
|
+
Book.default_value_for :number do |n|
|
263
|
+
n.user.default_number
|
346
264
|
end
|
347
|
-
|
348
|
-
assert_equal 123, object.number
|
265
|
+
assert_equal 123, user.books.create!.number
|
349
266
|
end
|
350
267
|
|
351
268
|
def test_default_values
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
269
|
+
Book.default_values({
|
270
|
+
:type => "normal",
|
271
|
+
:number => lambda { 10 + 5 },
|
272
|
+
:timestamp => lambda {|_| Time.now }
|
273
|
+
})
|
357
274
|
|
358
|
-
object =
|
275
|
+
object = Book.new
|
359
276
|
assert_equal("normal", object.type)
|
360
277
|
assert_equal(15, object.number)
|
361
278
|
end
|
362
279
|
|
363
280
|
def test_default_value_order
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
this.count * 2
|
368
|
-
end
|
281
|
+
Book.default_value_for :count, 5
|
282
|
+
Book.default_value_for :number do |this|
|
283
|
+
this.count * 2
|
369
284
|
end
|
370
|
-
object =
|
285
|
+
object = Book.new
|
371
286
|
assert_equal(5, object.count)
|
372
287
|
assert_equal(10, object.number)
|
373
288
|
end
|
374
289
|
|
375
290
|
def test_attributes_with_default_values_are_not_marked_as_changed
|
376
|
-
|
377
|
-
|
378
|
-
default_value_for :number, 2
|
379
|
-
end
|
291
|
+
Book.default_value_for :count, 5
|
292
|
+
Book.default_value_for :number, 2
|
380
293
|
|
381
|
-
object =
|
294
|
+
object = Book.new
|
382
295
|
assert(!object.changed?)
|
383
296
|
assert_equal([], object.changed)
|
384
297
|
|
@@ -388,65 +301,78 @@ class DefaultValuePluginTest < TestCaseClass
|
|
388
301
|
end
|
389
302
|
|
390
303
|
def test_default_values_are_duplicated
|
391
|
-
|
392
|
-
|
393
|
-
self.table_name = "users"
|
394
|
-
else
|
395
|
-
set_table_name "users"
|
396
|
-
end
|
397
|
-
default_value_for :username, "hello"
|
398
|
-
end
|
399
|
-
user1 = TestClass.new
|
304
|
+
User.default_value_for :username, "hello"
|
305
|
+
user1 = User.new
|
400
306
|
user1.username << " world"
|
401
|
-
user2 =
|
307
|
+
user2 = User.new
|
402
308
|
assert_equal("hello", user2.username)
|
403
309
|
end
|
404
310
|
|
405
311
|
def test_default_values_are_shallow_copied
|
406
|
-
|
407
|
-
if respond_to?(:table_name=)
|
408
|
-
self.table_name = "users"
|
409
|
-
else
|
410
|
-
set_table_name "users"
|
411
|
-
end
|
312
|
+
User.class_eval do
|
412
313
|
attr_accessor :hash
|
413
314
|
default_value_for :hash, { 1 => [] }
|
414
315
|
end
|
415
|
-
user1 =
|
316
|
+
user1 = User.new
|
416
317
|
user1.hash[1] << 1
|
417
|
-
user2 =
|
318
|
+
user2 = User.new
|
418
319
|
assert_equal([1], user2.hash[1])
|
419
320
|
end
|
420
321
|
|
421
322
|
def test_constructor_does_not_affect_the_hash_passed_to_it
|
422
|
-
|
423
|
-
default_value_for :count, 5
|
424
|
-
end
|
425
|
-
|
323
|
+
Book.default_value_for :count, 5
|
426
324
|
options = { :count => 5, :user_id => 1 }
|
427
325
|
options_dup = options.dup
|
428
|
-
|
326
|
+
Book.new(options)
|
429
327
|
assert_equal(options_dup, options)
|
430
328
|
end
|
431
329
|
|
432
330
|
def test_subclass_find
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
define_model_class("SpecialNumber", "TestClass")
|
437
|
-
n = SpecialNumber.create
|
438
|
-
assert SpecialNumber.find(n.id)
|
331
|
+
Book.default_value_for :number, 5678
|
332
|
+
n = Novel.create
|
333
|
+
assert Novel.find(n.id)
|
439
334
|
end
|
440
335
|
|
441
336
|
def test_does_not_see_false_as_blank_at_boolean_columns_for_existing_records
|
442
|
-
|
443
|
-
default_value_for(:flag, :allows_nil => false) { true }
|
444
|
-
end
|
337
|
+
Book.default_value_for(:flag, :allows_nil => false) { true }
|
445
338
|
|
446
|
-
object =
|
339
|
+
object = Book.create
|
447
340
|
|
448
341
|
# allows nil for existing records
|
449
342
|
object.update_attribute(:flag, false)
|
450
|
-
assert_equal false,
|
343
|
+
assert_equal false, Book.find(object.id).flag
|
344
|
+
end
|
345
|
+
|
346
|
+
def test_works_with_nested_attributes
|
347
|
+
User.accepts_nested_attributes_for :books
|
348
|
+
User.default_value_for :books do
|
349
|
+
[Book.create!(:number => 0)]
|
350
|
+
end
|
351
|
+
|
352
|
+
user = User.create! :books_attributes => [{:number => 1}]
|
353
|
+
assert_equal 1, Book.all.first.number
|
354
|
+
end
|
355
|
+
|
356
|
+
if ActiveRecord::VERSION::MAJOR == 3
|
357
|
+
def test_constructor_ignores_forbidden_mass_assignment_attributes
|
358
|
+
Book.class_eval do
|
359
|
+
default_value_for :number, 1234
|
360
|
+
attr_protected :number
|
361
|
+
end
|
362
|
+
object = Book.new(:number => 5678, :count => 987)
|
363
|
+
assert_equal 1234, object.number
|
364
|
+
assert_equal 987, object.count
|
365
|
+
end
|
366
|
+
|
367
|
+
def test_constructor_respects_without_protection_option
|
368
|
+
Book.class_eval do
|
369
|
+
default_value_for :number, 1234
|
370
|
+
attr_protected :number
|
371
|
+
end
|
372
|
+
|
373
|
+
object = Book.create!({:number => 5678, :count => 987}, :without_protection => true)
|
374
|
+
assert_equal 5678, object.number
|
375
|
+
assert_equal 987, object.count
|
376
|
+
end
|
451
377
|
end
|
452
378
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: default_value_for
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.
|
4
|
+
version: 3.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hongli Lai
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -64,6 +64,34 @@ dependencies:
|
|
64
64
|
- - ! '>='
|
65
65
|
- !ruby/object:Gem::Version
|
66
66
|
version: '4.2'
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: minitest-around
|
69
|
+
requirement: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
type: :development
|
75
|
+
prerelease: false
|
76
|
+
version_requirements: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: appraisal
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
67
95
|
description: The default_value_for plugin allows one to define default values for
|
68
96
|
ActiveRecord models in a declarative manner
|
69
97
|
email: software-signing@phusion.nl
|
checksums.yaml.gz.asc
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
-----BEGIN PGP SIGNATURE-----
|
2
|
-
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
|
3
|
-
Comment: GPGTools - http://gpgtools.org
|
4
|
-
|
5
|
-
iQEcBAABAgAGBQJTiKJoAAoJECrHRaUKISqMBMAIAIJzmjgE9LA3ICQLebX+rSyR
|
6
|
-
EX+4LORtl9aq7/wwqotkw05UP+ItsWYk7YxgIo+pUZrsri6XLFxhdGW+ovxksO7c
|
7
|
-
27QyamNe05ZH7i2ebt3abiyNVfHYWbgx1bwsdjC3FjR3IUltNANMMDthxuYY7XUR
|
8
|
-
MWNLWPpVG9mwweyy/c7kS7MqThTxxRNQBRsonA/ULEAL6FMDMT1uR/RHrrOt/dQv
|
9
|
-
it+jo2Cy9wG31PyK2KtnW6dJADgLGmlAmowfdvFNc4g4V905DmliVUQa5wfYP/LZ
|
10
|
-
jQA9n+lCH8cA6baFXpV4F4aHoh5kuD6idXmnpu/H+Ezcu0wLYospFf3dk8cP4yI=
|
11
|
-
=n3f0
|
12
|
-
-----END PGP SIGNATURE-----
|
data.tar.gz.asc
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
-----BEGIN PGP SIGNATURE-----
|
2
|
-
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
|
3
|
-
Comment: GPGTools - http://gpgtools.org
|
4
|
-
|
5
|
-
iQEcBAABAgAGBQJTiKJoAAoJECrHRaUKISqM35AH+wff1B5OYbLhUWOkKk7/iYTK
|
6
|
-
UfDAIACcKsst3H7CBHZQKdxgLv0MHQqJ4RMW3MFioKJQpoQj29MuqMJm45jPrB60
|
7
|
-
b4+3h4z7kNkbR2yQ/hGxn1D9o4ODzgjNBIN25ipPkSdsLxEa/cef8Bhw2CXnKJjc
|
8
|
-
WKgxtuTr/16m71PnFF/syHk4rq00J6PAY5bGxk5gCTl1k7FjMb3PBIVmJ19miZBQ
|
9
|
-
qlUqiDhYwHUkIkE4kEGETRGu4hItO2jRozvvkLFJe/fxVDaCb+vxxlyrRWiy69/Y
|
10
|
-
39/fLDK4K3vg8tyHpomJ7yrHF8bjAqcwSit1Hg43P8RtKcdJoxYm2ANMKC1AOrE=
|
11
|
-
=OqS0
|
12
|
-
-----END PGP SIGNATURE-----
|
metadata.gz.asc
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
-----BEGIN PGP SIGNATURE-----
|
2
|
-
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
|
3
|
-
Comment: GPGTools - http://gpgtools.org
|
4
|
-
|
5
|
-
iQEcBAABAgAGBQJTiKJoAAoJECrHRaUKISqMw1QH+gJo/kWFIhDXf5nHMYrsKuNg
|
6
|
-
atpW59t9U7xa1dZi9knw7SJoxXjJ9+yxZUXeHd+h+UiFu5MVcC6IhZYz2V5f1Dy3
|
7
|
-
uqRTMNiK1X4/JJkAzRyC45J4jPRkRfnuea07fmb2wC5RFlDmCKCcm2FTfDu3hYUt
|
8
|
-
xlvay9TDoMyB1ualJW40XywIWYY7EL9miucQoH0mUsdAmIm7y8VtoKVLjceNWQV9
|
9
|
-
jXcE3ATjjQjkc1lLcOQ8tMY+l+Tnf8IDFiQGLjLXnx+H3bTNesuiuDSONULT4A2m
|
10
|
-
994BGrKdOwY7vkvpbTo8Ou1DMIEn5QYcwHxwPuapYmAwMaNQIZbXsWi57jLLk6U=
|
11
|
-
=Ls01
|
12
|
-
-----END PGP SIGNATURE-----
|