default_value_for 3.0.3 → 3.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d622d97fecd784ed09f1202e354a721fa02a7700
4
- data.tar.gz: c0ee0dc2feeebeee11e63b246fe77a4e46260200
2
+ SHA256:
3
+ metadata.gz: 15a799717c30677d582f0c87b7491f2a6a15206dd52018057e2df15db60fb21f
4
+ data.tar.gz: b884a6fbd0e6c679562a3765499514893649a8d3659318a92dbf009e1a1a43aa
5
5
  SHA512:
6
- metadata.gz: 68da2ccf0e955ad0c7c4daa68fea6607d0ba3da8521e7ecb5bcc0d42d750be6ec1b5ab93767939a74459ef6b1b7efa16c8c81c74036e0de2ac2cff88093b0ee8
7
- data.tar.gz: 67c18c88f8f57429cf502f7c69efe38883ed286c8791e258bd3fc59fdb053814097f68ead33494a4ce00d76e3f672b2f2b9c8e599a43e38414fc4fb9ed5d0474
6
+ metadata.gz: 1814019f12d56dcde794f9a70e530088ab1746976a99a8d076ef3b6858cb9c56410e85932f690a26a0d6ab8ee632c93634b81622e7b45548362c19d2551d548f
7
+ data.tar.gz: 73995f62cc25df268848f6630018dcba0f9fc8093d28382f2006919b08e32eda7b375de1359ae7506f5e074359db6839f1178e1446d52579ee69cb30d06c0ba6
data/README.md CHANGED
@@ -22,12 +22,12 @@ u.last_seen # => Mon Sep 22 17:28:38 +0200 2008
22
22
 
23
23
  ### Rails 3.2 - 4.2 / Ruby 1.9.3 and higher
24
24
 
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.
25
+ The current version of default_value_for (3.x+) is compatible with Rails 3.2 or higher, and Ruby 1.9.3 and higher.
26
26
 
27
27
  Add it to your Gemfile:
28
28
 
29
29
  ```ruby
30
- gem "default_value_for", "~> 3.0.0"
30
+ gem "default_value_for", "~> 3.0"
31
31
  ```
32
32
 
33
33
  This gem is signed using PGP with the Phusion Software Signing key: http://www.phusion.nl/about/gpg. That key in turn is signed by the rubygems-openpgp Certificate Authority: http://www.rubygems-openpgp-ca.org/.
data/Rakefile CHANGED
@@ -5,7 +5,19 @@ task :test do
5
5
  ruby "test.rb"
6
6
  end
7
7
 
8
- ['3.2', '4.0', '4.1', '4.2', '5.0', '5.1'].each do |version|
8
+ rails_versions = %w(
9
+ 3.2
10
+ 4.0
11
+ 4.1
12
+ 4.2
13
+ 5.0
14
+ 5.1
15
+ 5.2
16
+ 6.0
17
+ 6.1
18
+ )
19
+
20
+ rails_versions.each do |version|
9
21
  dotless = version.delete('.')
10
22
 
11
23
  namespace :bundle do
@@ -24,3 +36,14 @@ end
24
36
  end
25
37
  end
26
38
  end
39
+
40
+ namespace :test do
41
+ desc "Test with all supported Rails versions"
42
+ task :railsall do
43
+ rails_versions.each do |version|
44
+ dotless = version.delete('.')
45
+ ENV['BUNDLE_GEMFILE'] = "gemfiles/rails_#{dotless}.gemfile"
46
+ ruby "test.rb"
47
+ end
48
+ end
49
+ end
@@ -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"
3
+ s.version = "3.4.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}
@@ -14,8 +14,9 @@ Gem::Specification.new do |s|
14
14
  'lib/default_value_for.rb',
15
15
  'lib/default_value_for/railtie.rb'
16
16
  ]
17
- s.add_dependency 'activerecord', '>= 3.2.0', '< 5.2'
18
- s.add_development_dependency 'railties', '>= 3.2.0', '< 5.2'
17
+ s.add_dependency 'activerecord', '>= 3.2.0', '< 7.0'
18
+ s.add_development_dependency 'actionpack', '>= 3.2.0', '< 7.0'
19
+ s.add_development_dependency 'railties', '>= 3.2.0', '< 7.0'
19
20
  s.add_development_dependency 'minitest', '>= 4.2'
20
21
  s.add_development_dependency 'minitest-around'
21
22
  s.add_development_dependency 'appraisal'
@@ -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]
@@ -156,12 +157,18 @@ module DefaultValueFor
156
157
 
157
158
  connection_default_value_defined = new_record? && respond_to?("#{attribute}_changed?") && !__send__("#{attribute}_changed?")
158
159
 
159
- column = self.class.columns.detect {|c| c.name == attribute}
160
- attribute_blank = if column && column.type == :boolean
161
- attributes[attribute].nil?
162
- else
163
- attributes[attribute].blank?
164
- end
160
+ attribute_blank = if attributes.has_key?(attribute)
161
+ column = self.class.columns_hash[attribute]
162
+ if column && column.type == :boolean
163
+ attributes[attribute].nil?
164
+ else
165
+ attributes[attribute].blank?
166
+ end
167
+ elsif respond_to?(attribute)
168
+ send(attribute).nil?
169
+ else
170
+ instance_variable_get("@#{attribute}").nil?
171
+ end
165
172
  next unless connection_default_value_defined || attribute_blank
166
173
 
167
174
  # allow explicitly setting nil through allow nil option
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}\n\n"
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)
@@ -49,6 +83,7 @@ ActiveRecord::Base.establish_connection(
49
83
  ActiveRecord::Base.connection.create_table(:users, :force => true) do |t|
50
84
  t.string :username
51
85
  t.integer :default_number
86
+ t.text :settings
52
87
  end
53
88
 
54
89
  ActiveRecord::Base.connection.create_table(:books, :force => true) do |t|
@@ -233,6 +268,14 @@ class DefaultValuePluginTest < TestCaseClass
233
268
  assert_equal 'hi', Book.new.hello
234
269
  end
235
270
 
271
+ def test_works_on_attributes_that_only_have_writers
272
+ Book.class_eval do
273
+ default_value_for :hello, "hi"
274
+ attr_writer :hello
275
+ end
276
+ assert_equal 'hi', Book.new.instance_variable_get('@hello')
277
+ end
278
+
236
279
  def test_doesnt_conflict_with_overrided_initialize_method_in_model_class
237
280
  Book.class_eval do
238
281
  def initialize(attrs = {})
@@ -353,6 +396,46 @@ class DefaultValuePluginTest < TestCaseClass
353
396
  assert_equal 1, Book.all.first.number
354
397
  end
355
398
 
399
+ def test_works_with_stored_attribute_accessors_when_initializing_value_that_does_not_allow_nil
400
+ User.store :settings, :accessors => :bio
401
+ User.default_value_for :bio, :value => 'None given', :allows_nil => false
402
+
403
+ user = User.create!(:bio => 'This is a bio')
404
+ assert_equal 'This is a bio', user.bio
405
+ end
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
+
356
439
  if ActiveRecord::VERSION::MAJOR == 3
357
440
  def test_constructor_ignores_forbidden_mass_assignment_attributes
358
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.0.3
4
+ version: 3.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hongli Lai
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-02 00:00:00.000000000 Z
11
+ date: 2021-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: 3.2.0
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '5.2'
22
+ version: '7.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,27 @@ dependencies:
29
29
  version: 3.2.0
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '5.2'
32
+ version: '7.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: actionpack
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: '7.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: '7.0'
33
53
  - !ruby/object:Gem::Dependency
34
54
  name: railties
35
55
  requirement: !ruby/object:Gem::Requirement
@@ -39,7 +59,7 @@ dependencies:
39
59
  version: 3.2.0
40
60
  - - "<"
41
61
  - !ruby/object:Gem::Version
42
- version: '5.2'
62
+ version: '7.0'
43
63
  type: :development
44
64
  prerelease: false
45
65
  version_requirements: !ruby/object:Gem::Requirement
@@ -49,7 +69,7 @@ dependencies:
49
69
  version: 3.2.0
50
70
  - - "<"
51
71
  - !ruby/object:Gem::Version
52
- version: '5.2'
72
+ version: '7.0'
53
73
  - !ruby/object:Gem::Dependency
54
74
  name: minitest
55
75
  requirement: !ruby/object:Gem::Requirement
@@ -111,7 +131,7 @@ homepage: https://github.com/FooBarWidget/default_value_for
111
131
  licenses:
112
132
  - MIT
113
133
  metadata: {}
114
- post_install_message:
134
+ post_install_message:
115
135
  rdoc_options: []
116
136
  require_paths:
117
137
  - lib
@@ -126,9 +146,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
146
  - !ruby/object:Gem::Version
127
147
  version: '0'
128
148
  requirements: []
129
- rubyforge_project:
130
- rubygems_version: 2.6.13
131
- signing_key:
149
+ rubygems_version: 3.0.3
150
+ signing_key:
132
151
  specification_version: 4
133
152
  summary: Provides a way to specify default values for ActiveRecord models
134
153
  test_files: []