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 CHANGED
@@ -1,4 +1,4 @@
1
- ## active_record-duplicate [![Build Status](https://secure.travis-ci.org/haihappen/active_record-duplicate.png)](http://travis-ci.org/haihappen/active_record-duplicate)
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
- self.duplicatable = false
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 'active_record-duplicate'
49
+ gem 'activerecord-duplicate'
50
50
  ```
51
51
 
52
52
  ## Contributing
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = 'activerecord-duplicate'
3
- gem.version = '0.4.2'
3
+ gem.version = '0.5.0'
4
4
  gem.authors = 'Mario Uher'
5
5
  gem.email = 'uher.mario@gmail.com'
6
6
  gem.description = gem.summary = 'Duplicating ActiveRecords is easy again.'
@@ -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
- # Duplicate all belongs_to associations.
39
- self.class.reflect_on_all_associations(:belongs_to).each do |association|
40
- association = self.association(association.name)
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
- duplicate.send(:"#{association.reflection.name}=", send(association.reflection.name).map do |object|
55
- object.duplication_parent = duplicate
56
- object = object.duplicate
57
- next unless object.present?
58
-
59
- object
60
- end.compact)
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 :duplicatable do
21
+ describe 'before_duplication { false }' do
22
22
  it 'marks associations as non-duplicatable' do
23
- klass.duplicatable = false
23
+ klass.instance_eval do
24
+ before_duplication { false }
25
+ end
24
26
 
25
- klass.duplicatable.must_equal(false)
27
+ klass.new.duplicate.must_equal(false)
26
28
  end
27
29
  end
28
30
 
@@ -52,7 +52,7 @@ describe 'Integration' do
52
52
  end
53
53
 
54
54
  class Comment < ActiveRecord::Base
55
- self.duplicatable = false
55
+ before_duplication { false }
56
56
 
57
57
  belongs_to :post
58
58
  has_many :ratings, as: :parent
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.2
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: &70294310520280 !ruby/object:Gem::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: *70294310520280
24
+ version_requirements: *70285775741840
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: minitest
27
- requirement: &70294310519880 !ruby/object:Gem::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: *70294310519880
35
+ version_requirements: *70285775741440
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: purdytest
38
- requirement: &70294310519420 !ruby/object:Gem::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: *70294310519420
46
+ version_requirements: *70285775740980
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rails
49
- requirement: &70294310518920 !ruby/object:Gem::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: *70294310518920
57
+ version_requirements: *70285775740480
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &70294310567580 !ruby/object:Gem::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: *70294310567580
68
+ version_requirements: *70285775740060
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: sqlite3
71
- requirement: &70294310567120 !ruby/object:Gem::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: *70294310567120
79
+ version_requirements: *70285775739600
80
80
  description: Duplicating ActiveRecords is easy again.
81
81
  email: uher.mario@gmail.com
82
82
  executables: []