copyable 0.2.0 → 0.3.3

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: 6c394690b8922c1b1d88ff6665fb2b4e640d8459
4
- data.tar.gz: c7f7852188d8a62166b93063d85c77b8a1b1f31a
2
+ SHA256:
3
+ metadata.gz: 211edbc077e2ffc3c48116862c8d585ad8275fb609be84193edeb13627869133
4
+ data.tar.gz: be4b0cbed19a6fb04e4cf031d77cda9666a54b5d32854781a3ab01cd36a5734d
5
5
  SHA512:
6
- metadata.gz: 5f5f9657c9506dd9583327da181245782944a57a54ced61627c87b3794aa851ba87e5a656aaeb391170c1b0cf31f5442a15398baa2931d2f7f2c918b88626f4d
7
- data.tar.gz: e7dc40c25c3b728d940c648b6a528cc9ba2b740d85b12307b30e40dc3135bed51387b6c0f005036ba8279886b8d1ff6b9efec652a58b7b9b105e0c97979d8dc8
6
+ metadata.gz: 75be387a12e69cc325dff83e9c8d12c27d7f1ac4809fc7e4054a81446daa3dda04c51e24bf990f6fe30d76a395e823f8dc1bc3be4d756cf1020126ee6633e439
7
+ data.tar.gz: caa074106ed16b43536d6aabbd9177bc767c832fb9ba30dcd32c23f2b2cf6dfba3627c0fe5bed80e4cbfa58afe474658c59d31c1adada43c5039f1d8a194393b
data/.travis.yml CHANGED
@@ -1,3 +1,3 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0.0
3
+ - 2.7.2
data/README.md CHANGED
@@ -121,16 +121,9 @@ The advice must be one of the following:
121
121
 
122
122
  ## Callbacks
123
123
 
124
- It depends on the situation as to whether you would want a particular callback to be fired when a model is copied. Since the logic of callbacks is situational, Copyable makes the decision to completely disable all callbacks and observers for the duration of the `create_copy!` method. The only exception are callbacks and observers related to validation.
125
-
126
- To make it easier to reason about the code, and for the sake of being obvious, every copyable declaration *must* include a declaration called `disable_all_callbacks_and_observers_except_validate`. This declaration itself does not do anything; it exists as documentation.
127
-
128
- copyable do
129
- disable_all_callbacks_and_observers_except_validate
130
- ...
131
- end
132
-
133
-
124
+ It depends on the situation as to whether you would want a particular callback to be fired when a model is copied. Since the logic of callbacks is situational, Copyable makes the decision to bypass callbacks
125
+ when saving the copied models by using raw SQL insert statements during the copy. It will check
126
+ validations unless `skip_validations` is passed to `create_copy`.
134
127
 
135
128
  ## The After Copy Declaration
136
129
 
@@ -267,4 +260,4 @@ So the recommended approach is to use `after_copy` to tweak the columns of the c
267
260
 
268
261
  ## About
269
262
 
270
- Copyable was developed at [District Management Group](https://dmgroupK12.com).
263
+ Copyable was developed at [District Management Group](https://dmgroupK12.com).
data/copyable.gemspec CHANGED
@@ -18,11 +18,11 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "activerecord", "~>4.1.0"
21
+ spec.add_dependency "activerecord", ">= 4.2", "< 6"
22
22
 
23
- spec.add_development_dependency "database_cleaner", "~> 1.4.0"
24
- spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "database_cleaner", "~> 2"
24
+ spec.add_development_dependency "bundler"
25
25
  spec.add_development_dependency "rake"
26
26
  spec.add_development_dependency "rspec"
27
- spec.add_development_dependency "sqlite3"
27
+ spec.add_development_dependency "sqlite3", "~> 1.3.6"
28
28
  end
@@ -11,7 +11,7 @@ module Copyable
11
11
  @skip_associations = skip_associations
12
12
  @global_override = global_override
13
13
  association_list.each do |assoc_name, advice|
14
- association = original_model.class.reflections[assoc_name.to_sym]
14
+ association = original_model.class.reflections[assoc_name.to_s]
15
15
  check_advice(association, advice, original_model)
16
16
  unless advice == :do_not_copy || skip_associations.include?(assoc_name.to_sym)
17
17
  copy_association(association, original_model, new_model)
@@ -28,14 +28,14 @@ module Copyable
28
28
  message << "has unrecognized advice '#{advice}'."
29
29
  raise AssociationError.new(message)
30
30
  end
31
- if association.macro == :has_and_belongs_to_many && advice == :copy
31
+ if association.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection) && advice == :copy
32
32
  message = "Error in copyable:associations of "
33
33
  message << "#{original_model.class.name}: the association '#{association.name}' "
34
34
  message << "only supports the :copy_only_habtm_join_records advice, not the :copy advice, "
35
35
  message << "because it is a has_and_belongs_to_many association."
36
36
  raise AssociationError.new(message)
37
37
  end
38
- if association.macro != :has_and_belongs_to_many && advice == :copy_only_habtm_join_records
38
+ if !association.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection) && advice == :copy_only_habtm_join_records
39
39
  message = "Error in copyable:associations of "
40
40
  message << "#{original_model.class.name}: the association '#{association.name}' "
41
41
  message << "only supports the :copy advice, not the :copy_only_habtm_join_records advice, "
@@ -45,15 +45,14 @@ module Copyable
45
45
  end
46
46
 
47
47
  def copy_association(association, original_model, new_model)
48
- case association.macro
49
- when :has_many
48
+ if association.is_a?(ActiveRecord::Reflection::HasManyReflection)
50
49
  copy_has_many(association, original_model, new_model)
51
- when :has_one
50
+ elsif association.is_a?(ActiveRecord::Reflection::HasOneReflection)
52
51
  copy_has_one(association, original_model, new_model)
53
- when :has_and_belongs_to_many
52
+ elsif association.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection)
54
53
  copy_habtm(association, original_model, new_model)
55
54
  else
56
- raise "Unsupported association #{association.macro}" # should never happen, since we filter out belongs_to
55
+ raise "Unsupported association #{association.class}" # should never happen, since we filter out belongs_to
57
56
  end
58
57
  end
59
58
 
@@ -16,30 +16,33 @@ module Copyable
16
16
 
17
17
  def self.disable_all_callbacks(klass)
18
18
  klass.class_eval do
19
- # Don't do it if it was already done
20
- return if self.method_defined? :__disabled__run_callbacks
21
-
22
- alias_method :__disabled__run_callbacks, :run_callbacks
23
- # We are violently duck-punching ActiveRecord because ActiveRecord
24
- # gives us no way to turn off callbacks. My apologies to the
25
- # squeamish.
26
- def run_callbacks(kind, *args, &block)
27
- if block_given?
28
- yield
29
- else
30
- true
31
- end
19
+ return if self.method_defined? :old_save! # Don't do this more than once
20
+
21
+ @all_callbacks_disabled = true
22
+ class << self
23
+ attr_reader :all_callbacks_disabled
24
+ end
25
+
26
+ alias_method :old_save!, :save!
27
+
28
+ # Hold my beer while we bypass all the Rails callbacks
29
+ # in favor of some raw SQL
30
+ def save!
31
+ Copyable::Saver.save!(self)
32
32
  end
33
33
  end
34
34
  end
35
35
 
36
36
  def self.reenable_all_callbacks(klass)
37
37
  klass.class_eval do
38
- # Only do it if the disabled callbacks are defined
39
- return unless self.method_defined? :__disabled__run_callbacks
38
+ return unless self.method_defined? :old_save! # Don't do this more than once
39
+ @all_callbacks_disabled = false
40
+ class << self
41
+ attr_reader :all_callbacks_disabled
42
+ end
40
43
 
41
- alias_method :run_callbacks, :__disabled__run_callbacks
42
- remove_method :__disabled__run_callbacks
44
+ alias_method :save!, :old_save!
45
+ remove_method :old_save!
43
46
  end
44
47
  end
45
48
 
@@ -1,19 +1,27 @@
1
1
  module Copyable
2
2
  class Saver
3
3
 
4
+ def self.direct_sql_insert!(new_model)
5
+ new_model.send(:_create_record) # bypass all callbacks and validations
6
+ end
7
+
8
+ def self.direct_sql_update!(new_model)
9
+ new_model.send(:_update_record) # bypass all callbacks and validations
10
+ end
11
+
4
12
  # this is the algorithm for saving the new record
5
- def self.save!(new_model, skip_validations)
6
- unless skip_validations
7
- ModelHooks.reenable!(new_model.class) # we must re-enable or validation does not work
8
- if !new_model.valid?(:create)
9
- ModelHooks.disable!(new_model.class)
10
- raise(ActiveRecord::RecordInvalid.new(new_model))
11
- else
12
- ModelHooks.disable!(new_model.class)
13
- end
13
+ def self.save!(new_model, skip_validations=false)
14
+ unless skip_validations || new_model.valid?(:create)
15
+ raise(ActiveRecord::RecordInvalid.new(new_model))
14
16
  end
15
- new_model.save!
16
- end
17
17
 
18
+ if new_model.class.all_callbacks_disabled && new_model.id.nil?
19
+ self.direct_sql_insert!(new_model)
20
+ elsif new_model.class.all_callbacks_disabled
21
+ self.direct_sql_update!(new_model)
22
+ else
23
+ new_model.save!
24
+ end
25
+ end
18
26
  end
19
27
  end
@@ -10,7 +10,8 @@ module Copyable
10
10
  def expected_entries
11
11
  all_associations = model_class.reflect_on_all_associations
12
12
  required_associations = all_associations.select do |ass|
13
- (ass.macro != :belongs_to) && ass.options[:through].blank?
13
+ !ass.is_a?(ActiveRecord::Reflection::BelongsToReflection) &&
14
+ !ass.is_a?(ActiveRecord::Reflection::ThroughReflection)
14
15
  end
15
16
  required_associations.map(&:name).map(&:to_s)
16
17
  end
@@ -1,3 +1,3 @@
1
1
  module Copyable
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.3"
3
3
  end
@@ -39,7 +39,8 @@ task :copyable => :environment do
39
39
 
40
40
  all_associations = model_class.reflect_on_all_associations
41
41
  required_associations = all_associations.select do |ass|
42
- (ass.macro != :belongs_to) && ass.options[:through].blank?
42
+ !ass.is_a?(ActiveRecord::Reflection::BelongsToReflection) &&
43
+ !ass.is_a?(ActiveRecord::Reflection::ThroughReflection)
43
44
  end
44
45
  associations = required_associations.map(&:name).map(&:to_s)
45
46
  max_length = associations.map(&:length).max
@@ -11,14 +11,14 @@ module Copyable
11
11
 
12
12
  create_table :copyable_albums, force: true do |t|
13
13
  t.string :name, null: false
14
- t.timestamps
14
+ t.timestamps null: false
15
15
  end
16
16
 
17
17
  create_table :copyable_amenities, force: true do |t|
18
18
  t.string :name
19
19
  t.integer :copyable_vehicle_id
20
20
  t.integer :copyable_owner_id
21
- t.timestamps
21
+ t.timestamps null: false
22
22
  end
23
23
 
24
24
  create_table :copyable_addresses, force: true do |t|
@@ -27,30 +27,30 @@ module Copyable
27
27
  t.string :city, null: false
28
28
  t.string :state, null: false
29
29
  t.integer :copyable_pet_profile_id, null: false
30
- t.timestamps
30
+ t.timestamps null: false
31
31
  end
32
32
 
33
33
  create_table :copyable_cars, force: true do |t|
34
34
  t.string :make, null: false
35
35
  t.string :model, null: false
36
36
  t.integer :year, null: false
37
- t.timestamps
37
+ t.timestamps null: false
38
38
  end
39
39
 
40
40
  create_table :copyable_coins, force: true do |t|
41
41
  t.string :kind
42
42
  t.integer :year
43
- t.timestamps
43
+ t.timestamps null: false
44
44
  end
45
45
 
46
46
  create_table :copyable_owners, force: true do |t|
47
47
  t.string :name, null: false
48
- t.timestamps
48
+ t.timestamps null: false
49
49
  end
50
50
 
51
51
  create_table :copyable_pet_foods, force: true do |t|
52
52
  t.string :name, null: false
53
- t.timestamps
53
+ t.timestamps null: false
54
54
  end
55
55
 
56
56
  create_table :copyable_pet_foods_pets, force: true, id: false do |t|
@@ -62,31 +62,31 @@ module Copyable
62
62
  t.string :description, null: false
63
63
  t.string :nickname
64
64
  t.integer :copyable_pet_id, null: false
65
- t.timestamps
65
+ t.timestamps null: false
66
66
  end
67
67
 
68
68
  create_table :copyable_pet_sitters, force: true do |t|
69
69
  t.string :name, null: false
70
- t.timestamps
70
+ t.timestamps null: false
71
71
  end
72
72
 
73
73
  create_table :copyable_pet_sitting_patronages, force: true do |t|
74
74
  t.integer :copyable_pet_id, null: false
75
75
  t.integer :copyable_pet_sitter_id, null: false
76
- t.timestamps
76
+ t.timestamps null: false
77
77
  end
78
78
 
79
79
  create_table :copyable_pet_tags, force: true do |t|
80
80
  t.string :registered_name, null: false
81
81
  t.integer :copyable_pet_id, null: false
82
- t.timestamps
82
+ t.timestamps null: false
83
83
  end
84
84
 
85
85
  create_table :copyable_pets, force: true do |t|
86
86
  t.string :name, null: false
87
87
  t.string :kind, null: false
88
88
  t.integer :birth_year
89
- t.timestamps
89
+ t.timestamps null: false
90
90
  end
91
91
 
92
92
  create_table :copyable_pictures, force: true do |t|
@@ -94,36 +94,36 @@ module Copyable
94
94
  t.integer :imageable_id
95
95
  t.string :imageable_type
96
96
  t.integer :picture_album_id
97
- t.timestamps
97
+ t.timestamps null: false
98
98
  end
99
99
 
100
100
  create_table :copyable_products, force: true do |t|
101
101
  t.string :name
102
- t.timestamps
102
+ t.timestamps null: false
103
103
  end
104
104
 
105
105
  create_table :copyable_toys, force: true do |t|
106
106
  t.string :name
107
107
  t.string :kind
108
108
  t.integer :copyable_pet_id
109
- t.timestamps
109
+ t.timestamps null: false
110
110
  end
111
111
 
112
112
  create_table :copyable_trees, force: true do |t|
113
113
  t.string :kind
114
- t.timestamps
114
+ t.timestamps null: false
115
115
  end
116
116
 
117
117
  create_table :copyable_vehicles, force: true do |t|
118
118
  t.string :name
119
119
  t.integer :copyable_owner_id
120
- t.timestamps
120
+ t.timestamps null: false
121
121
  end
122
122
 
123
123
  create_table :copyable_warranties, force: true do |t|
124
124
  t.string :name
125
125
  t.integer :copyable_amenity_id
126
- t.timestamps
126
+ t.timestamps null: false
127
127
  end
128
128
 
129
129
  end
@@ -9,85 +9,31 @@ describe Copyable::ModelHooks do
9
9
  before(:each) do
10
10
  Copyable::ModelHooks.disable!(CopyableTree)
11
11
  end
12
+
12
13
  after(:each) do
13
14
  Copyable::ModelHooks.reenable!(CopyableTree)
14
15
  end
15
- it 'should prevent callbacks from executing' do
16
- expect {
17
- CopyableTree.create!(kind: 'magnolia')
18
- }.to_not raise_error
16
+
17
+ it 'defines an instance variable on the class' do
18
+ expect(CopyableTree.all_callbacks_disabled).to eq(true)
19
19
  end
20
+
20
21
  it 'should not prevent model actions from executing' do
21
22
  expect(CopyableTree.count).to eq(0)
22
- CopyableTree.create!(kind: 'magnolia')
23
- expect(CopyableTree.count).to eq(1)
24
23
  end
25
24
  end
26
25
 
27
26
  describe '.reenable!' do
28
- it 'should allow callbacks to execute again' do
29
- Copyable::ModelHooks.disable!(CopyableTree)
27
+ it 'defines an instance variable on the class' do
30
28
  Copyable::ModelHooks.reenable!(CopyableTree)
31
- expect {
32
- CopyableTree.create!(kind: 'magnolia')
33
- }.to raise_error(RuntimeError, "callback2 called")
29
+ expect(CopyableTree.all_callbacks_disabled).to eq(false)
34
30
  end
35
- end
36
- end
37
-
38
- context 'validations' do
39
31
 
40
- # Note: the relevant model and observer class is defined in helper/test_models.rb
41
-
42
- describe '.disable!' do
43
- before(:each) do
44
- Copyable::ModelHooks.disable!(CopyableCoin)
45
- end
46
- after(:each) do
47
- Copyable::ModelHooks.reenable!(CopyableCoin)
48
- end
49
- it 'should prevent validations from executing' do
50
- expect {
51
- CopyableCoin.create!(year: -10)
52
- }.to_not raise_error
53
- end
54
- it 'should not prevent model actions from executing' do
55
- expect(CopyableCoin.count).to eq(0)
56
- CopyableCoin.create!(year: -10)
57
- expect(CopyableCoin.count).to eq(1)
58
- end
59
- end
60
-
61
- describe '.reenable!' do
62
- it 'should allow validations to execute again' do
63
- Copyable::ModelHooks.disable!(CopyableCoin)
64
- Copyable::ModelHooks.reenable!(CopyableCoin)
32
+ it 'should allow callbacks to execute again' do
65
33
  expect {
66
- CopyableCoin.create!(year: -10)
67
- }.to raise_error(ActiveRecord::RecordInvalid)
34
+ CopyableTree.create!(kind: 'magnolia')
35
+ }.to raise_error(RuntimeError, "callback2 called")
68
36
  end
69
37
  end
70
38
  end
71
-
72
- describe 'nested disables and enables' do
73
- it 'should allow callbacks to execute again' do
74
- Copyable::ModelHooks.disable!(CopyableTree)
75
- expect { CopyableTree.create!(kind: 'magnolia') }.to_not raise_error
76
-
77
- Copyable::ModelHooks.disable!(CopyableCoin)
78
- expect { CopyableCoin.create!(year: -10) }.to_not raise_error
79
-
80
- Copyable::ModelHooks.disable!(CopyableTree)
81
- expect { CopyableTree.create!(kind: 'magnolia') }.to_not raise_error
82
-
83
- Copyable::ModelHooks.reenable!(CopyableCoin)
84
- expect { CopyableCoin.create!(year: -10) }.to raise_error(ActiveRecord::RecordInvalid)
85
-
86
- Copyable::ModelHooks.reenable!(CopyableTree)
87
- expect { CopyableTree.create!(kind: 'magnolia') }.to raise_error(RuntimeError)
88
-
89
- Copyable::ModelHooks.reenable!(CopyableTree)
90
- expect { CopyableTree.create!(kind: 'magnolia') }.to raise_error(RuntimeError)
91
- end
92
- end
93
39
  end
metadata CHANGED
@@ -1,60 +1,66 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: copyable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wyatt Greene
8
8
  - Dennis Chan
9
9
  - Anne Geiersbach
10
10
  - Parker Morse
11
- autorequire:
11
+ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2018-12-05 00:00:00.000000000 Z
14
+ date: 2021-08-19 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activerecord
18
18
  requirement: !ruby/object:Gem::Requirement
19
19
  requirements:
20
- - - "~>"
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: '4.2'
23
+ - - "<"
21
24
  - !ruby/object:Gem::Version
22
- version: 4.1.0
25
+ version: '6'
23
26
  type: :runtime
24
27
  prerelease: false
25
28
  version_requirements: !ruby/object:Gem::Requirement
26
29
  requirements:
27
- - - "~>"
30
+ - - ">="
28
31
  - !ruby/object:Gem::Version
29
- version: 4.1.0
32
+ version: '4.2'
33
+ - - "<"
34
+ - !ruby/object:Gem::Version
35
+ version: '6'
30
36
  - !ruby/object:Gem::Dependency
31
37
  name: database_cleaner
32
38
  requirement: !ruby/object:Gem::Requirement
33
39
  requirements:
34
40
  - - "~>"
35
41
  - !ruby/object:Gem::Version
36
- version: 1.4.0
42
+ version: '2'
37
43
  type: :development
38
44
  prerelease: false
39
45
  version_requirements: !ruby/object:Gem::Requirement
40
46
  requirements:
41
47
  - - "~>"
42
48
  - !ruby/object:Gem::Version
43
- version: 1.4.0
49
+ version: '2'
44
50
  - !ruby/object:Gem::Dependency
45
51
  name: bundler
46
52
  requirement: !ruby/object:Gem::Requirement
47
53
  requirements:
48
- - - "~>"
54
+ - - ">="
49
55
  - !ruby/object:Gem::Version
50
- version: '1.6'
56
+ version: '0'
51
57
  type: :development
52
58
  prerelease: false
53
59
  version_requirements: !ruby/object:Gem::Requirement
54
60
  requirements:
55
- - - "~>"
61
+ - - ">="
56
62
  - !ruby/object:Gem::Version
57
- version: '1.6'
63
+ version: '0'
58
64
  - !ruby/object:Gem::Dependency
59
65
  name: rake
60
66
  requirement: !ruby/object:Gem::Requirement
@@ -87,16 +93,16 @@ dependencies:
87
93
  name: sqlite3
88
94
  requirement: !ruby/object:Gem::Requirement
89
95
  requirements:
90
- - - ">="
96
+ - - "~>"
91
97
  - !ruby/object:Gem::Version
92
- version: '0'
98
+ version: 1.3.6
93
99
  type: :development
94
100
  prerelease: false
95
101
  version_requirements: !ruby/object:Gem::Requirement
96
102
  requirements:
97
- - - ">="
103
+ - - "~>"
98
104
  - !ruby/object:Gem::Version
99
- version: '0'
105
+ version: 1.3.6
100
106
  description: Copyable makes it easy to copy ActiveRecord models.
101
107
  email:
102
108
  - dchan@dmgroupK12.com
@@ -162,7 +168,7 @@ homepage: https://github.com/dmcouncil/copyable
162
168
  licenses:
163
169
  - MIT
164
170
  metadata: {}
165
- post_install_message:
171
+ post_install_message:
166
172
  rdoc_options: []
167
173
  require_paths:
168
174
  - lib
@@ -177,9 +183,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
183
  - !ruby/object:Gem::Version
178
184
  version: '0'
179
185
  requirements: []
180
- rubyforge_project:
181
- rubygems_version: 2.4.6
182
- signing_key:
186
+ rubygems_version: 3.1.4
187
+ signing_key:
183
188
  specification_version: 4
184
189
  summary: ActiveRecord copier
185
190
  test_files: