rails_admin_clone 0.0.2 → 0.0.3
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.
- checksums.yaml +15 -0
- data/MIT-LICENSE +1 -1
- data/lib/rails_admin_clone/model_cloner.rb +63 -52
- data/lib/rails_admin_clone/version.rb +1 -1
- metadata +7 -13
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YzYwNjFlYzE1M2E3N2E3NzY3YmVjM2QxZWI1MjljY2Y2NDE5NWIyMw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZTUzZDE3MTlmZmU2MmZjNTQwODFiMDZhM2Y3YmM3MWZlYzdkMTRlZQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YjM3YmQ5NjBlY2E1NmYzMjg5OTlhMTM3NTdmNGFmNDg0OWNkYTc1ZTBhZDU0
|
10
|
+
MzI2NzBhYmFmMTlhYjdjYjhhZWY2Yzg5NmJlMDFjZDM0ZjNhMDBiN2YzMzVh
|
11
|
+
OTlkMTJjZTFhNGM0Y2IxYTMwNWE0NDExNjljYTUzNTFiODNlNzg=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NDc5MzI0NjI4MTg1N2RmMGQxNWExMzIzZjA0NjFmYTFhZGMyMzRmOGZmMTli
|
14
|
+
MGJiN2Q0ZThmYzc3ZTAzZTljYmMyNTFlY2NjZDRjY2U2ZWY2ZjdkOWM4YmE3
|
15
|
+
ODA4Y2Y5NWNiNThlMzRkYjI2Yjk3ZWNhYzM1NGRhZGNkZGUxNTE=
|
data/MIT-LICENSE
CHANGED
@@ -9,62 +9,85 @@ module RailsAdminClone
|
|
9
9
|
@original_model
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
|
12
|
+
def class_model
|
13
|
+
original_model.class
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
rescue Exception => e
|
19
|
-
Rails.logger.info "RailsAdminClone Exception on clone_recursively!:\n#{e}"
|
20
|
-
end
|
16
|
+
def default_clone
|
17
|
+
new_object = clone_object(original_model)
|
18
|
+
clone_recursively!(original_model, new_object)
|
21
19
|
|
22
20
|
new_object
|
23
21
|
end
|
24
22
|
|
25
23
|
def method_clone(method)
|
26
|
-
|
24
|
+
original_model.send(method)
|
27
25
|
end
|
28
26
|
|
29
27
|
protected
|
30
28
|
|
31
|
-
def
|
32
|
-
|
33
|
-
|
34
|
-
|
29
|
+
def class_with_strong_parameters?(klass)
|
30
|
+
defined?(ActiveModel::ForbiddenAttributesProtection) && klass.include?(ActiveModel::ForbiddenAttributesProtection)
|
31
|
+
end
|
32
|
+
|
33
|
+
def timestamp_columns
|
34
|
+
%w(created_at created_on updated_at updated_on)
|
35
|
+
end
|
36
|
+
|
37
|
+
def attributes_black_list_from_model(model)
|
38
|
+
[model.primary_key, model.inheritance_column] + timestamp_columns
|
39
|
+
end
|
40
|
+
|
41
|
+
def attributes_black_list_from_association(association)
|
42
|
+
model = association.class_name.constantize
|
43
|
+
attributes = attributes_black_list_from_model(model)
|
44
|
+
attributes + [association.try(:foreign_key), association.try(:type)]
|
45
|
+
end
|
46
|
+
|
47
|
+
def get_model_attributes_from(object)
|
48
|
+
object.attributes.select do |k,v|
|
49
|
+
!attributes_black_list_from_model(object.class).include?(k)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def get_association_attributes_from(object, association)
|
54
|
+
object.attributes.select do |k,v|
|
55
|
+
!attributes_black_list_from_association(association).include?(k)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def assign_attributes_for(object, attributes)
|
60
|
+
if class_with_strong_parameters?(object.class)
|
61
|
+
object.assign_attributes attributes
|
62
|
+
else
|
63
|
+
object.assign_attributes attributes, without_protection: true
|
64
|
+
end
|
65
|
+
end
|
35
66
|
|
67
|
+
# deep clone
|
68
|
+
def clone_recursively!(old_object, new_object)
|
69
|
+
new_object = clone_has_one old_object, new_object
|
70
|
+
new_object = clone_habtm old_object, new_object
|
71
|
+
new_object = clone_has_many old_object, new_object
|
36
72
|
|
37
73
|
new_object
|
38
74
|
end
|
39
75
|
|
40
76
|
# clone object without associations
|
41
77
|
def clone_object(old_object)
|
42
|
-
object
|
43
|
-
|
44
|
-
![object.class.primary_key, 'created_at', 'updated_at'].include?(k)
|
45
|
-
end
|
78
|
+
object = build_from(old_object)
|
79
|
+
assign_attributes_for(object, get_model_attributes_from(old_object))
|
46
80
|
|
47
|
-
object.assign_attributes attributes, without_protection: true
|
48
81
|
object
|
49
82
|
end
|
50
83
|
|
51
84
|
# clone has_one associations
|
52
85
|
def clone_has_one(old_object, new_object)
|
53
|
-
old_object.class.reflect_on_all_associations(:has_one).each do |
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
# primary_key = association_name.to_s.singularize.camelize.constantize.try(:primary_key) || 'id'
|
59
|
-
primary_key = 'id'
|
60
|
-
|
61
|
-
attributes = old_association.attributes.select do |k,v|
|
62
|
-
![primary_key, class_association.try(:foreign_key), class_association.try(:type), 'created_at', 'updated_at'].include?(k)
|
63
|
-
end
|
64
|
-
|
65
|
-
new_object.send(:"build_#{association_name}").tap do |new_association|
|
66
|
-
new_association.assign_attributes attributes, without_protection: true
|
67
|
-
new_association = self.clone_recursively!(old_association, new_association)
|
86
|
+
old_object.class.reflect_on_all_associations(:has_one).each do |association|
|
87
|
+
if old_association = old_object.send(association.name)
|
88
|
+
new_object.send(:"build_#{association.name}").tap do |new_association|
|
89
|
+
assign_attributes_for(new_association, get_association_attributes_from(old_association, association))
|
90
|
+
new_association = clone_recursively!(old_association, new_association)
|
68
91
|
end
|
69
92
|
end
|
70
93
|
end
|
@@ -77,19 +100,11 @@ module RailsAdminClone
|
|
77
100
|
associations = old_object.class.reflect_on_all_associations(:has_many)
|
78
101
|
.select{|a| !a.options.keys.include?(:through)}
|
79
102
|
|
80
|
-
associations.each do |
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
old_object.send(association_name).each do |old_association|
|
86
|
-
attributes = old_association.attributes.select do |k,v|
|
87
|
-
![primary_key, class_association.try(:foreign_key), class_association.try(:type), 'created_at', 'updated_at'].include?(k)
|
88
|
-
end
|
89
|
-
|
90
|
-
new_object.send(association_name).build.tap do |new_association|
|
91
|
-
new_association.assign_attributes attributes, without_protection: true
|
92
|
-
new_association = self.clone_recursively!(old_association, new_association)
|
103
|
+
associations.each do |association|
|
104
|
+
old_object.send(association.name).each do |old_association|
|
105
|
+
new_object.send(association.name).build.tap do |new_association|
|
106
|
+
assign_attributes_for(new_association, get_association_attributes_from(old_association, association))
|
107
|
+
new_association = clone_recursively!(old_association, new_association)
|
93
108
|
end
|
94
109
|
end
|
95
110
|
end
|
@@ -103,12 +118,8 @@ module RailsAdminClone
|
|
103
118
|
a.macro == :has_and_belongs_to_many || (a.macro == :has_many && a.options.keys.include?(:through))
|
104
119
|
end
|
105
120
|
|
106
|
-
associations.each do |
|
107
|
-
|
108
|
-
method_ids = "#{association_name.to_s.singularize.to_sym}_ids"
|
109
|
-
|
110
|
-
Rails.logger.info "**** association_name: #{association_name}"
|
111
|
-
|
121
|
+
associations.each do |association|
|
122
|
+
method_ids = "#{association.name.to_s.singularize.to_sym}_ids"
|
112
123
|
new_object.send(:"#{method_ids}=", old_object.send(method_ids))
|
113
124
|
end
|
114
125
|
|
metadata
CHANGED
@@ -1,36 +1,32 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_admin_clone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.3
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Andrea Dal Ponte
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
11
|
+
date: 2013-11-21 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rails
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ! '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version: '3.
|
19
|
+
version: '3.2'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ! '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
|
-
version: '3.
|
26
|
+
version: '3.2'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rails_admin
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ! '>='
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ! '>='
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -63,26 +58,25 @@ files:
|
|
63
58
|
homepage: https://github.com/dalpo/rails_admin_clone
|
64
59
|
licenses:
|
65
60
|
- MIT
|
61
|
+
metadata: {}
|
66
62
|
post_install_message:
|
67
63
|
rdoc_options: []
|
68
64
|
require_paths:
|
69
65
|
- lib
|
70
66
|
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
-
none: false
|
72
67
|
requirements:
|
73
68
|
- - ! '>='
|
74
69
|
- !ruby/object:Gem::Version
|
75
70
|
version: '0'
|
76
71
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
-
none: false
|
78
72
|
requirements:
|
79
73
|
- - ! '>='
|
80
74
|
- !ruby/object:Gem::Version
|
81
75
|
version: '0'
|
82
76
|
requirements: []
|
83
77
|
rubyforge_project:
|
84
|
-
rubygems_version: 1.
|
78
|
+
rubygems_version: 2.1.10
|
85
79
|
signing_key:
|
86
|
-
specification_version:
|
80
|
+
specification_version: 4
|
87
81
|
summary: Rails Admin plugin to clone existing records
|
88
82
|
test_files: []
|