rails_admin_clone 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -17,8 +17,7 @@ module RailsAdmin
|
|
17
17
|
|
18
18
|
register_instance_option :controller do
|
19
19
|
Proc.new do
|
20
|
-
|
21
|
-
model_cloner = RailsAdminClone::ModelCloner.new(@object)
|
20
|
+
model_cloner = RailsAdminClone::ModelCloner.new(@object)
|
22
21
|
custom_method = model_config.clone_config.custom_method
|
23
22
|
|
24
23
|
if custom_method.present?
|
@@ -39,7 +38,6 @@ module RailsAdmin
|
|
39
38
|
format.html { render @action.template_name }
|
40
39
|
format.js { render @action.template_name, :layout => false }
|
41
40
|
end
|
42
|
-
|
43
41
|
end
|
44
42
|
end
|
45
43
|
|
@@ -11,7 +11,15 @@ module RailsAdminClone
|
|
11
11
|
|
12
12
|
def default_clone
|
13
13
|
new_object = self.clone_object(self.original_model)
|
14
|
-
|
14
|
+
|
15
|
+
self.clone_recursively!(self.original_model, new_object)
|
16
|
+
begin
|
17
|
+
self.clone_recursively!(self.original_model, new_object)
|
18
|
+
rescue Exception => e
|
19
|
+
Rails.logger.info "RailsAdminClone Exception on clone_recursively!:\n#{e}"
|
20
|
+
end
|
21
|
+
|
22
|
+
new_object
|
15
23
|
end
|
16
24
|
|
17
25
|
def method_clone(method)
|
@@ -20,10 +28,11 @@ module RailsAdminClone
|
|
20
28
|
|
21
29
|
protected
|
22
30
|
|
23
|
-
def clone_recursively(old_object, new_object)
|
31
|
+
def clone_recursively!(old_object, new_object)
|
24
32
|
new_object = clone_has_one(old_object, new_object)
|
25
|
-
new_object = clone_has_many(old_object, new_object)
|
26
33
|
new_object = clone_habtm(old_object, new_object)
|
34
|
+
new_object = clone_has_many(old_object, new_object)
|
35
|
+
|
27
36
|
|
28
37
|
new_object
|
29
38
|
end
|
@@ -55,7 +64,7 @@ module RailsAdminClone
|
|
55
64
|
|
56
65
|
new_object.send(:"build_#{association_name}").tap do |new_association|
|
57
66
|
new_association.assign_attributes attributes, without_protection: true
|
58
|
-
new_association = self.clone_recursively(old_association, new_association)
|
67
|
+
new_association = self.clone_recursively!(old_association, new_association)
|
59
68
|
end
|
60
69
|
end
|
61
70
|
end
|
@@ -65,7 +74,10 @@ module RailsAdminClone
|
|
65
74
|
|
66
75
|
# clone has_many associations
|
67
76
|
def clone_has_many(old_object, new_object)
|
68
|
-
old_object.class.reflect_on_all_associations(:has_many)
|
77
|
+
associations = old_object.class.reflect_on_all_associations(:has_many)
|
78
|
+
.select{|a| !a.options.keys.include?(:through)}
|
79
|
+
|
80
|
+
associations.each do |class_association|
|
69
81
|
association_name = class_association.name
|
70
82
|
# primary_key = association_name.to_s.singularize.camelize.constantize.try(:primary_key) || 'id'
|
71
83
|
primary_key = 'id'
|
@@ -77,7 +89,7 @@ module RailsAdminClone
|
|
77
89
|
|
78
90
|
new_object.send(association_name).build.tap do |new_association|
|
79
91
|
new_association.assign_attributes attributes, without_protection: true
|
80
|
-
new_association = self.clone_recursively(old_association, new_association)
|
92
|
+
new_association = self.clone_recursively!(old_association, new_association)
|
81
93
|
end
|
82
94
|
end
|
83
95
|
end
|
@@ -87,11 +99,17 @@ module RailsAdminClone
|
|
87
99
|
|
88
100
|
# clone has_and_belongs_to_many associtations
|
89
101
|
def clone_habtm(old_object, new_object)
|
90
|
-
old_object.class.reflect_on_all_associations
|
102
|
+
associations = old_object.class.reflect_on_all_associations.select do |a|
|
103
|
+
a.macro == :has_and_belongs_to_many || (a.macro == :has_many && a.options.keys.include?(:through))
|
104
|
+
end
|
105
|
+
|
106
|
+
associations.each do |class_association|
|
91
107
|
association_name = class_association.name
|
92
108
|
method_ids = "#{association_name.to_s.singularize.to_sym}_ids"
|
93
109
|
|
94
|
-
|
110
|
+
Rails.logger.info "**** association_name: #{association_name}"
|
111
|
+
|
112
|
+
new_object.send(:"#{method_ids}=", old_object.send(method_ids))
|
95
113
|
end
|
96
114
|
|
97
115
|
new_object
|
@@ -100,7 +118,6 @@ module RailsAdminClone
|
|
100
118
|
def build_from(object)
|
101
119
|
object.class.new
|
102
120
|
end
|
103
|
-
|
104
121
|
end
|
105
122
|
end
|
106
123
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-10-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|