activerecord-define_nils 0.0.3 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +6 -14
- data/README.md +10 -8
- data/Rakefile +14 -8
- data/lib/activerecord-define_nils.rb +2 -0
- data/lib/activerecord-define_nils/early_rails4_model.rb +82 -0
- data/lib/activerecord-define_nils/model.rb +5 -5
- data/lib/activerecord-define_nils/rails3_model.rb +82 -0
- data/lib/activerecord-define_nils/railtie.rb +9 -1
- data/lib/activerecord-define_nils/version.rb +1 -1
- metadata +13 -11
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
metadata.gz: !binary |-
|
9
|
-
MTVmMTk4MTFmY2NhMzk1OWRkMjY4OWI3ZDFmNmNhODRiOTI5Mjk1OGE2YzYx
|
10
|
-
NmUzYTgzZDM1YjcxOWJhOTE1MTM0MDYzZjRiZWEyZmU3OWJlY2RhNzAxZjVj
|
11
|
-
NGNhNmExMjZjZmIyODljNzRkZmI5YjJmZjUxNGI4YzIyOTE1NTU=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
MWMyNThlNzMxOWE5NWViYmU4OWJhYjBlZTNkNTQ1OTFkNmU5MWEyMGE2NDIy
|
14
|
-
OGRlNTQxNjMwODE3OTlmZDRiZjllZDNkMmM2YTM2MDg0MmYzODcwNDhiZGEw
|
15
|
-
ZjE3NDJmZDIwYTlmMGE3MjgwYjFkMjE0MjJjMGM2MDI0MmNmNGY=
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9d412a0dfa95d7673ea71b6b2282993c5bc8ff32
|
4
|
+
data.tar.gz: 43bb8fc329da8e22b8208bdd5f9c14ee5073df43
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 64cfaeeba84005015e95bc5ba0682806070f5a5076cc43fadd921872ed5331a00edc8cad47495cb094aabdc0d7e8364b91ee4510a562c0781bbba81bef93ab71
|
7
|
+
data.tar.gz: 112ecc75ce20d42b67d7df337612948028786f4d7e2cfb3cc27a945ccbcfad862ef0a9f220df294f604e9f04ea1d5e397640599b632a092e85ffad0344c45a7d
|
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
Define Nils
|
2
|
-
|
1
|
+
## Define Nils
|
2
|
+
|
3
|
+
Tested with Rails/ActiveRecord 3.1, 3.2, 4.0, 4.1, and 4.2.
|
3
4
|
|
4
5
|
If working with a legacy database used by another application that uses a non-null value to mean null, you may need to translate certain values to nil to make them more normal to work with in Ruby/Rails and then save nil as a specific value.
|
5
6
|
|
@@ -31,10 +32,6 @@ In your ActiveRecord/Rails 3.1+ project, add this to your Gemfile:
|
|
31
32
|
|
32
33
|
gem 'activerecord-define_nils'
|
33
34
|
|
34
|
-
For the development version:
|
35
|
-
|
36
|
-
gem 'activerecord-define_nils', :git => 'git://github.com/garysweaver/activerecord-define_nils.git'
|
37
|
-
|
38
35
|
Then run:
|
39
36
|
|
40
37
|
bundle install
|
@@ -45,8 +42,13 @@ If you are on Rails, the concern `ActiveRecordDefineNils::Model` is included aut
|
|
45
42
|
|
46
43
|
Be sure to place `define_nils` above your `belongs_to` associations in the model class or those associations will query on unaltered foreign key values, which is probably not what you want.
|
47
44
|
|
45
|
+
### Authors
|
46
|
+
|
47
|
+
This was written by [FineLine Prototyping, Inc.](http://www.finelineprototyping.com) by the following contributors:
|
48
|
+
* Gary Weaver (https://github.com/garysweaver)
|
49
|
+
|
48
50
|
### License
|
49
51
|
|
50
|
-
Copyright (c) 2013
|
52
|
+
Copyright (c) 2013 FineLine Prototyping, Inc., released under the [MIT license][lic].
|
51
53
|
|
52
|
-
[lic]: http://github.com/
|
54
|
+
[lic]: http://github.com/FineLinePrototyping/activerecord-define_nils/blob/master/LICENSE
|
data/Rakefile
CHANGED
@@ -1,10 +1,16 @@
|
|
1
|
-
require '
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
require 'appraisal'
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
5
|
+
# Override the default task
|
6
|
+
task :default => [] # Just in case it hasn't already been set
|
7
|
+
Rake::Task[:default].clear
|
8
|
+
task :default => :appraise
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
task :appraise do |t|
|
11
|
+
if ENV['BUNDLE_GEMFILE'] =~ /gemfiles/
|
12
|
+
load 'test/define_nils_test.rb'
|
13
|
+
else
|
14
|
+
exec 'appraisal install && rake appraisal'
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module ActiveRecordDefineNils
|
2
|
+
module EarlyRails3Model
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
class_attribute :nil_definitions, instance_writer: false
|
7
|
+
class_attribute :nil_saved_as, instance_writer: false
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
def define_nils(options)
|
13
|
+
raise ArgumentError.new("define_nils takes a hash, but got #{options.inspect}") unless options.is_a?(Hash)
|
14
|
+
if options.has_key?(:as) && options.has_key?(:for)
|
15
|
+
self.nil_saved_as ||= {}
|
16
|
+
self.nil_definitions ||= {}
|
17
|
+
as_vals = Array.wrap(options[:as])
|
18
|
+
raise ArgumentError.new("define_nils must supply at least one value for :as, but got #{options.inspect}") if as_vals.size == 0
|
19
|
+
Array.wrap(options[:for]).each do |attr_name|
|
20
|
+
attr_sym = attr_name.to_sym
|
21
|
+
self.nil_saved_as[attr_sym] = options.key?(:saving_as) ? options[:saving_as] : as_vals[0]
|
22
|
+
self.nil_definitions[attr_sym] = as_vals
|
23
|
+
class_eval "def #{attr_name}; value = super(); self.nil_definitions && self.nil_definitions[#{attr_sym.inspect}] && self.nil_definitions[#{attr_sym.inspect}].include?(value) ? nil : value; end"
|
24
|
+
end
|
25
|
+
else
|
26
|
+
raise ArgumentError.new("define_nils takes a hash with :as and :for keys, but got #{options.inspect}")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def belongs_to(*args)
|
31
|
+
super(*args)
|
32
|
+
return unless self.nil_definitions
|
33
|
+
# after belongs_to is finished, we wrap it
|
34
|
+
self.nil_definitions.keys.each do |attr_sym|
|
35
|
+
self.reflections.collect {|association_name, reflection|
|
36
|
+
if reflection.macro == :belongs_to && reflection.foreign_key.to_sym == attr_sym
|
37
|
+
class_eval "def #{association_name}; return nil if __send__(#{attr_sym.inspect}).nil?; super(); end"
|
38
|
+
end
|
39
|
+
}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
def read_attribute(attr_name)
|
46
|
+
value = super(attr_name)
|
47
|
+
(@define_nils_no_read_mod ||= false) ? value : (self.nil_definitions && self.nil_definitions[attr_name.to_sym] && self.nil_definitions[attr_name.to_sym].include?(value) ? nil : value)
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def create_record
|
53
|
+
@define_nils_no_read_mod = true
|
54
|
+
if self.nil_saved_as
|
55
|
+
self.nil_saved_as.each do |column, translated_nil|
|
56
|
+
if respond_to?(column) && respond_to?("#{column}=") && self.__send__(column).nil?
|
57
|
+
write_attribute(column.to_s, translated_nil)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
super
|
63
|
+
ensure
|
64
|
+
@define_nils_no_read_mod = false
|
65
|
+
end
|
66
|
+
|
67
|
+
def create_record(*args)
|
68
|
+
@define_nils_no_read_mod = true
|
69
|
+
if self.nil_saved_as
|
70
|
+
self.nil_saved_as.each do |column, translated_nil|
|
71
|
+
if respond_to?(column) && respond_to?("#{column}=") && self.__send__(column).nil?
|
72
|
+
write_attribute(column.to_s, translated_nil)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
super
|
78
|
+
ensure
|
79
|
+
@define_nils_no_read_mod = false
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -18,7 +18,7 @@ module ActiveRecordDefineNils
|
|
18
18
|
raise ArgumentError.new("define_nils must supply at least one value for :as, but got #{options.inspect}") if as_vals.size == 0
|
19
19
|
Array.wrap(options[:for]).each do |attr_name|
|
20
20
|
attr_sym = attr_name.to_sym
|
21
|
-
self.nil_saved_as[attr_sym] = options[:saving_as]
|
21
|
+
self.nil_saved_as[attr_sym] = options.key?(:saving_as) ? options[:saving_as] : as_vals[0]
|
22
22
|
self.nil_definitions[attr_sym] = as_vals
|
23
23
|
class_eval "def #{attr_name}; value = super(); self.nil_definitions && self.nil_definitions[#{attr_sym.inspect}] && self.nil_definitions[#{attr_sym.inspect}].include?(value) ? nil : value; end"
|
24
24
|
end
|
@@ -49,7 +49,7 @@ module ActiveRecordDefineNils
|
|
49
49
|
|
50
50
|
private
|
51
51
|
|
52
|
-
def
|
52
|
+
def _create_record
|
53
53
|
@define_nils_no_read_mod = true
|
54
54
|
if self.nil_saved_as
|
55
55
|
self.nil_saved_as.each do |column, translated_nil|
|
@@ -64,9 +64,9 @@ module ActiveRecordDefineNils
|
|
64
64
|
@define_nils_no_read_mod = false
|
65
65
|
end
|
66
66
|
|
67
|
-
def
|
67
|
+
def _update_record(*args, touch: true, **options)
|
68
68
|
@define_nils_no_read_mod = true
|
69
|
-
if self.nil_saved_as
|
69
|
+
if touch && self.nil_saved_as
|
70
70
|
self.nil_saved_as.each do |column, translated_nil|
|
71
71
|
if respond_to?(column) && respond_to?("#{column}=") && self.__send__(column).nil?
|
72
72
|
write_attribute(column.to_s, translated_nil)
|
@@ -74,7 +74,7 @@ module ActiveRecordDefineNils
|
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
77
|
-
super
|
77
|
+
super(*args, touch, **options)
|
78
78
|
ensure
|
79
79
|
@define_nils_no_read_mod = false
|
80
80
|
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module ActiveRecordDefineNils
|
2
|
+
module Rails3Model
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
class_attribute :nil_definitions, instance_writer: false
|
7
|
+
class_attribute :nil_saved_as, instance_writer: false
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
def define_nils(options)
|
13
|
+
raise ArgumentError.new("define_nils takes a hash, but got #{options.inspect}") unless options.is_a?(Hash)
|
14
|
+
if options.has_key?(:as) && options.has_key?(:for)
|
15
|
+
self.nil_saved_as ||= {}
|
16
|
+
self.nil_definitions ||= {}
|
17
|
+
as_vals = Array.wrap(options[:as])
|
18
|
+
raise ArgumentError.new("define_nils must supply at least one value for :as, but got #{options.inspect}") if as_vals.size == 0
|
19
|
+
Array.wrap(options[:for]).each do |attr_name|
|
20
|
+
attr_sym = attr_name.to_sym
|
21
|
+
self.nil_saved_as[attr_sym] = options.key?(:saving_as) ? options[:saving_as] : as_vals[0]
|
22
|
+
self.nil_definitions[attr_sym] = as_vals
|
23
|
+
class_eval "def #{attr_name}; value = super(); self.nil_definitions && self.nil_definitions[#{attr_sym.inspect}] && self.nil_definitions[#{attr_sym.inspect}].include?(value) ? nil : value; end"
|
24
|
+
end
|
25
|
+
else
|
26
|
+
raise ArgumentError.new("define_nils takes a hash with :as and :for keys, but got #{options.inspect}")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def belongs_to(*args)
|
31
|
+
super(*args)
|
32
|
+
return unless self.nil_definitions
|
33
|
+
# after belongs_to is finished, we wrap it
|
34
|
+
self.nil_definitions.keys.each do |attr_sym|
|
35
|
+
self.reflections.collect {|association_name, reflection|
|
36
|
+
if reflection.macro == :belongs_to && reflection.foreign_key.to_sym == attr_sym
|
37
|
+
class_eval "def #{association_name}; return nil if __send__(#{attr_sym.inspect}).nil?; super(); end"
|
38
|
+
end
|
39
|
+
}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
def read_attribute(attr_name)
|
46
|
+
value = super(attr_name)
|
47
|
+
(@define_nils_no_read_mod ||= false) ? value : (self.nil_definitions && self.nil_definitions[attr_name.to_sym] && self.nil_definitions[attr_name.to_sym].include?(value) ? nil : value)
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def create
|
53
|
+
@define_nils_no_read_mod = true
|
54
|
+
if self.nil_saved_as
|
55
|
+
self.nil_saved_as.each do |column, translated_nil|
|
56
|
+
if respond_to?(column) && respond_to?("#{column}=") && self.__send__(column).nil?
|
57
|
+
write_attribute(column.to_s, translated_nil)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
super
|
63
|
+
ensure
|
64
|
+
@define_nils_no_read_mod = false
|
65
|
+
end
|
66
|
+
|
67
|
+
def update(*args)
|
68
|
+
@define_nils_no_read_mod = true
|
69
|
+
if self.nil_saved_as
|
70
|
+
self.nil_saved_as.each do |column, translated_nil|
|
71
|
+
if respond_to?(column) && respond_to?("#{column}=") && self.__send__(column).nil?
|
72
|
+
write_attribute(column.to_s, translated_nil)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
super
|
78
|
+
ensure
|
79
|
+
@define_nils_no_read_mod = false
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -4,7 +4,15 @@ module ActiveRecordDefineNils
|
|
4
4
|
class Railtie < Rails::Railtie
|
5
5
|
initializer "define_nils.active_record" do
|
6
6
|
ActiveSupport.on_load(:active_record) do
|
7
|
-
|
7
|
+
if Rails::VERSION::MAJOR == 4
|
8
|
+
if Rails::VERSION::MINOR < 2
|
9
|
+
include ::ActiveRecordDefineNils::EarlyRails4Model
|
10
|
+
else
|
11
|
+
include ::ActiveRecordDefineNils::Model
|
12
|
+
end
|
13
|
+
elsif Rails::VERSION::MAJOR == 3
|
14
|
+
include ::ActiveRecordDefineNils::Rails3Model
|
15
|
+
end
|
8
16
|
end
|
9
17
|
end
|
10
18
|
end
|
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-define_nils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gary S. Weaver
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
description: Allows you to redefine what is translated to nil on read and what is
|
@@ -32,13 +32,15 @@ executables: []
|
|
32
32
|
extensions: []
|
33
33
|
extra_rdoc_files: []
|
34
34
|
files:
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- lib/activerecord-define_nils.rb
|
38
|
+
- lib/activerecord-define_nils/early_rails4_model.rb
|
35
39
|
- lib/activerecord-define_nils/model.rb
|
40
|
+
- lib/activerecord-define_nils/rails3_model.rb
|
36
41
|
- lib/activerecord-define_nils/railtie.rb
|
37
42
|
- lib/activerecord-define_nils/version.rb
|
38
|
-
|
39
|
-
- Rakefile
|
40
|
-
- README.md
|
41
|
-
homepage: https://github.com/garysweaver/activerecord-define_nils
|
43
|
+
homepage: https://github.com/FineLinePrototyping/activerecord-define_nils
|
42
44
|
licenses:
|
43
45
|
- MIT
|
44
46
|
metadata: {}
|
@@ -48,17 +50,17 @@ require_paths:
|
|
48
50
|
- lib
|
49
51
|
required_ruby_version: !ruby/object:Gem::Requirement
|
50
52
|
requirements:
|
51
|
-
- -
|
53
|
+
- - ">="
|
52
54
|
- !ruby/object:Gem::Version
|
53
55
|
version: '0'
|
54
56
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
57
|
requirements:
|
56
|
-
- -
|
58
|
+
- - ">="
|
57
59
|
- !ruby/object:Gem::Version
|
58
60
|
version: '0'
|
59
61
|
requirements: []
|
60
62
|
rubyforge_project:
|
61
|
-
rubygems_version: 2.
|
63
|
+
rubygems_version: 2.4.5.1
|
62
64
|
signing_key:
|
63
65
|
specification_version: 4
|
64
66
|
summary: Define nils for ActiveRecord 3.x/4.x.
|