activerecord-duplicate 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -3
- data/activerecord-duplicate.gemspec +1 -1
- data/lib/activerecord-duplicate/active_record/duplicate.rb +24 -34
- data/spec/acts_as_duplicator_spec.rb +5 -3
- data/spec/integration_spec.rb +1 -1
- metadata +13 -13
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
##
|
1
|
+
## activerecord-duplicate [![Build Status](https://secure.travis-ci.org/haihappen/activerecord-duplicate.png)](http://travis-ci.org/haihappen/activerecord-duplicate)
|
2
2
|
|
3
3
|
Duplicating ActiveRecords is easy again. All you have to do:
|
4
4
|
|
@@ -32,7 +32,7 @@ class Tag < ActiveRecord::Base
|
|
32
32
|
end
|
33
33
|
|
34
34
|
class Comment < ActiveRecord::Base
|
35
|
-
|
35
|
+
before_duplication { false }
|
36
36
|
end
|
37
37
|
|
38
38
|
# Duplicates non-copyrighted posts and tags as well, but ignores comments.
|
@@ -46,7 +46,7 @@ Tested against Ruby versions `1.9.2`, `1.9.3`, `ruby-head` and Rails versions `3
|
|
46
46
|
In your `Gemfile`:
|
47
47
|
|
48
48
|
```ruby
|
49
|
-
gem '
|
49
|
+
gem 'activerecord-duplicate'
|
50
50
|
```
|
51
51
|
|
52
52
|
## Contributing
|
@@ -6,16 +6,13 @@ module ActiveRecord
|
|
6
6
|
|
7
7
|
included do
|
8
8
|
class_attribute :_duplicatable_attributes
|
9
|
-
class_attribute :_duplicatable
|
10
9
|
|
11
10
|
attr_accessor :duplication_parent
|
12
11
|
|
13
12
|
include ActiveRecord::Duplicate::Callbacks
|
14
13
|
end
|
15
14
|
|
16
|
-
def duplicate
|
17
|
-
return unless self.class.duplicatable
|
18
|
-
|
15
|
+
def duplicate
|
19
16
|
dup.tap do |duplicate|
|
20
17
|
attributes.each do |key, value|
|
21
18
|
value = case true
|
@@ -35,29 +32,31 @@ module ActiveRecord
|
|
35
32
|
duplicate.send(:"#{key}=", value)
|
36
33
|
end
|
37
34
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
if duplication_parent.is_a?(association.klass)
|
43
|
-
duplicate.send(:"#{association.reflection.name}=", duplication_parent)
|
44
|
-
duplicate.send(:"#{association.reflection.name}_id=", nil)
|
45
|
-
else
|
46
|
-
duplicate.send(:"#{association.reflection.name}=", send(association.reflection.name))
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
# Duplicate all has_many associations.
|
51
|
-
self.class.reflect_on_all_associations(:has_many).each do |association|
|
35
|
+
self.class.reflect_on_all_associations.each do |association|
|
36
|
+
name = association.name
|
37
|
+
macro = association.macro
|
52
38
|
association = self.association(association.name)
|
53
39
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
40
|
+
case macro
|
41
|
+
when :belongs_to
|
42
|
+
# Duplicate all belongs_to associations.
|
43
|
+
if duplication_parent.is_a?(association.klass)
|
44
|
+
duplicate.send(:"#{name}=", duplication_parent)
|
45
|
+
duplicate.send(:"#{name}_id=", nil)
|
46
|
+
else
|
47
|
+
duplicate.send(:"#{name}=", send(name))
|
48
|
+
end
|
49
|
+
|
50
|
+
when :has_many
|
51
|
+
# Duplicate all has_many associations.
|
52
|
+
duplicate.send(:"#{name}=", send(name).map do |object|
|
53
|
+
object.duplication_parent = duplicate
|
54
|
+
object = object.duplicate
|
55
|
+
next unless object.present?
|
56
|
+
|
57
|
+
object
|
58
|
+
end.compact)
|
59
|
+
end
|
61
60
|
end
|
62
61
|
end
|
63
62
|
end
|
@@ -67,15 +66,6 @@ module ActiveRecord
|
|
67
66
|
self._duplicatable_attributes = attributes.map(&:to_sym) if attributes.present?
|
68
67
|
self._duplicatable_attributes || []
|
69
68
|
end
|
70
|
-
|
71
|
-
def duplicatable=(duplicatable)
|
72
|
-
self._duplicatable = duplicatable unless duplicatable.nil?
|
73
|
-
self.duplicatable
|
74
|
-
end
|
75
|
-
|
76
|
-
def duplicatable
|
77
|
-
self._duplicatable.nil? ? true : !!self._duplicatable
|
78
|
-
end
|
79
69
|
end
|
80
70
|
end
|
81
71
|
end
|
@@ -18,11 +18,13 @@ describe ActiveRecord::Duplicate do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
|
21
|
-
describe
|
21
|
+
describe 'before_duplication { false }' do
|
22
22
|
it 'marks associations as non-duplicatable' do
|
23
|
-
klass.
|
23
|
+
klass.instance_eval do
|
24
|
+
before_duplication { false }
|
25
|
+
end
|
24
26
|
|
25
|
-
klass.
|
27
|
+
klass.new.duplicate.must_equal(false)
|
26
28
|
end
|
27
29
|
end
|
28
30
|
|
data/spec/integration_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-duplicate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-04-18 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &70285775741840 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.1'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70285775741840
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: minitest
|
27
|
-
requirement: &
|
27
|
+
requirement: &70285775741440 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70285775741440
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: purdytest
|
38
|
-
requirement: &
|
38
|
+
requirement: &70285775740980 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70285775740980
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rails
|
49
|
-
requirement: &
|
49
|
+
requirement: &70285775740480 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '3.1'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70285775740480
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rake
|
60
|
-
requirement: &
|
60
|
+
requirement: &70285775740060 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70285775740060
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: sqlite3
|
71
|
-
requirement: &
|
71
|
+
requirement: &70285775739600 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,7 +76,7 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70285775739600
|
80
80
|
description: Duplicating ActiveRecords is easy again.
|
81
81
|
email: uher.mario@gmail.com
|
82
82
|
executables: []
|