acts_as_replaceable 1.3.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/lib/acts_as_replaceable/acts_as_replaceable.rb +17 -3
- data/spec/acts_as_replaceable_spec.rb +17 -1
- data/spec/spec_helper.rb +1 -0
- metadata +15 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 30bcf32b9f0dfe41412a58f51f84c005042e925f463cc90279a5b036dc8283eb
|
4
|
+
data.tar.gz: d3339e413d4cfcf298655f893990c7b0b020b3f4ce9197fb44c80639a14a9e59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0900b51b609176b16ffd78048bc9ecede2f6a9ceea01a7ace1e86f27740235d3b38a289267cee909b8a82c42a7844cee97713b2080b2368de0fc4e7da269f6fe'
|
7
|
+
data.tar.gz: 7c9ddecdaefc99ede6aa48387d064399b29d754918de655ac8a6d2785e34d6f8bf3b1b205fa389d0e2ff5497b7486d44b137fddd27be7c68bae1193c1c0491b4
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'openssl'
|
2
2
|
require 'timeout'
|
3
3
|
|
4
4
|
module ActsAsReplaceable
|
@@ -40,6 +40,10 @@ module ActsAsReplaceable
|
|
40
40
|
|
41
41
|
module HelperMethods
|
42
42
|
def self.sanitize_attribute_names(klass, *args)
|
43
|
+
unless klass.table_exists?
|
44
|
+
ActiveRecord::Base.logger.warn "(acts_as_replaceable) table `#{klass.table_name}` does not exist so excluding all attribute names"
|
45
|
+
return []
|
46
|
+
end
|
43
47
|
# Intersect the proposed attributes with the column names so we don't start assigning attributes that don't exist. e.g. if the model doesn't have timestamps
|
44
48
|
klass.column_names & args.flatten.compact.collect(&:to_s)
|
45
49
|
end
|
@@ -64,6 +68,8 @@ module ActsAsReplaceable
|
|
64
68
|
sql << "#{attribute_name} IS NULL"
|
65
69
|
end
|
66
70
|
end
|
71
|
+
|
72
|
+
return nil if sql.empty?
|
67
73
|
return [sql.join(' AND ')] + binds
|
68
74
|
end
|
69
75
|
|
@@ -86,7 +92,7 @@ module ActsAsReplaceable
|
|
86
92
|
|
87
93
|
# Searches the database for an existing copies of record
|
88
94
|
def self.find_existing(record)
|
89
|
-
existing = record.class
|
95
|
+
existing = record.class.default_scoped
|
90
96
|
existing = existing.where match_conditions(record)
|
91
97
|
existing = existing.where insensitive_match_conditions(record)
|
92
98
|
end
|
@@ -104,7 +110,7 @@ module ActsAsReplaceable
|
|
104
110
|
# eg. In a multi-threaded environment, 'find_or_create' is prone to failure due to the possibility
|
105
111
|
# that the process is preempted between the 'find' and 'create' logic
|
106
112
|
def self.lock(record, timeout = 20)
|
107
|
-
lock_id = "ActsAsReplaceable/#{Digest::MD5.digest([match_conditions(record), insensitive_match_conditions(record)].inspect)}"
|
113
|
+
lock_id = "ActsAsReplaceable/#{OpenSSL::Digest::MD5.digest([match_conditions(record), insensitive_match_conditions(record)].inspect)}"
|
108
114
|
acquired = false
|
109
115
|
|
110
116
|
# Acquire the lock by atomically incrementing and returning the value to see if we're first
|
@@ -175,6 +181,14 @@ module ActsAsReplaceable
|
|
175
181
|
# Inherit target's attributes for those in acts_as_replaceable_options[:inherit]
|
176
182
|
ActsAsReplaceable::HelperMethods.copy_attributes(acts_as_replaceable_options[:inherit], existing, self)
|
177
183
|
|
184
|
+
# Rails 5 introduced AR::Dirty and started using `mutations_from_database` to
|
185
|
+
# lookup `id_in_database` which is required for the `_update_record` call
|
186
|
+
#
|
187
|
+
# This chunk of code is copied from https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-becomes
|
188
|
+
if existing.respond_to?(:mutations_from_database, true)
|
189
|
+
instance_variable_set("@mutations_from_database", existing.send(:mutations_from_database) || nil)
|
190
|
+
end
|
191
|
+
|
178
192
|
@new_record = false
|
179
193
|
@has_been_replaced = true
|
180
194
|
@has_not_changed = !ActsAsReplaceable::HelperMethods.mark_changes(self, existing)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe '
|
3
|
+
describe 'acts_as_replaceable' do
|
4
4
|
before(:each) do
|
5
5
|
[Material, Item, Person].each(&:destroy_all) # Because we're using sqlite3 and it doesn't support transactional specs (afaik)
|
6
6
|
end
|
@@ -14,6 +14,16 @@ describe 'acts_as_dag' do
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
+
describe "Model" do
|
18
|
+
it 'evaluates without error when no database table exists' do
|
19
|
+
eval("class NoTable < ActiveRecord::Base; end")
|
20
|
+
klass = NoTable
|
21
|
+
|
22
|
+
klass.table_name = "some_table_that_does_not_exist"
|
23
|
+
expect(klass.acts_as_replaceable).to be_nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
17
27
|
describe "Helper Methods" do
|
18
28
|
before(:each) { @record = insert_model(Material, :name => 'glass')}
|
19
29
|
|
@@ -109,6 +119,12 @@ describe 'acts_as_dag' do
|
|
109
119
|
Person.where(:first_name => 'John').count.should == 1
|
110
120
|
end
|
111
121
|
|
122
|
+
it "should correctly detect difference between blank and nil values" do
|
123
|
+
a = Person.create! :first_name => 'John', :last_name => ''
|
124
|
+
a = Person.create! :first_name => 'John', :last_name => nil
|
125
|
+
Person.where(:first_name => 'John').count.should == 2
|
126
|
+
end
|
127
|
+
|
112
128
|
it "should inherit the id of the existing record" do
|
113
129
|
a = Material.create! :name => 'wood'
|
114
130
|
b = Material.create! :name => 'wood'
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_replaceable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicholas Jakobsen
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2024-02-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -17,62 +17,48 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: 4.
|
20
|
+
version: 4.1.8
|
21
21
|
- - "<"
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version:
|
23
|
+
version: '8'
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
26
|
version_requirements: !ruby/object:Gem::Requirement
|
27
27
|
requirements:
|
28
28
|
- - ">="
|
29
29
|
- !ruby/object:Gem::Version
|
30
|
-
version: 4.
|
30
|
+
version: 4.1.8
|
31
31
|
- - "<"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
34
|
-
- !ruby/object:Gem::Dependency
|
35
|
-
name: bundler
|
36
|
-
requirement: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '1.5'
|
41
|
-
type: :development
|
42
|
-
prerelease: false
|
43
|
-
version_requirements: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '1.5'
|
33
|
+
version: '8'
|
48
34
|
- !ruby/object:Gem::Dependency
|
49
35
|
name: sqlite3
|
50
36
|
requirement: !ruby/object:Gem::Requirement
|
51
37
|
requirements:
|
52
|
-
- - "
|
38
|
+
- - ">="
|
53
39
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
40
|
+
version: '0'
|
55
41
|
type: :development
|
56
42
|
prerelease: false
|
57
43
|
version_requirements: !ruby/object:Gem::Requirement
|
58
44
|
requirements:
|
59
|
-
- - "
|
45
|
+
- - ">="
|
60
46
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
47
|
+
version: '0'
|
62
48
|
- !ruby/object:Gem::Dependency
|
63
49
|
name: rspec-rails
|
64
50
|
requirement: !ruby/object:Gem::Requirement
|
65
51
|
requirements:
|
66
|
-
- - "
|
52
|
+
- - ">="
|
67
53
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
54
|
+
version: '0'
|
69
55
|
type: :development
|
70
56
|
prerelease: false
|
71
57
|
version_requirements: !ruby/object:Gem::Requirement
|
72
58
|
requirements:
|
73
|
-
- - "
|
59
|
+
- - ">="
|
74
60
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
61
|
+
version: '0'
|
76
62
|
description:
|
77
63
|
email: technical@rrnpilot.org
|
78
64
|
executables: []
|
@@ -102,8 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
88
|
- !ruby/object:Gem::Version
|
103
89
|
version: '0'
|
104
90
|
requirements: []
|
105
|
-
|
106
|
-
rubygems_version: 2.4.7
|
91
|
+
rubygems_version: 3.3.23
|
107
92
|
signing_key:
|
108
93
|
specification_version: 4
|
109
94
|
summary: Overloads the create_or_update_without_callbacks method to allow duplicate
|