activerecord-duplicate 0.5.1 → 0.6.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.
- data/CHANGELOG.md +6 -0
- data/README.md +6 -6
- data/activerecord-duplicate.gemspec +1 -1
- data/lib/activerecord-duplicate/duplicate.rb +9 -2
- data/spec/acts_as_duplicator_spec.rb +64 -2
- data/spec/integration_spec.rb +11 -8
- metadata +15 -14
data/CHANGELOG.md
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
# 0.6.0 (April 24, 2012)
|
2
|
+
|
3
|
+
* Associations must be declared explicit as duplicatable as well, e.g. `belongs_to :posts; attr_duplicatable :text, :posts`.
|
4
|
+
|
5
|
+
* Without specifying `attr_duplicatable`, all attributes (except the primary-key) and associations will be duplicated automatically.
|
6
|
+
This allows you to skip the `attr_duplicatable` declarations completely, if you don't have special requirements.
|
data/README.md
CHANGED
@@ -4,13 +4,12 @@ Duplicating ActiveRecords is easy again. All you have to do:
|
|
4
4
|
|
5
5
|
```ruby
|
6
6
|
class Post < ActiveRecord::Base
|
7
|
-
attr_duplicatable :title, :content
|
8
7
|
end
|
9
8
|
|
10
9
|
post.duplicate
|
11
10
|
```
|
12
11
|
|
13
|
-
You want more
|
12
|
+
You want more control? Check out this example:
|
14
13
|
|
15
14
|
```ruby
|
16
15
|
class Post < ActiveRecord::Base
|
@@ -23,16 +22,17 @@ class Post < ActiveRecord::Base
|
|
23
22
|
has_many :comments
|
24
23
|
has_many :tags
|
25
24
|
|
26
|
-
#
|
27
|
-
attr_duplicatable :title, :content
|
25
|
+
# Only these attributes and associations will be duplicated.
|
26
|
+
attr_duplicatable :title, :content, :tags
|
28
27
|
end
|
29
28
|
|
30
29
|
class Tag < ActiveRecord::Base
|
31
|
-
|
30
|
+
belongs_to :post
|
31
|
+
|
32
|
+
attr_duplicatable :tag, :post
|
32
33
|
end
|
33
34
|
|
34
35
|
class Comment < ActiveRecord::Base
|
35
|
-
before_duplication { false }
|
36
36
|
end
|
37
37
|
|
38
38
|
# Duplicates non-copyrighted posts and tags as well, but ignores comments.
|
@@ -18,7 +18,7 @@ module ActiveRecord::Duplicate
|
|
18
18
|
value = case true
|
19
19
|
|
20
20
|
# Duplicate attribute if whitelisted
|
21
|
-
when self.class.attr_duplicatable
|
21
|
+
when self.class.attr_duplicatable?(key)
|
22
22
|
value
|
23
23
|
|
24
24
|
# If not whitelisted, set to default value
|
@@ -34,6 +34,8 @@ module ActiveRecord::Duplicate
|
|
34
34
|
|
35
35
|
self.class.reflect_on_all_associations.each do |association|
|
36
36
|
name = association.name
|
37
|
+
|
38
|
+
next unless self.class.attr_duplicatable?(name)
|
37
39
|
macro = association.macro
|
38
40
|
association = self.association(association.name)
|
39
41
|
|
@@ -66,7 +68,12 @@ module ActiveRecord::Duplicate
|
|
66
68
|
module ClassMethods
|
67
69
|
def attr_duplicatable(*attributes)
|
68
70
|
self._duplicatable_attributes = attributes.map(&:to_sym) if attributes.present?
|
69
|
-
self._duplicatable_attributes
|
71
|
+
self._duplicatable_attributes
|
72
|
+
end
|
73
|
+
|
74
|
+
def attr_duplicatable?(attribute)
|
75
|
+
attribute = attribute.to_sym
|
76
|
+
attr_duplicatable.present? ? attr_duplicatable.include?(attribute) : primary_key.to_sym != attribute
|
70
77
|
end
|
71
78
|
|
72
79
|
# Duplicated from activemodel/lib/active_model/validations/callbacks.rb
|
@@ -1,11 +1,19 @@
|
|
1
1
|
require File.expand_path('../spec_helper', __FILE__)
|
2
2
|
|
3
3
|
describe ActiveRecord::Duplicate do
|
4
|
+
before do
|
5
|
+
ActiveRecord::Schema.define do
|
6
|
+
change_table :records do |t|
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
4
12
|
let(:klass) { Class.new(ActiveRecord::Base) { self.table_name = 'records' } }
|
5
13
|
|
6
14
|
|
7
15
|
describe :attr_duplicatable do
|
8
|
-
it 'allows you to whitelist attributes' do
|
16
|
+
it 'allows you to whitelist attributes and associations' do
|
9
17
|
klass.attr_duplicatable(:created_at, :updated_at)
|
10
18
|
end
|
11
19
|
|
@@ -18,6 +26,60 @@ describe ActiveRecord::Duplicate do
|
|
18
26
|
end
|
19
27
|
|
20
28
|
|
29
|
+
describe :attr_duplicatable? do
|
30
|
+
describe 'attr_duplicatable is not set' do
|
31
|
+
it 'returns for all attributes and associations true' do
|
32
|
+
klass.instance_eval do
|
33
|
+
has_many :records
|
34
|
+
end
|
35
|
+
|
36
|
+
klass.attr_duplicatable?(:created_at).must_equal(true)
|
37
|
+
klass.attr_duplicatable?(:updated_at).must_equal(true)
|
38
|
+
klass.attr_duplicatable?(:records).must_equal(true)
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
# but
|
43
|
+
it 'returns false for the primary-key' do
|
44
|
+
klass.attr_duplicatable?(:id).must_equal(false)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
describe 'attr_duplicatable is set' do
|
50
|
+
it 'returns whether an attribute is allowed to duplicated or not' do
|
51
|
+
klass.instance_eval do
|
52
|
+
has_many :records
|
53
|
+
|
54
|
+
attr_duplicatable :created_at, :records
|
55
|
+
end
|
56
|
+
|
57
|
+
klass.attr_duplicatable?(:created_at).must_equal(true)
|
58
|
+
klass.attr_duplicatable?(:updated_at).must_equal(false)
|
59
|
+
klass.attr_duplicatable?(:records).must_equal(true)
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
it 'allows primary-keys only if explicit set' do
|
64
|
+
klass.instance_eval do
|
65
|
+
attr_duplicatable :id
|
66
|
+
end
|
67
|
+
|
68
|
+
klass.attr_duplicatable?(:id).must_equal(true)
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
it 'allows (non-standard) primary-keys only if explicit set' do
|
73
|
+
klass.instance_eval do
|
74
|
+
attr_duplicatable primary_key
|
75
|
+
end
|
76
|
+
|
77
|
+
klass.attr_duplicatable?(klass.primary_key).must_equal(true)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
|
21
83
|
describe 'before_duplication { false }' do
|
22
84
|
it 'marks associations as non-duplicatable' do
|
23
85
|
klass.instance_eval do
|
@@ -30,7 +92,7 @@ describe ActiveRecord::Duplicate do
|
|
30
92
|
|
31
93
|
|
32
94
|
describe :duplicate do
|
33
|
-
it 'duplicates records' do
|
95
|
+
it 'duplicates records' do
|
34
96
|
record = klass.create
|
35
97
|
duplicate = record.duplicate
|
36
98
|
|
data/spec/integration_spec.rb
CHANGED
@@ -24,11 +24,13 @@ describe 'Integration' do
|
|
24
24
|
t.integer :value
|
25
25
|
end
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
class Blog < ActiveRecord::Base
|
29
29
|
has_many :posts, inverse_of: :blog
|
30
|
+
|
31
|
+
attr_duplicatable :posts
|
30
32
|
end
|
31
|
-
|
33
|
+
|
32
34
|
class Post < ActiveRecord::Base
|
33
35
|
# Don't duplicate copyrighted posts
|
34
36
|
before_duplication { !self.is_copyrighted? }
|
@@ -38,21 +40,22 @@ describe 'Integration' do
|
|
38
40
|
has_many :comments
|
39
41
|
has_many :ratings, as: :parent
|
40
42
|
|
41
|
-
attr_duplicatable :title, :content
|
42
|
-
class_attribute :counter
|
43
|
+
attr_duplicatable :title, :content, :blog, :ratings
|
43
44
|
|
44
45
|
validates :title, presence: true, uniqueness: true
|
45
46
|
end
|
46
|
-
|
47
|
+
|
47
48
|
class Comment < ActiveRecord::Base
|
48
|
-
before_duplication { false }
|
49
|
-
|
50
49
|
belongs_to :post
|
51
50
|
has_many :ratings, as: :parent
|
51
|
+
|
52
|
+
attr_duplicatable :ratings, :post
|
52
53
|
end
|
53
|
-
|
54
|
+
|
54
55
|
class Rating < ActiveRecord::Base
|
55
56
|
belongs_to :parent, polymorphic: true
|
57
|
+
|
58
|
+
attr_duplicatable :parent
|
56
59
|
end
|
57
60
|
end
|
58
61
|
|
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.6.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &70185001321060 !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: *70185001321060
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: minitest
|
27
|
-
requirement: &
|
27
|
+
requirement: &70185001320660 !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: *70185001320660
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: purdytest
|
38
|
-
requirement: &
|
38
|
+
requirement: &70185001369300 !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: *70185001369300
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rails
|
49
|
-
requirement: &
|
49
|
+
requirement: &70185001368800 !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: *70185001368800
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rake
|
60
|
-
requirement: &
|
60
|
+
requirement: &70185001368380 !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: *70185001368380
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: sqlite3
|
71
|
-
requirement: &
|
71
|
+
requirement: &70185001367920 !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: *70185001367920
|
80
80
|
description: Duplicating ActiveRecords is easy again.
|
81
81
|
email: uher.mario@gmail.com
|
82
82
|
executables: []
|
@@ -84,6 +84,7 @@ extensions: []
|
|
84
84
|
extra_rdoc_files: []
|
85
85
|
files:
|
86
86
|
- .travis.yml
|
87
|
+
- CHANGELOG.md
|
87
88
|
- Gemfile
|
88
89
|
- README.md
|
89
90
|
- Rakefile
|