default_value_for 2.0.3 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- checksums.yaml.gz.asc +8 -0
- data.tar.gz.asc +3 -7
- data/README.md +556 -0
- data/Rakefile +21 -1
- data/default_value_for.gemspec +6 -2
- data/lib/default_value_for.rb +28 -15
- data/lib/default_value_for/railtie.rb +15 -15
- data/test.rb +64 -48
- metadata +64 -12
- metadata.gz.asc +3 -7
- data/README.rdoc +0 -510
data/Rakefile
CHANGED
@@ -2,5 +2,25 @@ task :default => :test
|
|
2
2
|
|
3
3
|
desc "Run unit tests."
|
4
4
|
task :test do
|
5
|
-
|
5
|
+
ruby "test.rb"
|
6
|
+
end
|
7
|
+
|
8
|
+
['3.2', '4.0', '4.1'].each do |version|
|
9
|
+
dotless = version.delete('.')
|
10
|
+
|
11
|
+
namespace :bundle do
|
12
|
+
desc "Bundle with Rails #{version}.x"
|
13
|
+
task :"rails#{dotless}" do
|
14
|
+
ENV['BUNDLE_GEMFILE'] = "Gemfile.rails.#{version}.rb"
|
15
|
+
sh "bundle"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
namespace :test do
|
20
|
+
desc "Test with Rails #{version}.x"
|
21
|
+
task :"rails#{dotless}" do
|
22
|
+
ENV['BUNDLE_GEMFILE'] = "Gemfile.rails.#{version}.rb"
|
23
|
+
ruby "test.rb"
|
24
|
+
end
|
25
|
+
end
|
6
26
|
end
|
data/default_value_for.gemspec
CHANGED
@@ -1,15 +1,19 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = %q{default_value_for}
|
3
|
-
s.version = "
|
3
|
+
s.version = "3.0.0"
|
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}
|
7
7
|
s.homepage = %q{https://github.com/FooBarWidget/default_value_for}
|
8
8
|
s.authors = ["Hongli Lai"]
|
9
9
|
s.files = ['default_value_for.gemspec',
|
10
|
-
'LICENSE.TXT', 'Rakefile', 'README.
|
10
|
+
'LICENSE.TXT', 'Rakefile', 'README.md', 'test.rb',
|
11
11
|
'init.rb',
|
12
12
|
'lib/default_value_for.rb',
|
13
13
|
'lib/default_value_for/railtie.rb'
|
14
14
|
]
|
15
|
+
s.add_dependency 'activerecord', '>= 3.2.0', '< 5.0'
|
16
|
+
s.add_development_dependency 'railties', '>= 3.2.0', '< 5.0'
|
17
|
+
s.add_development_dependency 'minitest', '>= 4.2'
|
18
|
+
s.required_ruby_version = '>= 1.9.3'
|
15
19
|
end
|
data/lib/default_value_for.rb
CHANGED
@@ -56,32 +56,34 @@ module DefaultValueFor
|
|
56
56
|
# * <tt>value</tt> - Sets the default value.
|
57
57
|
# * <tt>allows_nil (default: true)</tt> - Sets explicitly passed nil values if option is set to true.
|
58
58
|
def default_value_for(attribute, options = {}, &block)
|
59
|
-
value
|
60
|
-
allows_nil =
|
59
|
+
value = options
|
60
|
+
allows_nil = true
|
61
|
+
|
62
|
+
if options.is_a?(Hash)
|
63
|
+
opts = options.stringify_keys
|
64
|
+
value = opts.fetch('value', options)
|
65
|
+
allows_nil = opts.fetch('allows_nil', true)
|
66
|
+
end
|
61
67
|
|
62
68
|
if !method_defined?(:set_default_values)
|
63
69
|
include(InstanceMethods)
|
64
70
|
|
65
71
|
after_initialize :set_default_values
|
66
72
|
|
67
|
-
|
68
|
-
|
69
|
-
class_attribute :_default_attribute_values_not_allowing_nil
|
70
|
-
else
|
71
|
-
class_inheritable_accessor :_default_attribute_values
|
72
|
-
class_inheritable_accessor :_default_attribute_values_not_allowing_nil
|
73
|
-
end
|
73
|
+
class_attribute :_default_attribute_values
|
74
|
+
class_attribute :_default_attribute_values_not_allowing_nil
|
74
75
|
|
75
76
|
extend(DelayedClassMethods)
|
76
77
|
init_hash = true
|
77
78
|
else
|
78
|
-
|
79
|
-
init_hash = !methods.include?("_default_attribute_values") && !methods.include?(:_default_attribute_values)
|
79
|
+
init_hash = !singleton_methods(false).include?(:_default_attribute_values)
|
80
80
|
end
|
81
|
+
|
81
82
|
if init_hash
|
82
|
-
self._default_attribute_values =
|
83
|
+
self._default_attribute_values = {}
|
83
84
|
self._default_attribute_values_not_allowing_nil = []
|
84
85
|
end
|
86
|
+
|
85
87
|
if block_given?
|
86
88
|
container = BlockValueContainer.new(block)
|
87
89
|
else
|
@@ -134,20 +136,31 @@ module DefaultValueFor
|
|
134
136
|
end
|
135
137
|
end
|
136
138
|
|
137
|
-
if
|
139
|
+
if self.class.respond_to? :protected_attributes
|
138
140
|
super(attributes, options)
|
139
141
|
else
|
140
142
|
super(attributes)
|
141
143
|
end
|
142
144
|
end
|
143
145
|
|
146
|
+
def attributes_for_create(attribute_names)
|
147
|
+
attribute_names += _default_attribute_values.keys.map(&:to_s)
|
148
|
+
super
|
149
|
+
end
|
150
|
+
|
144
151
|
def set_default_values
|
145
152
|
self.class._all_default_attribute_values.each do |attribute, container|
|
146
|
-
next unless
|
153
|
+
next unless new_record? || self.class._all_default_attribute_values_not_allowing_nil.include?(attribute)
|
147
154
|
|
148
155
|
connection_default_value_defined = new_record? && respond_to?("#{attribute}_changed?") && !__send__("#{attribute}_changed?")
|
149
156
|
|
150
|
-
|
157
|
+
column = self.class.columns.detect {|c| c.name == attribute}
|
158
|
+
attribute_blank = if column && column.type == :boolean
|
159
|
+
attributes[attribute].nil?
|
160
|
+
else
|
161
|
+
attributes[attribute].blank?
|
162
|
+
end
|
163
|
+
next unless connection_default_value_defined || attribute_blank
|
151
164
|
|
152
165
|
# allow explicitly setting nil through allow nil option
|
153
166
|
next if @initialization_attributes.is_a?(Hash) && @initialization_attributes.has_key?(attribute) && !self.class._all_default_attribute_values_not_allowing_nil.include?(attribute)
|
@@ -21,19 +21,19 @@
|
|
21
21
|
# Rails 3 initialization
|
22
22
|
|
23
23
|
module DefaultValueFor
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
24
|
+
def self.initialize_railtie
|
25
|
+
ActiveSupport.on_load :active_record do
|
26
|
+
DefaultValueFor.initialize_active_record_extensions
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.initialize_active_record_extensions
|
31
|
+
ActiveRecord::Base.extend(DefaultValueFor::ClassMethods)
|
32
|
+
end
|
33
|
+
|
34
|
+
class Railtie < Rails::Railtie
|
35
|
+
initializer 'default_value_for.insert_into_active_record' do
|
36
|
+
DefaultValueFor.initialize_railtie
|
37
|
+
end
|
38
|
+
end
|
39
39
|
end
|
data/test.rb
CHANGED
@@ -18,35 +18,36 @@
|
|
18
18
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
19
|
# THE SOFTWARE.
|
20
20
|
|
21
|
-
|
21
|
+
require 'bundler/setup'
|
22
|
+
require 'minitest/autorun'
|
23
|
+
require 'active_record'
|
24
|
+
require 'active_support/dependencies'
|
25
|
+
|
26
|
+
if ActiveSupport::VERSION::MAJOR < 4
|
27
|
+
require 'active_support/core_ext/logger'
|
28
|
+
end
|
22
29
|
|
23
|
-
require 'rubygems'
|
24
|
-
gem 'rails', want_rails_version
|
25
|
-
gem 'activerecord', want_rails_version
|
26
30
|
begin
|
27
|
-
|
28
|
-
rescue
|
31
|
+
TestCaseClass = MiniTest::Test
|
32
|
+
rescue NameError
|
33
|
+
TestCaseClass = MiniTest::Unit::TestCase
|
29
34
|
end
|
30
|
-
|
31
|
-
require 'test/unit'
|
32
|
-
require 'active_support/dependencies'
|
33
|
-
require 'active_support/core_ext/logger'
|
34
|
-
$LOAD_PATH.unshift File.expand_path("lib", File.dirname(__FILE__))
|
35
|
+
|
35
36
|
require 'default_value_for'
|
36
|
-
|
37
|
+
|
38
|
+
puts "\nTesting with Active Record version #{ActiveRecord::VERSION::STRING}\n\n"
|
37
39
|
|
38
40
|
if RUBY_PLATFORM == "java"
|
39
41
|
database_adapter = "jdbcsqlite3"
|
40
42
|
else
|
41
43
|
database_adapter = "sqlite3"
|
42
44
|
end
|
43
|
-
|
44
|
-
File.unlink('test.sqlite3') rescue nil
|
45
|
+
ActiveRecord::Base.default_timezone = :local
|
45
46
|
ActiveRecord::Base.logger = Logger.new(STDERR)
|
46
47
|
ActiveRecord::Base.logger.level = Logger::WARN
|
47
48
|
ActiveRecord::Base.establish_connection(
|
48
|
-
:adapter
|
49
|
-
:database => '
|
49
|
+
:adapter => database_adapter,
|
50
|
+
:database => ':memory:'
|
50
51
|
)
|
51
52
|
ActiveRecord::Base.connection.create_table(:users, :force => true) do |t|
|
52
53
|
t.string :username
|
@@ -58,6 +59,7 @@ ActiveRecord::Base.connection.create_table(:numbers, :force => true) do |t|
|
|
58
59
|
t.integer :count, :null => false, :default => 1
|
59
60
|
t.integer :user_id
|
60
61
|
t.timestamp :timestamp
|
62
|
+
t.boolean :flag
|
61
63
|
end
|
62
64
|
|
63
65
|
if defined?(Rails::Railtie)
|
@@ -72,7 +74,7 @@ end
|
|
72
74
|
class Number < ActiveRecord::Base
|
73
75
|
end
|
74
76
|
|
75
|
-
class DefaultValuePluginTest <
|
77
|
+
class DefaultValuePluginTest < TestCaseClass
|
76
78
|
def setup
|
77
79
|
Number.create(:number => 9876)
|
78
80
|
end
|
@@ -115,10 +117,10 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
115
117
|
define_model_class do
|
116
118
|
default_value_for :number, 1234
|
117
119
|
end
|
118
|
-
|
120
|
+
|
119
121
|
object = TestClass.create
|
120
|
-
|
121
|
-
|
122
|
+
refute_nil TestClass.find_by_number(1234)
|
123
|
+
|
122
124
|
# allows nil for existing records
|
123
125
|
object.update_attribute(:number, nil)
|
124
126
|
assert_nil TestClass.find_by_number(1234)
|
@@ -129,9 +131,9 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
129
131
|
define_model_class do
|
130
132
|
default_value_for(:number, :allows_nil => false) { 1234 }
|
131
133
|
end
|
132
|
-
|
134
|
+
|
133
135
|
object = TestClass.create
|
134
|
-
|
136
|
+
|
135
137
|
# allows nil for existing records
|
136
138
|
object.update_attribute(:number, nil)
|
137
139
|
assert_nil TestClass.find_by_number(1234)
|
@@ -257,7 +259,7 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
257
259
|
attr_accessor :hello
|
258
260
|
end
|
259
261
|
|
260
|
-
|
262
|
+
assert TestSuperClass.new
|
261
263
|
assert !TestSuperClass._default_attribute_values.include?(:hello)
|
262
264
|
end
|
263
265
|
|
@@ -265,7 +267,7 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
265
267
|
define_model_class do
|
266
268
|
default_value_for :number, 1234
|
267
269
|
end
|
268
|
-
assert_equal 9876, TestClass.
|
270
|
+
assert_equal 9876, TestClass.first.number
|
269
271
|
end
|
270
272
|
|
271
273
|
def test_also_works_on_attributes_that_arent_database_columns
|
@@ -277,31 +279,33 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
277
279
|
assert_equal 'hi', object.hello
|
278
280
|
end
|
279
281
|
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
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
|
284
291
|
end
|
285
|
-
object = TestClass.new(:number => 5678, :count => 987)
|
286
|
-
assert_equal 1234, object.number
|
287
|
-
assert_equal 987, object.count
|
288
|
-
end
|
289
292
|
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
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
|
297
308
|
end
|
298
|
-
end
|
299
|
-
|
300
|
-
if TestClass.new.respond_to_mass_assignment_options?
|
301
|
-
# test without protection feature if available in current ActiveRecord version
|
302
|
-
object = TestClass.create!({:number => 5678, :count => 987}, :without_protection => true)
|
303
|
-
assert_equal 5678, object.number
|
304
|
-
assert_equal 987, object.count
|
305
309
|
end
|
306
310
|
end
|
307
311
|
|
@@ -413,7 +417,7 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
413
417
|
user2 = TestClass.new
|
414
418
|
assert_equal([1], user2.hash[1])
|
415
419
|
end
|
416
|
-
|
420
|
+
|
417
421
|
def test_constructor_does_not_affect_the_hash_passed_to_it
|
418
422
|
define_model_class do
|
419
423
|
default_value_for :count, 5
|
@@ -431,6 +435,18 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
431
435
|
end
|
432
436
|
define_model_class("SpecialNumber", "TestClass")
|
433
437
|
n = SpecialNumber.create
|
434
|
-
|
438
|
+
assert SpecialNumber.find(n.id)
|
439
|
+
end
|
440
|
+
|
441
|
+
def test_does_not_see_false_as_blank_at_boolean_columns_for_existing_records
|
442
|
+
define_model_class do
|
443
|
+
default_value_for(:flag, :allows_nil => false) { true }
|
444
|
+
end
|
445
|
+
|
446
|
+
object = TestClass.create
|
447
|
+
|
448
|
+
# allows nil for existing records
|
449
|
+
object.update_attribute(:flag, false)
|
450
|
+
assert_equal false, TestClass.find(object.id).flag
|
435
451
|
end
|
436
452
|
end
|
metadata
CHANGED
@@ -1,16 +1,69 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: default_value_for
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease:
|
4
|
+
version: 3.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Hongli Lai
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
dependencies:
|
11
|
+
date: 2014-01-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.0
|
20
|
+
- - <
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.0
|
30
|
+
- - <
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: railties
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 3.2.0
|
40
|
+
- - <
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '5.0'
|
43
|
+
type: :development
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 3.2.0
|
50
|
+
- - <
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '5.0'
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: minitest
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '4.2'
|
60
|
+
type: :development
|
61
|
+
prerelease: false
|
62
|
+
version_requirements: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '4.2'
|
14
67
|
description: The default_value_for plugin allows one to define default values for
|
15
68
|
ActiveRecord models in a declarative manner
|
16
69
|
email: software-signing@phusion.nl
|
@@ -18,36 +71,35 @@ executables: []
|
|
18
71
|
extensions: []
|
19
72
|
extra_rdoc_files: []
|
20
73
|
files:
|
21
|
-
- default_value_for.gemspec
|
22
74
|
- LICENSE.TXT
|
75
|
+
- README.md
|
23
76
|
- Rakefile
|
24
|
-
-
|
25
|
-
- test.rb
|
77
|
+
- default_value_for.gemspec
|
26
78
|
- init.rb
|
27
79
|
- lib/default_value_for.rb
|
28
80
|
- lib/default_value_for/railtie.rb
|
81
|
+
- test.rb
|
29
82
|
homepage: https://github.com/FooBarWidget/default_value_for
|
30
83
|
licenses: []
|
84
|
+
metadata: {}
|
31
85
|
post_install_message:
|
32
86
|
rdoc_options: []
|
33
87
|
require_paths:
|
34
88
|
- lib
|
35
89
|
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
-
none: false
|
37
90
|
requirements:
|
38
91
|
- - ! '>='
|
39
92
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
93
|
+
version: 1.9.3
|
41
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
-
none: false
|
43
95
|
requirements:
|
44
96
|
- - ! '>='
|
45
97
|
- !ruby/object:Gem::Version
|
46
98
|
version: '0'
|
47
99
|
requirements: []
|
48
100
|
rubyforge_project:
|
49
|
-
rubygems_version:
|
101
|
+
rubygems_version: 2.2.0
|
50
102
|
signing_key:
|
51
|
-
specification_version:
|
103
|
+
specification_version: 4
|
52
104
|
summary: Provides a way to specify default values for ActiveRecord models
|
53
105
|
test_files: []
|