rails_admin_clone 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -2
- data/lib/rails_admin_clone/model_cloner.rb +19 -9
- data/lib/rails_admin_clone/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9236ea8e86153f83d0f1f39ed873248cc3bc167f
|
4
|
+
data.tar.gz: bb7319860cf320b5564a1c40a7ba3c7aa391417d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: caf3ee7fd37599ae2e751860a1b9b07d6d7393f99193e19992c189d53cc139c5570980443b0c28e3fa0573ff4d8221b5c9ed0bc17481f557298c4c74ea465107
|
7
|
+
data.tar.gz: 7e92b2bae810e3c46df799ffb036fdf3003d433d4340cabff3adc5dc37b07b08e64c4707b2fcff291e0794df8213fbdca1dfc28ffc43a366efd6278f5ff2418f
|
data/README.md
CHANGED
@@ -40,14 +40,33 @@ end
|
|
40
40
|
You can specify a custom model method to clone objects with the following configuration:
|
41
41
|
```ruby
|
42
42
|
RailsAdmin.config do |config|
|
43
|
-
config.model '
|
43
|
+
config.model 'Post' do
|
44
44
|
clone_config do
|
45
|
-
custom_method :
|
45
|
+
custom_method :my_custom_clone_method
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
49
49
|
```
|
50
50
|
|
51
|
+
Here an implementation example for a clone `custom_method`:
|
52
|
+
```ruby
|
53
|
+
require 'rails_admin_clone/model_cloner'
|
54
|
+
|
55
|
+
class Post < ActiveRecord::Base
|
56
|
+
|
57
|
+
...
|
58
|
+
|
59
|
+
def my_custom_clone_method
|
60
|
+
RailsAdminClone::ModelCloner.new(self).default_clone.tap do |post|
|
61
|
+
post.status = :draft
|
62
|
+
post.slug = nil
|
63
|
+
post.title = "Copy of #{self.title}"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
51
70
|
## Contributing
|
52
71
|
Submitting a Pull Request:
|
53
72
|
|
@@ -64,6 +64,11 @@ module RailsAdminClone
|
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
+
def assign_association(association, old_association, new_association)
|
68
|
+
assign_attributes_for(new_association, get_association_attributes_from(old_association, association))
|
69
|
+
new_association = clone_recursively!(old_association, new_association)
|
70
|
+
end
|
71
|
+
|
67
72
|
# deep clone
|
68
73
|
def clone_recursively!(old_object, new_object)
|
69
74
|
new_object = clone_has_one old_object, new_object
|
@@ -84,17 +89,23 @@ module RailsAdminClone
|
|
84
89
|
# clone has_one associations
|
85
90
|
def clone_has_one(old_object, new_object)
|
86
91
|
old_object.class.reflect_on_all_associations(:has_one).each do |association|
|
87
|
-
|
88
|
-
|
89
|
-
assign_attributes_for(new_association, get_association_attributes_from(old_association, association))
|
90
|
-
new_association = clone_recursively!(old_association, new_association)
|
91
|
-
end
|
92
|
-
end
|
92
|
+
old_association = old_object.send(association.name)
|
93
|
+
build_has_one(new_object, association, old_association) if build_has_one?(old_object, association)
|
93
94
|
end
|
94
95
|
|
95
96
|
new_object
|
96
97
|
end
|
97
98
|
|
99
|
+
def build_has_one?(object, association)
|
100
|
+
object.send(association.name) && association.options[:through].blank?
|
101
|
+
end
|
102
|
+
|
103
|
+
def build_has_one(new_object, association, old_association)
|
104
|
+
new_object.send(:"build_#{association.name}").tap do |new_association|
|
105
|
+
assign_association(association, old_association, new_association)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
98
109
|
# clone has_many associations
|
99
110
|
def clone_has_many(old_object, new_object)
|
100
111
|
associations = old_object.class.reflect_on_all_associations(:has_many)
|
@@ -103,8 +114,7 @@ module RailsAdminClone
|
|
103
114
|
associations.each do |association|
|
104
115
|
old_object.send(association.name).each do |old_association|
|
105
116
|
new_object.send(association.name).build.tap do |new_association|
|
106
|
-
|
107
|
-
new_association = clone_recursively!(old_association, new_association)
|
117
|
+
assign_association(association, old_association, new_association)
|
108
118
|
end
|
109
119
|
end
|
110
120
|
end
|
@@ -119,7 +129,7 @@ module RailsAdminClone
|
|
119
129
|
end
|
120
130
|
|
121
131
|
associations.each do |association|
|
122
|
-
method_ids
|
132
|
+
method_ids = "#{association.name.to_s.singularize.to_sym}_ids"
|
123
133
|
new_object.send(:"#{method_ids}=", old_object.send(method_ids))
|
124
134
|
end
|
125
135
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_admin_clone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrea Dal Ponte
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 3.2.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 3.2.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rails_admin
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|