default_value_for 3.1.0 → 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/default_value_for.gemspec +2 -1
- data/lib/default_value_for.rb +2 -1
- data/test.rb +67 -1
- metadata +22 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0fc011401b53010c6cc35ec4b7d603ca7c25caf4c4f5602aaad7d5ac594e5d54
|
4
|
+
data.tar.gz: 19c64f2d05b114507c1aeb16745f2c9dd9559acf6a37643594c86eaf9a2e9165
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 398b1ffc9a6c087f854e3779b73c3624f0e5da598f60b63c41c5c7a14943d4294c2031147110e998c60026e9bce09d50d68f73e1bff98e33d61a6915ade1078b
|
7
|
+
data.tar.gz: 422c73abf865cee8d4619f983a91d3a20850b1ee66393e1ed5b6e1667ffcbc3529e36dd88fff4b72a07712c33096b03a1c18618377c9d4961f6f5fef5fe2ce5c
|
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.
|
3
|
+
s.version = "3.2.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}
|
@@ -19,4 +19,5 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.add_development_dependency 'minitest', '>= 4.2'
|
20
20
|
s.add_development_dependency 'minitest-around'
|
21
21
|
s.add_development_dependency 'appraisal'
|
22
|
+
s.add_development_dependency 'actionpack', '>= 3.2.0', '< 6.0'
|
22
23
|
end
|
data/lib/default_value_for.rb
CHANGED
@@ -124,6 +124,7 @@ module DefaultValueFor
|
|
124
124
|
|
125
125
|
module InstanceMethods
|
126
126
|
def initialize(attributes = nil, options = {})
|
127
|
+
attributes = attributes.to_h if attributes.respond_to?(:to_h)
|
127
128
|
@initialization_attributes = attributes.is_a?(Hash) ? attributes.stringify_keys : {}
|
128
129
|
|
129
130
|
unless options[:without_protection]
|
@@ -157,7 +158,7 @@ module DefaultValueFor
|
|
157
158
|
connection_default_value_defined = new_record? && respond_to?("#{attribute}_changed?") && !__send__("#{attribute}_changed?")
|
158
159
|
|
159
160
|
attribute_blank = if attributes.has_key?(attribute)
|
160
|
-
column = self.class.
|
161
|
+
column = self.class.columns_hash[attribute]
|
161
162
|
if column && column.type == :boolean
|
162
163
|
attributes[attribute].nil?
|
163
164
|
else
|
data/test.rb
CHANGED
@@ -22,11 +22,44 @@ require 'bundler/setup'
|
|
22
22
|
require 'minitest/autorun'
|
23
23
|
require 'minitest/around/unit'
|
24
24
|
require 'active_record'
|
25
|
+
require 'action_pack'
|
25
26
|
|
26
27
|
if ActiveSupport::VERSION::MAJOR == 3
|
27
28
|
require 'active_support/core_ext/logger'
|
28
29
|
end
|
29
30
|
|
31
|
+
if ActionPack::VERSION::MAJOR > 3
|
32
|
+
require 'action_controller'
|
33
|
+
end
|
34
|
+
|
35
|
+
# Handle an edge-case when using Arel 5 (i.e. Rails <= 4.1) with Ruby >= 2.4:
|
36
|
+
# See: https://github.com/rails/arel/commit/dc85a6e9c74942945ad696f5da4d82490a85b865
|
37
|
+
# See: https://stackoverflow.com/a/51481088
|
38
|
+
rails_match_data = RUBY_VERSION.match(/\A(?<major>\d+).(?<minor>\d+)/)
|
39
|
+
rails_2_4_or_newer = rails_match_data[:major].to_i > 2 || (rails_match_data[:major].to_i == 2 && rails_match_data[:minor].to_i >= 4)
|
40
|
+
arel_match_data = Arel::VERSION.match(/\A(?<major>\d+).(?<minor>\d+)/)
|
41
|
+
arel_older_than_7_1 = arel_match_data[:major].to_i < 7 || (arel_match_data[:major].to_i == 7 && arel_match_data[:minor].to_i < 1)
|
42
|
+
|
43
|
+
if rails_2_4_or_newer && arel_older_than_7_1
|
44
|
+
module Arel
|
45
|
+
module Visitors
|
46
|
+
class DepthFirst < Arel::Visitors::Visitor
|
47
|
+
alias :visit_Integer :terminal
|
48
|
+
end
|
49
|
+
|
50
|
+
class Dot < Arel::Visitors::Visitor
|
51
|
+
alias :visit_Integer :visit_String
|
52
|
+
end
|
53
|
+
|
54
|
+
# The super class for ToSql changed with Arel 6
|
55
|
+
# See: https://github.com/rails/arel/commit/a6a7c75ff486657909e20e2f48764136caa5e87e#diff-3538aead5b80677372eea0e903ff728eR7
|
56
|
+
class ToSql < (Arel::VERSION[/\A\d+/].to_i >= 6 ? Arel::Visitors::Reduce : Arel::Visitors::Visitor)
|
57
|
+
alias :visit_Integer :literal
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
30
63
|
begin
|
31
64
|
TestCaseClass = MiniTest::Test
|
32
65
|
rescue NameError
|
@@ -35,7 +68,8 @@ end
|
|
35
68
|
|
36
69
|
require 'default_value_for'
|
37
70
|
|
38
|
-
puts "\nTesting with Active Record version #{ActiveRecord::VERSION::STRING}
|
71
|
+
puts "\nTesting with Active Record version #{ActiveRecord::VERSION::STRING}"
|
72
|
+
puts "\nTesting with Action Pack version #{ActionPack::VERSION::STRING}\n\n"
|
39
73
|
|
40
74
|
ActiveRecord::Base.default_timezone = :local
|
41
75
|
ActiveRecord::Base.logger = Logger.new(STDERR)
|
@@ -370,6 +404,38 @@ class DefaultValuePluginTest < TestCaseClass
|
|
370
404
|
assert_equal 'This is a bio', user.bio
|
371
405
|
end
|
372
406
|
|
407
|
+
if ActionPack::VERSION::MAJOR > 3
|
408
|
+
def test_doesnt_overwrite_explicitly_provided_nil_values_in_mass_assignment_with_action_controller_parameters
|
409
|
+
Book.default_value_for :number, 1234
|
410
|
+
|
411
|
+
assert_nil Book.new(ActionController::Parameters.new(:number => nil).permit!).number
|
412
|
+
end
|
413
|
+
|
414
|
+
def test_overwrites_explicitly_provided_nil_values_in_mass_assignment_with_action_controller_parameters
|
415
|
+
Book.default_value_for :number, :value => 1234, :allows_nil => false
|
416
|
+
|
417
|
+
assert_equal 1234, Book.new(ActionController::Parameters.new(:number => nil).permit!).number
|
418
|
+
end
|
419
|
+
|
420
|
+
def test_works_with_nested_attributes_with_action_controller_parameters
|
421
|
+
User.accepts_nested_attributes_for :books
|
422
|
+
User.default_value_for :books do
|
423
|
+
[Book.create!(:number => 0)]
|
424
|
+
end
|
425
|
+
|
426
|
+
user = User.create!(ActionController::Parameters.new(:books_attributes => [{:number => 1}]).permit!)
|
427
|
+
assert_equal 1, Book.all.first.number
|
428
|
+
end
|
429
|
+
|
430
|
+
def test_works_with_stored_attribute_accessors_when_initializing_value_that_does_not_allow_nil_with_action_controller_parameters
|
431
|
+
User.store :settings, :accessors => :bio
|
432
|
+
User.default_value_for :bio, :value => 'None given', :allows_nil => false
|
433
|
+
|
434
|
+
user = User.create!(ActionController::Parameters.new(:bio => 'This is a bio').permit!)
|
435
|
+
assert_equal 'This is a bio', user.bio
|
436
|
+
end
|
437
|
+
end
|
438
|
+
|
373
439
|
if ActiveRecord::VERSION::MAJOR == 3
|
374
440
|
def test_constructor_ignores_forbidden_mass_assignment_attributes
|
375
441
|
Book.class_eval do
|
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.
|
4
|
+
version: 3.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hongli Lai
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-06-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -92,6 +92,26 @@ dependencies:
|
|
92
92
|
- - ">="
|
93
93
|
- !ruby/object:Gem::Version
|
94
94
|
version: '0'
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: actionpack
|
97
|
+
requirement: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 3.2.0
|
102
|
+
- - "<"
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '6.0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: 3.2.0
|
112
|
+
- - "<"
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '6.0'
|
95
115
|
description: The default_value_for plugin allows one to define default values for
|
96
116
|
ActiveRecord models in a declarative manner
|
97
117
|
email: software-signing@phusion.nl
|