deep_cloneable 2.3.0 → 2.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/deep_cloneable.gemspec +3 -3
- data/lib/deep_cloneable.rb +4 -2
- data/readme.md +2 -0
- data/test/models.rb +13 -0
- data/test/schema.rb +14 -0
- data/test/test_deep_cloneable.rb +24 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66564e7a8d80a1e53865df29962c198d806cfa1a
|
4
|
+
data.tar.gz: 7c8225b19e4b29d9dd060114f9b3bd48844dc0f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96879e01a3bf45576bff535d91c143c62bd2512761083035651fb369df0269257e597ac0b675922c5a5c3b91881079e27940bea432c969fb795bc663cf6297f3
|
7
|
+
data.tar.gz: 71a1f902e51e6087e0c2ed6413d1b0d6975d321360abfe467dc6a11aa9b15ba5f413991ebe593eed45eaa7b8c9fbb43eaba69efea86deb3b0cb2357ea02fd732
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.3.
|
1
|
+
2.3.1
|
data/deep_cloneable.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: deep_cloneable 2.3.
|
5
|
+
# stub: deep_cloneable 2.3.1 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "deep_cloneable"
|
9
|
-
s.version = "2.3.
|
9
|
+
s.version = "2.3.1"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["Reinier de Lange"]
|
14
|
-
s.date = "2017-
|
14
|
+
s.date = "2017-10-02"
|
15
15
|
s.description = "Extends the functionality of ActiveRecord::Base#dup to perform a deep clone that includes user specified associations. "
|
16
16
|
s.email = "r.j.delange@nedforce.nl"
|
17
17
|
s.extra_rdoc_files = [
|
data/lib/deep_cloneable.rb
CHANGED
@@ -48,9 +48,11 @@ class ActiveRecord::Base
|
|
48
48
|
end
|
49
49
|
|
50
50
|
if conditions_or_deep_associations.kind_of?(Hash)
|
51
|
+
conditions_or_deep_associations = conditions_or_deep_associations.dup
|
51
52
|
conditions[:if] = conditions_or_deep_associations.delete(:if) if conditions_or_deep_associations[:if]
|
52
53
|
conditions[:unless] = conditions_or_deep_associations.delete(:unless) if conditions_or_deep_associations[:unless]
|
53
54
|
elsif conditions_or_deep_associations.kind_of?(Array)
|
55
|
+
conditions_or_deep_associations = conditions_or_deep_associations.dup
|
54
56
|
conditions_or_deep_associations.delete_if {|entry| conditions.merge!(entry) if entry.is_a?(Hash) && (entry.key?(:if) || entry.key?(:unless)) }
|
55
57
|
end
|
56
58
|
|
@@ -176,7 +178,7 @@ class ActiveRecord::Base
|
|
176
178
|
def evaluate_conditions object, conditions
|
177
179
|
(conditions[:if] && conditions[:if].call(object)) || (conditions[:unless] && !conditions[:unless].call(object))
|
178
180
|
end
|
179
|
-
|
181
|
+
|
180
182
|
def normalized_includes_list includes
|
181
183
|
list = []
|
182
184
|
Array(includes).each do |item|
|
@@ -186,7 +188,7 @@ class ActiveRecord::Base
|
|
186
188
|
list << item
|
187
189
|
end
|
188
190
|
end
|
189
|
-
|
191
|
+
|
190
192
|
list
|
191
193
|
end
|
192
194
|
|
data/readme.md
CHANGED
@@ -110,6 +110,8 @@ pirate.deep_clone include: :parrot do |original, kopy|
|
|
110
110
|
end
|
111
111
|
```
|
112
112
|
|
113
|
+
*Note*: The block is invoked before attributes are excluded.
|
114
|
+
|
113
115
|
*Note*: Using `deep_clone` with a block will also pass the associated objects that are being cloned to the block, so be sure to check whether the object actually responds to your method of choice.
|
114
116
|
|
115
117
|
### Cloning models with files associated through Carrierwave
|
data/test/models.rb
CHANGED
@@ -127,3 +127,16 @@ class Contractor < ActiveRecord::Base
|
|
127
127
|
belongs_to :building
|
128
128
|
has_and_belongs_to_many :apartments
|
129
129
|
end
|
130
|
+
|
131
|
+
class User < ActiveRecord::Base
|
132
|
+
has_many :orders
|
133
|
+
end
|
134
|
+
|
135
|
+
class Order < ActiveRecord::Base
|
136
|
+
belongs_to :user
|
137
|
+
has_many :products
|
138
|
+
end
|
139
|
+
|
140
|
+
class Product < ActiveRecord::Base
|
141
|
+
belongs_to :order
|
142
|
+
end
|
data/test/schema.rb
CHANGED
@@ -129,4 +129,18 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
129
129
|
t.column :apartment_id, :integer
|
130
130
|
t.column :contractor_id, :integer
|
131
131
|
end
|
132
|
+
|
133
|
+
create_table :users, :force => true do |t|
|
134
|
+
t.column :name, :string
|
135
|
+
t.column :contractor_id, :integer
|
136
|
+
end
|
137
|
+
|
138
|
+
create_table :orders, :force => true do |t|
|
139
|
+
t.column :user_id, :integer
|
140
|
+
end
|
141
|
+
|
142
|
+
create_table :products, :force => true do |t|
|
143
|
+
t.column :name, :string
|
144
|
+
t.column :order_id, :integer
|
145
|
+
end
|
132
146
|
end
|
data/test/test_deep_cloneable.rb
CHANGED
@@ -126,7 +126,7 @@ class TestDeepCloneable < MiniTest::Unit::TestCase
|
|
126
126
|
assert_equal 1, deep_clone.gold_pieces.size
|
127
127
|
assert_equal 1, deep_clone.mateys.size
|
128
128
|
end
|
129
|
-
|
129
|
+
|
130
130
|
def test_multiple_and_deep_include_association_with_array_and_multikey_hash
|
131
131
|
deep_clone = @jack.deep_clone(:include => [:parrot, {:treasures => :gold_pieces, :mateys => {}}])
|
132
132
|
assert deep_clone.new_record?
|
@@ -134,7 +134,7 @@ class TestDeepCloneable < MiniTest::Unit::TestCase
|
|
134
134
|
assert_equal 1, deep_clone.treasures.size
|
135
135
|
assert_equal 1, deep_clone.gold_pieces.size
|
136
136
|
assert_equal 1, deep_clone.mateys.size
|
137
|
-
end
|
137
|
+
end
|
138
138
|
|
139
139
|
def test_with_belongs_to_relation
|
140
140
|
deep_clone = @jack.deep_clone(:include => :parrot)
|
@@ -421,6 +421,27 @@ class TestDeepCloneable < MiniTest::Unit::TestCase
|
|
421
421
|
assert_equal 1, deep_clone.pirates.size
|
422
422
|
end
|
423
423
|
|
424
|
+
def test_should_reject_copies_if_conditionals_are_passed_with_nested_many_to_many_associations
|
425
|
+
@user = User.create :name => 'Jack'
|
426
|
+
@order1 = Order.create
|
427
|
+
@order2 = Order.create
|
428
|
+
@product1 = Product.create :name => 'Paper'
|
429
|
+
@product2 = Product.create :name => 'Ink'
|
430
|
+
@order2.products << [@product1, @product2]
|
431
|
+
@user.orders << [@order1, @order2]
|
432
|
+
|
433
|
+
deep_clone = @user.deep_clone(:include => [:orders => [:products => [{ :unless => lambda {|product| product.name == 'Ink' }}]]])
|
434
|
+
|
435
|
+
assert deep_clone.new_record?
|
436
|
+
assert deep_clone.save
|
437
|
+
assert_equal 1, deep_clone.orders.second.products.size
|
438
|
+
|
439
|
+
deep_clone = @user.deep_clone(:include => [:orders => [:products => [{ :if => lambda {|product| product.name == 'Ink'}}]]])
|
440
|
+
assert deep_clone.new_record?
|
441
|
+
assert deep_clone.save
|
442
|
+
assert_equal 1, deep_clone.orders.second.products.size
|
443
|
+
end
|
444
|
+
|
424
445
|
def test_should_find_in_dict_for_habtm
|
425
446
|
apt = Apartment.create(:number => "101")
|
426
447
|
contractor = Contractor.create(:name => "contractor", :apartments => [apt])
|
@@ -451,5 +472,5 @@ class TestDeepCloneable < MiniTest::Unit::TestCase
|
|
451
472
|
assert_nil deep_clone.name
|
452
473
|
refute deep_clone.name_changed?
|
453
474
|
end
|
454
|
-
|
475
|
+
|
455
476
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deep_cloneable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.3.
|
4
|
+
version: 2.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Reinier de Lange
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|