keep_defaults 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a680162d463c6c8b355d29d5e942c0ca3a77b77e1989aa76293f7284b97b252d
4
+ data.tar.gz: 7c204667723cab89a8e52cede65ca7a5408561be51e42492954f7587c8801124
5
+ SHA512:
6
+ metadata.gz: c40b8b79942fc57e5bddf08d70f9326af5605be2678ee0f3afb9e844a206b245b47452d652a4117b55d13dd5b5f98a3742e722fe4cba935a8d8a35c515889261
7
+ data.tar.gz: 93820af9f8bc222e315d4490c64ebd5e0476ff94a8bd498c1829eddd7349ed9bcd95c5c75ee44ba4d5247c4ea107278bc9d0bf89a32ed64fd4f5f8f8d325b168
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,34 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3
7
+ - 2.4
8
+ - 2.5
9
+ - 2.6
10
+ - 2.7
11
+
12
+ # https://docs.travis-ci.com/user/languages/ruby/#bundler-20
13
+ before_install:
14
+ - gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
15
+ - gem install bundler -v '< 2'
16
+
17
+ gemfile:
18
+ - gemfiles/4.2.gemfile
19
+ - gemfiles/5.0.gemfile
20
+ - gemfiles/5.1.gemfile
21
+ - gemfiles/5.2.gemfile
22
+ - gemfiles/6.0.gemfile
23
+
24
+ matrix:
25
+ exclude:
26
+ - rvm: 2.3
27
+ gemfile: gemfiles/6.0.gemfile
28
+ - rvm: 2.4
29
+ gemfile: gemfiles/6.0.gemfile
30
+ - rvm: 2.7
31
+ gemfile: gemfiles/4.2.gemfile
32
+
33
+ notifications:
34
+ email: false
@@ -0,0 +1,24 @@
1
+ appraise "4.2" do
2
+ gem "activerecord", "~> 4.2.11"
3
+ gem "sqlite3", "~> 1.3.6"
4
+ end
5
+
6
+ appraise "5.0" do
7
+ gem "activerecord", "~> 5.0.7"
8
+ gem "sqlite3", "~> 1.3.6"
9
+ end
10
+
11
+ appraise "5.1" do
12
+ gem "activerecord", "~> 5.1.7"
13
+ gem "sqlite3"
14
+ end
15
+
16
+ appraise "5.2" do
17
+ gem "activerecord", "~> 5.2.4"
18
+ gem "sqlite3"
19
+ end
20
+
21
+ appraise "6.0" do
22
+ gem "activerecord", "~> 6.0.2"
23
+ gem "sqlite3"
24
+ end
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in keep_defaults.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 sshaw
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,104 @@
1
+ # Keep Defaults
2
+
3
+ [![Build Status](https://travis-ci.org/sshaw/keep_defaults.svg?branch=master)](https://travis-ci.org/sshaw/keep_defaults)
4
+
5
+ Prevent ActiveRecord attributes for `not null` columns with default values from being set to `nil`.
6
+
7
+ Why is this necessary? Take this example:
8
+ ```rb
9
+ class OrderItem < ApplicationRecord
10
+ # Has column: total numeric(11,2) not null default 0
11
+ # Validation etc...
12
+ end
13
+
14
+ class Order < ApplicationRecord
15
+ # Has columns: total, subtotal, and taxes are all numeric(11,2) not null default 0
16
+ # Validation etc...
17
+
18
+ has_many :order_items
19
+
20
+ def total
21
+ order_items.sum(&:total) + subtotal + taxes
22
+ end
23
+ end
24
+ ```
25
+
26
+ The columns have a default value of `0`, but the attributes can still be set to `nil`.
27
+ This can make for code that is far from bulletproof:
28
+
29
+ ```rb
30
+ o = Order.new
31
+ o.total # 0
32
+ o.taxes = nil
33
+ o.total # 💥 TypeError: nil can't be coerced into Fixnum
34
+ ```
35
+
36
+ To fix you can do something like:
37
+ ```rb
38
+ class OrderItem < ApplicationRecord
39
+ def total
40
+ super || 0
41
+ end
42
+ end
43
+
44
+ class Order < ApplicationRecord
45
+ def total
46
+ order_items.sum(&:total).to_f + subtotal.to_f + taxes.to_f
47
+ end
48
+ end
49
+ ```
50
+
51
+ But what about the other contexts in which these can be called or the other attributes you have? This can get tedious.
52
+
53
+ With Keep Defaults:
54
+ ```rb
55
+ class ApplicationRecord < ActiveRecord::Base
56
+ include KeepDefaults
57
+ end
58
+ ```
59
+
60
+ ```rb
61
+ o = Order.new
62
+ o.total # 0
63
+ o.taxes = nil
64
+ o.total # 0
65
+ o.taxes # 0
66
+ ```
67
+
68
+ Now if an attribute is set to `nil` it will retain its default value instead.
69
+
70
+ ## Installation
71
+
72
+ Add this line to your application's `Gemfile`:
73
+
74
+ ```rb
75
+ gem "keep_defaults"
76
+ ```
77
+
78
+ Or
79
+
80
+ ```rb
81
+ gem install keep_defaults
82
+ ```
83
+
84
+ ## Usage
85
+
86
+ To use everywhere add to `ApplicationRecord`:
87
+
88
+ ```rb
89
+ class ApplicationRecord < ActiveRecord::Base
90
+ include KeepDefaults
91
+ end
92
+ ```
93
+
94
+ To use for a specific class add it directly to that class:
95
+
96
+ ```rb
97
+ class Order < ApplicationRecord
98
+ include KeepDefaults
99
+ end
100
+ ```
101
+
102
+ ## License
103
+
104
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_RETRY: "1"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.2.11"
6
+ gem "sqlite3", "~> 1.3.6"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,66 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ keep_defaults (0.0.1)
5
+ activerecord (>= 4.2, < 7)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (4.2.11.1)
11
+ activesupport (= 4.2.11.1)
12
+ builder (~> 3.1)
13
+ activerecord (4.2.11.1)
14
+ activemodel (= 4.2.11.1)
15
+ activesupport (= 4.2.11.1)
16
+ arel (~> 6.0)
17
+ activesupport (4.2.11.1)
18
+ i18n (~> 0.7)
19
+ minitest (~> 5.1)
20
+ thread_safe (~> 0.3, >= 0.3.4)
21
+ tzinfo (~> 1.1)
22
+ appraisal (2.2.0)
23
+ bundler
24
+ rake
25
+ thor (>= 0.14.0)
26
+ arel (6.0.4)
27
+ builder (3.2.4)
28
+ concurrent-ruby (1.1.6)
29
+ diff-lcs (1.3)
30
+ i18n (0.9.5)
31
+ concurrent-ruby (~> 1.0)
32
+ minitest (5.14.0)
33
+ rake (10.5.0)
34
+ rspec (3.9.0)
35
+ rspec-core (~> 3.9.0)
36
+ rspec-expectations (~> 3.9.0)
37
+ rspec-mocks (~> 3.9.0)
38
+ rspec-core (3.9.1)
39
+ rspec-support (~> 3.9.1)
40
+ rspec-expectations (3.9.0)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.9.0)
43
+ rspec-mocks (3.9.1)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.9.0)
46
+ rspec-support (3.9.2)
47
+ sqlite3 (1.3.13)
48
+ thor (1.0.1)
49
+ thread_safe (0.3.6)
50
+ tzinfo (1.2.6)
51
+ thread_safe (~> 0.1)
52
+
53
+ PLATFORMS
54
+ ruby
55
+
56
+ DEPENDENCIES
57
+ activerecord (~> 4.2.11)
58
+ appraisal
59
+ bundler (~> 1.16)
60
+ keep_defaults!
61
+ rake (~> 10.0)
62
+ rspec (~> 3.0)
63
+ sqlite3 (~> 1.3.6)
64
+
65
+ BUNDLED WITH
66
+ 1.17.3
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.0.7"
6
+ gem "sqlite3", "~> 1.3.6"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,64 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ keep_defaults (0.0.1)
5
+ activerecord (>= 4.2, < 7)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.0.7.2)
11
+ activesupport (= 5.0.7.2)
12
+ activerecord (5.0.7.2)
13
+ activemodel (= 5.0.7.2)
14
+ activesupport (= 5.0.7.2)
15
+ arel (~> 7.0)
16
+ activesupport (5.0.7.2)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ appraisal (2.2.0)
22
+ bundler
23
+ rake
24
+ thor (>= 0.14.0)
25
+ arel (7.1.4)
26
+ concurrent-ruby (1.1.6)
27
+ diff-lcs (1.3)
28
+ i18n (1.8.2)
29
+ concurrent-ruby (~> 1.0)
30
+ minitest (5.14.0)
31
+ rake (10.5.0)
32
+ rspec (3.9.0)
33
+ rspec-core (~> 3.9.0)
34
+ rspec-expectations (~> 3.9.0)
35
+ rspec-mocks (~> 3.9.0)
36
+ rspec-core (3.9.1)
37
+ rspec-support (~> 3.9.1)
38
+ rspec-expectations (3.9.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.9.0)
41
+ rspec-mocks (3.9.1)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.9.0)
44
+ rspec-support (3.9.2)
45
+ sqlite3 (1.3.13)
46
+ thor (1.0.1)
47
+ thread_safe (0.3.6)
48
+ tzinfo (1.2.6)
49
+ thread_safe (~> 0.1)
50
+
51
+ PLATFORMS
52
+ ruby
53
+
54
+ DEPENDENCIES
55
+ activerecord (~> 5.0.7)
56
+ appraisal
57
+ bundler (~> 1.16)
58
+ keep_defaults!
59
+ rake (~> 10.0)
60
+ rspec (~> 3.0)
61
+ sqlite3 (~> 1.3.6)
62
+
63
+ BUNDLED WITH
64
+ 1.17.3
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.1.7"
6
+ gem "sqlite3"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,64 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ keep_defaults (0.0.1)
5
+ activerecord (>= 4.2, < 7)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.1.7)
11
+ activesupport (= 5.1.7)
12
+ activerecord (5.1.7)
13
+ activemodel (= 5.1.7)
14
+ activesupport (= 5.1.7)
15
+ arel (~> 8.0)
16
+ activesupport (5.1.7)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ appraisal (2.2.0)
22
+ bundler
23
+ rake
24
+ thor (>= 0.14.0)
25
+ arel (8.0.0)
26
+ concurrent-ruby (1.1.6)
27
+ diff-lcs (1.3)
28
+ i18n (1.8.2)
29
+ concurrent-ruby (~> 1.0)
30
+ minitest (5.14.0)
31
+ rake (10.5.0)
32
+ rspec (3.9.0)
33
+ rspec-core (~> 3.9.0)
34
+ rspec-expectations (~> 3.9.0)
35
+ rspec-mocks (~> 3.9.0)
36
+ rspec-core (3.9.1)
37
+ rspec-support (~> 3.9.1)
38
+ rspec-expectations (3.9.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.9.0)
41
+ rspec-mocks (3.9.1)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.9.0)
44
+ rspec-support (3.9.2)
45
+ sqlite3 (1.4.2)
46
+ thor (1.0.1)
47
+ thread_safe (0.3.6)
48
+ tzinfo (1.2.6)
49
+ thread_safe (~> 0.1)
50
+
51
+ PLATFORMS
52
+ ruby
53
+
54
+ DEPENDENCIES
55
+ activerecord (~> 5.1.7)
56
+ appraisal
57
+ bundler (~> 1.16)
58
+ keep_defaults!
59
+ rake (~> 10.0)
60
+ rspec (~> 3.0)
61
+ sqlite3
62
+
63
+ BUNDLED WITH
64
+ 1.17.3
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.2.4"
6
+ gem "sqlite3"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,64 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ keep_defaults (0.0.1)
5
+ activerecord (>= 4.2, < 7)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.4.1)
11
+ activesupport (= 5.2.4.1)
12
+ activerecord (5.2.4.1)
13
+ activemodel (= 5.2.4.1)
14
+ activesupport (= 5.2.4.1)
15
+ arel (>= 9.0)
16
+ activesupport (5.2.4.1)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ appraisal (2.2.0)
22
+ bundler
23
+ rake
24
+ thor (>= 0.14.0)
25
+ arel (9.0.0)
26
+ concurrent-ruby (1.1.6)
27
+ diff-lcs (1.3)
28
+ i18n (1.8.2)
29
+ concurrent-ruby (~> 1.0)
30
+ minitest (5.14.0)
31
+ rake (10.5.0)
32
+ rspec (3.9.0)
33
+ rspec-core (~> 3.9.0)
34
+ rspec-expectations (~> 3.9.0)
35
+ rspec-mocks (~> 3.9.0)
36
+ rspec-core (3.9.1)
37
+ rspec-support (~> 3.9.1)
38
+ rspec-expectations (3.9.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.9.0)
41
+ rspec-mocks (3.9.1)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.9.0)
44
+ rspec-support (3.9.2)
45
+ sqlite3 (1.4.2)
46
+ thor (1.0.1)
47
+ thread_safe (0.3.6)
48
+ tzinfo (1.2.6)
49
+ thread_safe (~> 0.1)
50
+
51
+ PLATFORMS
52
+ ruby
53
+
54
+ DEPENDENCIES
55
+ activerecord (~> 5.2.4)
56
+ appraisal
57
+ bundler (~> 1.16)
58
+ keep_defaults!
59
+ rake (~> 10.0)
60
+ rspec (~> 3.0)
61
+ sqlite3
62
+
63
+ BUNDLED WITH
64
+ 1.17.3
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 6.0.2"
6
+ gem "sqlite3"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,64 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ keep_defaults (0.0.1)
5
+ activerecord (>= 4.2, < 7)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (6.0.2.1)
11
+ activesupport (= 6.0.2.1)
12
+ activerecord (6.0.2.1)
13
+ activemodel (= 6.0.2.1)
14
+ activesupport (= 6.0.2.1)
15
+ activesupport (6.0.2.1)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 0.7, < 2)
18
+ minitest (~> 5.1)
19
+ tzinfo (~> 1.1)
20
+ zeitwerk (~> 2.2)
21
+ appraisal (2.2.0)
22
+ bundler
23
+ rake
24
+ thor (>= 0.14.0)
25
+ concurrent-ruby (1.1.6)
26
+ diff-lcs (1.3)
27
+ i18n (1.8.2)
28
+ concurrent-ruby (~> 1.0)
29
+ minitest (5.14.0)
30
+ rake (10.5.0)
31
+ rspec (3.9.0)
32
+ rspec-core (~> 3.9.0)
33
+ rspec-expectations (~> 3.9.0)
34
+ rspec-mocks (~> 3.9.0)
35
+ rspec-core (3.9.1)
36
+ rspec-support (~> 3.9.1)
37
+ rspec-expectations (3.9.0)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.9.0)
40
+ rspec-mocks (3.9.1)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.9.0)
43
+ rspec-support (3.9.2)
44
+ sqlite3 (1.4.2)
45
+ thor (1.0.1)
46
+ thread_safe (0.3.6)
47
+ tzinfo (1.2.6)
48
+ thread_safe (~> 0.1)
49
+ zeitwerk (2.2.2)
50
+
51
+ PLATFORMS
52
+ ruby
53
+
54
+ DEPENDENCIES
55
+ activerecord (~> 6.0.2)
56
+ appraisal
57
+ bundler (~> 1.16)
58
+ keep_defaults!
59
+ rake (~> 10.0)
60
+ rspec (~> 3.0)
61
+ sqlite3
62
+
63
+ BUNDLED WITH
64
+ 1.17.3
@@ -0,0 +1,35 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "keep_defaults/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "keep_defaults"
8
+ spec.version = KeepDefaults::VERSION
9
+ spec.authors = ["sshaw"]
10
+ spec.email = ["skye.shaw@gmail.com"]
11
+
12
+ spec.summary = %q{Prevent ActiveRecord attributes for `not null` columns with default values from being set to `nil`}
13
+ spec.description =<<-DESC
14
+ Prevent ActiveRecord attributes for not null columns with default values from being set to nil.
15
+ Instead of setting/returning nil the column's default value is returned.
16
+ DESC
17
+ spec.homepage = "https://github.com/sshaw/keep_defaults"
18
+ spec.license = "MIT"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency "activerecord", ">= 4.2", "< 7"
30
+
31
+ spec.add_development_dependency "appraisal"
32
+ spec.add_development_dependency "bundler", "~> 1.16"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ end
@@ -0,0 +1,28 @@
1
+ require "keep_defaults/version"
2
+
3
+ module KeepDefaults
4
+ def self.included(klass)
5
+ klass.instance_eval do
6
+ column_defaults.each do |name, default|
7
+ next if default.nil? || columns_hash[name].null
8
+
9
+ # Use Module.new so classes can get this by calling super
10
+ module_eval %{
11
+ include Module.new {
12
+ def #{name}=(value)
13
+ return if value.nil?
14
+ super value
15
+ end
16
+ }
17
+ }, __FILE__, __LINE__ + 1
18
+ end
19
+ end
20
+ end
21
+
22
+ def write_attribute(name, value)
23
+ name = name.to_s if name.is_a?(Symbol)
24
+ name = self.class.attribute_alias(name) if self.class.attribute_alias?(name)
25
+ value = self.class.column_defaults[name] if value.nil? && !self.class.columns_hash[name].null
26
+ super
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module KeepDefaults
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: keep_defaults
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - sshaw
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-02 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: '4.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7'
33
+ - !ruby/object:Gem::Dependency
34
+ name: appraisal
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.16'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.16'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.0'
89
+ description: |2
90
+ Prevent ActiveRecord attributes for not null columns with default values from being set to nil.
91
+ Instead of setting/returning nil the column's default value is returned.
92
+ email:
93
+ - skye.shaw@gmail.com
94
+ executables: []
95
+ extensions: []
96
+ extra_rdoc_files: []
97
+ files:
98
+ - ".gitignore"
99
+ - ".rspec"
100
+ - ".travis.yml"
101
+ - Appraisals
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - gemfiles/.bundle/config
107
+ - gemfiles/4.2.gemfile
108
+ - gemfiles/4.2.gemfile.lock
109
+ - gemfiles/5.0.gemfile
110
+ - gemfiles/5.0.gemfile.lock
111
+ - gemfiles/5.1.gemfile
112
+ - gemfiles/5.1.gemfile.lock
113
+ - gemfiles/5.2.gemfile
114
+ - gemfiles/5.2.gemfile.lock
115
+ - gemfiles/6.0.gemfile
116
+ - gemfiles/6.0.gemfile.lock
117
+ - keep_defaults.gemspec
118
+ - lib/keep_defaults.rb
119
+ - lib/keep_defaults/version.rb
120
+ homepage: https://github.com/sshaw/keep_defaults
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.7.6
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Prevent ActiveRecord attributes for `not null` columns with default values
144
+ from being set to `nil`
145
+ test_files: []