default_value_for 1.0.0 → 1.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.
- data/LICENSE.TXT +1 -1
- data/README.rdoc +1 -1
- data/default_value_for.gemspec +8 -2
- data/init.rb +6 -82
- data/lib/default_value_for.rb +99 -0
- data/lib/default_value_for/railtie.rb +33 -0
- data/lib/rails.rb +23 -0
- data/test.rb +34 -32
- metadata +8 -5
data/LICENSE.TXT
CHANGED
data/README.rdoc
CHANGED
@@ -77,7 +77,7 @@ whatever reason:
|
|
77
77
|
|
78
78
|
As a shortcut, you can use +default_values+ to set multiple default values at once.
|
79
79
|
|
80
|
-
default_values :age => 20
|
80
|
+
default_values :age => 20,
|
81
81
|
:uuid => lambda { UuidGenerator.new.generate_uuid }
|
82
82
|
|
83
83
|
The difference is purely aesthetic. If you have lots of default values which are constants or constructed with one-line blocks, +default_values+ may look nicer. If you have default values constructed by longer blocks, +default_value_for+ suit you better. Feel free to mix and match.
|
data/default_value_for.gemspec
CHANGED
@@ -1,10 +1,16 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = %q{default_value_for}
|
3
|
-
s.version = "1.0.
|
3
|
+
s.version = "1.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{info@phusion.nl}
|
7
7
|
s.homepage = %q{http://github.com/FooBarWidget/default_value_for}
|
8
8
|
s.authors = ["Hongli Lai"]
|
9
|
-
s.files = ['default_value_for.gemspec',
|
9
|
+
s.files = ['default_value_for.gemspec',
|
10
|
+
'LICENSE.TXT', 'Rakefile', 'README.rdoc', 'test.rb',
|
11
|
+
'init.rb',
|
12
|
+
'lib/default_value_for.rb',
|
13
|
+
'lib/default_value_for/railtie.rb',
|
14
|
+
'lib/rails.rb'
|
15
|
+
]
|
10
16
|
end
|
data/init.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
# Copyright (c) 2008 Phusion
|
2
|
-
#
|
1
|
+
# Copyright (c) 2008, 2009, 2010 Phusion
|
2
|
+
#
|
3
3
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
4
|
# of this software and associated documentation files (the "Software"), to deal
|
5
5
|
# in the Software without restriction, including without limitation the rights
|
6
6
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
7
|
# copies of the Software, and to permit persons to whom the Software is
|
8
8
|
# furnished to do so, subject to the following conditions:
|
9
|
-
#
|
9
|
+
#
|
10
10
|
# The above copyright notice and this permission notice shall be included in
|
11
11
|
# all copies or substantial portions of the Software.
|
12
|
-
#
|
12
|
+
#
|
13
13
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
14
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
15
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
@@ -18,82 +18,6 @@
|
|
18
18
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
19
|
# THE SOFTWARE.
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
def initialize(value)
|
24
|
-
@value = value
|
25
|
-
end
|
26
|
-
|
27
|
-
def evaluate(instance)
|
28
|
-
if @value.duplicable?
|
29
|
-
return @value.dup
|
30
|
-
else
|
31
|
-
return @value
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
class BlockValueContainer
|
37
|
-
def initialize(block)
|
38
|
-
@block = block
|
39
|
-
end
|
40
|
-
|
41
|
-
def evaluate(instance)
|
42
|
-
return @block.call(instance)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
module ClassMethods
|
47
|
-
def default_value_for(attribute, value = nil, &block)
|
48
|
-
if !method_defined?(:initialize_with_defaults)
|
49
|
-
include(InstanceMethods)
|
50
|
-
alias_method_chain :initialize, :defaults
|
51
|
-
class_inheritable_accessor :_default_attribute_values
|
52
|
-
self._default_attribute_values = ActiveSupport::OrderedHash.new
|
53
|
-
end
|
54
|
-
if block_given?
|
55
|
-
container = BlockValueContainer.new(block)
|
56
|
-
else
|
57
|
-
container = NormalValueContainer.new(value)
|
58
|
-
end
|
59
|
-
_default_attribute_values[attribute.to_s] = container
|
60
|
-
end
|
61
|
-
|
62
|
-
def default_values(values)
|
63
|
-
values.each_pair do |key, value|
|
64
|
-
if value.kind_of? Proc
|
65
|
-
default_value_for(key, &value)
|
66
|
-
else
|
67
|
-
default_value_for(key, value)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
module InstanceMethods
|
74
|
-
def initialize_with_defaults(attrs = nil)
|
75
|
-
initialize_without_defaults(attrs) do
|
76
|
-
if attrs
|
77
|
-
stringified_attrs = attrs.stringify_keys
|
78
|
-
safe_attrs = if respond_to? :sanitize_for_mass_assignment
|
79
|
-
sanitize_for_mass_assignment(stringified_attrs)
|
80
|
-
else
|
81
|
-
remove_attributes_protected_from_mass_assignment(stringified_attrs)
|
82
|
-
end
|
83
|
-
safe_attribute_names = safe_attrs.keys.map do |x|
|
84
|
-
x.to_s
|
85
|
-
end
|
86
|
-
end
|
87
|
-
self.class._default_attribute_values.each do |attribute, container|
|
88
|
-
if safe_attribute_names.nil? || !safe_attribute_names.any? { |attr_name| attr_name =~ /^#{attribute}($|\()/ }
|
89
|
-
__send__("#{attribute}=", container.evaluate(self))
|
90
|
-
changed_attributes.delete(attribute)
|
91
|
-
end
|
92
|
-
end
|
93
|
-
yield(self) if block_given?
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
21
|
+
# Rails 2 initialization
|
22
|
+
require "default_value_for"
|
99
23
|
ActiveRecord::Base.extend(DefaultValueForPlugin::ClassMethods)
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# Copyright (c) 2008, 2009, 2010 Phusion
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require "default_value_for/railtie"
|
22
|
+
|
23
|
+
module DefaultValueForPlugin
|
24
|
+
class NormalValueContainer
|
25
|
+
def initialize(value)
|
26
|
+
@value = value
|
27
|
+
end
|
28
|
+
|
29
|
+
def evaluate(instance)
|
30
|
+
if @value.duplicable?
|
31
|
+
return @value.dup
|
32
|
+
else
|
33
|
+
return @value
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class BlockValueContainer
|
39
|
+
def initialize(block)
|
40
|
+
@block = block
|
41
|
+
end
|
42
|
+
|
43
|
+
def evaluate(instance)
|
44
|
+
return @block.call(instance)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
module ClassMethods
|
49
|
+
def default_value_for(attribute, value = nil, &block)
|
50
|
+
if !method_defined?(:initialize_with_defaults)
|
51
|
+
include(InstanceMethods)
|
52
|
+
alias_method_chain :initialize, :defaults
|
53
|
+
class_inheritable_accessor :_default_attribute_values
|
54
|
+
self._default_attribute_values = ActiveSupport::OrderedHash.new
|
55
|
+
end
|
56
|
+
if block_given?
|
57
|
+
container = BlockValueContainer.new(block)
|
58
|
+
else
|
59
|
+
container = NormalValueContainer.new(value)
|
60
|
+
end
|
61
|
+
_default_attribute_values[attribute.to_s] = container
|
62
|
+
end
|
63
|
+
|
64
|
+
def default_values(values)
|
65
|
+
values.each_pair do |key, value|
|
66
|
+
if value.kind_of? Proc
|
67
|
+
default_value_for(key, &value)
|
68
|
+
else
|
69
|
+
default_value_for(key, value)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
module InstanceMethods
|
76
|
+
def initialize_with_defaults(attrs = nil)
|
77
|
+
initialize_without_defaults(attrs) do
|
78
|
+
if attrs
|
79
|
+
stringified_attrs = attrs.stringify_keys
|
80
|
+
safe_attrs = if respond_to? :sanitize_for_mass_assignment
|
81
|
+
sanitize_for_mass_assignment(stringified_attrs)
|
82
|
+
else
|
83
|
+
remove_attributes_protected_from_mass_assignment(stringified_attrs)
|
84
|
+
end
|
85
|
+
safe_attribute_names = safe_attrs.keys.map do |x|
|
86
|
+
x.to_s
|
87
|
+
end
|
88
|
+
end
|
89
|
+
self.class._default_attribute_values.each do |attribute, container|
|
90
|
+
if safe_attribute_names.nil? || !safe_attribute_names.any? { |attr_name| attr_name =~ /^#{attribute}($|\()/ }
|
91
|
+
__send__("#{attribute}=", container.evaluate(self))
|
92
|
+
changed_attributes.delete(attribute)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
yield(self) if block_given?
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# Copyright (c) 2008, 2009, 2010 Phusion
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
# Rails 3 initialization
|
22
|
+
module DefaultValueForPlugin
|
23
|
+
if defined? Rails::Railtie
|
24
|
+
require 'rails'
|
25
|
+
class Railtie < Rails::Railtie
|
26
|
+
initializer 'default_value_for.insert_into_active_record' do
|
27
|
+
ActiveSupport.on_load :active_record do
|
28
|
+
ActiveRecord::Base.extend(DefaultValueForPlugin::ClassMethods)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/rails.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# Copyright (c) 2008, 2009, 2010 Phusion
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
# Rails 2 initialization
|
22
|
+
require "default_value_for"
|
23
|
+
ActiveRecord::Base.extend(DefaultValueForPlugin::ClassMethods)
|
data/test.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# Copyright (c) 2008, 2009 Phusion
|
2
|
-
#
|
2
|
+
#
|
3
3
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
4
|
# of this software and associated documentation files (the "Software"), to deal
|
5
5
|
# in the Software without restriction, including without limitation the rights
|
6
6
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
7
|
# copies of the Software, and to permit persons to whom the Software is
|
8
8
|
# furnished to do so, subject to the following conditions:
|
9
|
-
#
|
9
|
+
#
|
10
10
|
# The above copyright notice and this permission notice shall be included in
|
11
11
|
# all copies or substantial portions of the Software.
|
12
|
-
#
|
12
|
+
#
|
13
13
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
14
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
15
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
@@ -21,6 +21,8 @@
|
|
21
21
|
require 'rubygems'
|
22
22
|
require 'active_record'
|
23
23
|
require 'test/unit'
|
24
|
+
require 'active_support/core_ext/logger'
|
25
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
24
26
|
require File.dirname(__FILE__) + '/init'
|
25
27
|
Dir.chdir(File.dirname(__FILE__))
|
26
28
|
|
@@ -60,11 +62,11 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
60
62
|
def setup
|
61
63
|
Number.create(:number => 9876)
|
62
64
|
end
|
63
|
-
|
65
|
+
|
64
66
|
def teardown
|
65
67
|
Number.delete_all
|
66
68
|
end
|
67
|
-
|
69
|
+
|
68
70
|
def define_model_class(name = "TestClass", parent_class_name = "ActiveRecord::Base", &block)
|
69
71
|
Object.send(:remove_const, name) rescue nil
|
70
72
|
eval("class #{name} < #{parent_class_name}; end", TOPLEVEL_BINDING)
|
@@ -74,7 +76,7 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
74
76
|
end
|
75
77
|
klass.class_eval(&block) if block_given?
|
76
78
|
end
|
77
|
-
|
79
|
+
|
78
80
|
def test_default_value_can_be_passed_as_argument
|
79
81
|
define_model_class do
|
80
82
|
default_value_for(:number, 1234)
|
@@ -82,7 +84,7 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
82
84
|
object = TestClass.new
|
83
85
|
assert_equal 1234, object.number
|
84
86
|
end
|
85
|
-
|
87
|
+
|
86
88
|
def test_default_value_can_be_passed_as_block
|
87
89
|
define_model_class do
|
88
90
|
default_value_for(:number) { 1234 }
|
@@ -90,7 +92,7 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
90
92
|
object = TestClass.new
|
91
93
|
assert_equal 1234, object.number
|
92
94
|
end
|
93
|
-
|
95
|
+
|
94
96
|
def test_works_with_create
|
95
97
|
define_model_class do
|
96
98
|
default_value_for :number, 1234
|
@@ -98,7 +100,7 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
98
100
|
TestClass.create
|
99
101
|
assert_not_nil TestClass.find_by_number(1234)
|
100
102
|
end
|
101
|
-
|
103
|
+
|
102
104
|
def test_overwrites_db_default
|
103
105
|
define_model_class do
|
104
106
|
default_value_for :count, 1234
|
@@ -106,7 +108,7 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
106
108
|
object = TestClass.new
|
107
109
|
assert_equal 1234, object.count
|
108
110
|
end
|
109
|
-
|
111
|
+
|
110
112
|
def test_doesnt_overwrite_values_provided_by_mass_assignment
|
111
113
|
define_model_class do
|
112
114
|
default_value_for :number, 1234
|
@@ -114,7 +116,7 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
114
116
|
object = TestClass.new(:number => 1, :count => 2)
|
115
117
|
assert_equal 1, object.number
|
116
118
|
end
|
117
|
-
|
119
|
+
|
118
120
|
def test_doesnt_overwrite_values_provided_by_multiparameter_assignment
|
119
121
|
define_model_class do
|
120
122
|
default_value_for :timestamp, Time.mktime(2000, 1, 1)
|
@@ -123,7 +125,7 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
123
125
|
object = TestClass.new('timestamp(1i)' => '2009', 'timestamp(2i)' => '1', 'timestamp(3i)' => '1')
|
124
126
|
assert_equal timestamp, object.timestamp
|
125
127
|
end
|
126
|
-
|
128
|
+
|
127
129
|
def test_doesnt_overwrite_values_provided_by_constructor_block
|
128
130
|
define_model_class do
|
129
131
|
default_value_for :number, 1234
|
@@ -134,7 +136,7 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
134
136
|
end
|
135
137
|
assert_equal 1, object.number
|
136
138
|
end
|
137
|
-
|
139
|
+
|
138
140
|
def test_doesnt_overwrite_explicitly_provided_nil_values_in_mass_assignment
|
139
141
|
define_model_class do
|
140
142
|
default_value_for :number, 1234
|
@@ -142,7 +144,7 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
142
144
|
object = TestClass.new(:number => nil)
|
143
145
|
assert_nil object.number
|
144
146
|
end
|
145
|
-
|
147
|
+
|
146
148
|
def test_default_values_are_inherited
|
147
149
|
define_model_class("TestSuperClass") do
|
148
150
|
default_value_for :number, 1234
|
@@ -151,14 +153,14 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
151
153
|
object = TestClass.new
|
152
154
|
assert_equal 1234, object.number
|
153
155
|
end
|
154
|
-
|
156
|
+
|
155
157
|
def test_doesnt_set_default_on_saved_records
|
156
158
|
define_model_class do
|
157
159
|
default_value_for :number, 1234
|
158
160
|
end
|
159
161
|
assert_equal 9876, TestClass.find(:first).number
|
160
162
|
end
|
161
|
-
|
163
|
+
|
162
164
|
def test_also_works_on_attributes_that_arent_database_columns
|
163
165
|
define_model_class do
|
164
166
|
default_value_for :hello, "hi"
|
@@ -167,7 +169,7 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
167
169
|
object = TestClass.new
|
168
170
|
assert_equal 'hi', object.hello
|
169
171
|
end
|
170
|
-
|
172
|
+
|
171
173
|
def test_constructor_ignores_forbidden_mass_assignment_attributes
|
172
174
|
define_model_class do
|
173
175
|
default_value_for :number, 1234
|
@@ -177,14 +179,14 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
177
179
|
assert_equal 1234, object.number
|
178
180
|
assert_equal 987, object.count
|
179
181
|
end
|
180
|
-
|
182
|
+
|
181
183
|
def test_doesnt_conflict_with_overrided_initialize_method_in_model_class
|
182
184
|
define_model_class do
|
183
185
|
def initialize(attrs = {})
|
184
186
|
@initialized = true
|
185
187
|
super(:count => 5678)
|
186
188
|
end
|
187
|
-
|
189
|
+
|
188
190
|
default_value_for :number, 1234
|
189
191
|
end
|
190
192
|
object = TestClass.new
|
@@ -192,7 +194,7 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
192
194
|
assert_equal 5678, object.count
|
193
195
|
assert object.instance_variable_get('@initialized')
|
194
196
|
end
|
195
|
-
|
197
|
+
|
196
198
|
def test_model_instance_is_passed_to_the_given_block
|
197
199
|
$instance = nil
|
198
200
|
define_model_class do
|
@@ -203,12 +205,12 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
203
205
|
object = TestClass.new
|
204
206
|
assert_same object, $instance
|
205
207
|
end
|
206
|
-
|
208
|
+
|
207
209
|
def test_can_specify_default_value_via_association
|
208
210
|
user = User.create(:username => 'Kanako', :default_number => 123)
|
209
211
|
define_model_class do
|
210
212
|
belongs_to :user
|
211
|
-
|
213
|
+
|
212
214
|
default_value_for :number do |n|
|
213
215
|
n.user.default_number
|
214
216
|
end
|
@@ -216,18 +218,18 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
216
218
|
object = user.numbers.create
|
217
219
|
assert_equal 123, object.number
|
218
220
|
end
|
219
|
-
|
221
|
+
|
220
222
|
def test_default_values
|
221
223
|
define_model_class do
|
222
224
|
default_values :type => "normal",
|
223
225
|
:number => lambda { 10 + 5 }
|
224
226
|
end
|
225
|
-
|
227
|
+
|
226
228
|
object = TestClass.new
|
227
229
|
assert_equal("normal", object.type)
|
228
230
|
assert_equal(15, object.number)
|
229
231
|
end
|
230
|
-
|
232
|
+
|
231
233
|
def test_default_value_order
|
232
234
|
define_model_class do
|
233
235
|
default_value_for :count, 5
|
@@ -239,22 +241,22 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
239
241
|
assert_equal(5, object.count)
|
240
242
|
assert_equal(10, object.number)
|
241
243
|
end
|
242
|
-
|
244
|
+
|
243
245
|
def test_attributes_with_default_values_are_not_marked_as_changed
|
244
246
|
define_model_class do
|
245
247
|
default_value_for :count, 5
|
246
248
|
default_value_for :number, 2
|
247
249
|
end
|
248
|
-
|
250
|
+
|
249
251
|
object = TestClass.new
|
250
252
|
assert(!object.changed?)
|
251
253
|
assert_equal([], object.changed)
|
252
|
-
|
254
|
+
|
253
255
|
object.type = "foo"
|
254
256
|
assert(object.changed?)
|
255
257
|
assert_equal(["type"], object.changed)
|
256
258
|
end
|
257
|
-
|
259
|
+
|
258
260
|
def test_default_values_are_duplicated
|
259
261
|
define_model_class do
|
260
262
|
set_table_name "users"
|
@@ -265,7 +267,7 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
265
267
|
user2 = TestClass.new
|
266
268
|
assert_equal("hello", user2.username)
|
267
269
|
end
|
268
|
-
|
270
|
+
|
269
271
|
def test_default_values_are_shallow_copied
|
270
272
|
define_model_class do
|
271
273
|
set_table_name "users"
|
@@ -277,12 +279,12 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
277
279
|
user2 = TestClass.new
|
278
280
|
assert_equal([1], user2.hash[1])
|
279
281
|
end
|
280
|
-
|
282
|
+
|
281
283
|
def test_constructor_does_not_affect_the_hash_passed_to_it
|
282
284
|
define_model_class do
|
283
285
|
default_value_for :count, 5
|
284
286
|
end
|
285
|
-
|
287
|
+
|
286
288
|
options = { :count => 5, :user_id => 1 }
|
287
289
|
options_dup = options.dup
|
288
290
|
object = TestClass.new(options)
|
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:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
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: 2010-08
|
18
|
+
date: 2010-11-08 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -29,11 +29,14 @@ extra_rdoc_files: []
|
|
29
29
|
|
30
30
|
files:
|
31
31
|
- default_value_for.gemspec
|
32
|
-
- init.rb
|
33
32
|
- LICENSE.TXT
|
34
33
|
- Rakefile
|
35
34
|
- README.rdoc
|
36
35
|
- test.rb
|
36
|
+
- init.rb
|
37
|
+
- lib/default_value_for.rb
|
38
|
+
- lib/default_value_for/railtie.rb
|
39
|
+
- lib/rails.rb
|
37
40
|
has_rdoc: true
|
38
41
|
homepage: http://github.com/FooBarWidget/default_value_for
|
39
42
|
licenses: []
|