rails_admin_clone 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'RailsAdminClone'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
@@ -0,0 +1,10 @@
1
+ en:
2
+ admin:
3
+ actions:
4
+ clone:
5
+ title: "Clone for %{model_label} '%{object_label}'"
6
+ menu: "Clone"
7
+ breadcrumb: "Clone"
8
+ link: "Clone"
9
+ bulk_link: "Clone selected %{model_label_plural}"
10
+ done: "Cloneed"
@@ -0,0 +1,10 @@
1
+ it:
2
+ admin:
3
+ actions:
4
+ clone:
5
+ title: "Clona %{model_label} '%{object_label}'"
6
+ menu: "Clona"
7
+ breadcrumb: "Clona"
8
+ link: "Clona"
9
+ bulk_link: "Clona %{model_label_plural} selezionati"
10
+ done: "Clonato"
@@ -0,0 +1,56 @@
1
+ require 'rails_admin/config/actions'
2
+ require 'rails_admin/config/actions/base'
3
+
4
+ module RailsAdmin
5
+ module Config
6
+ module Actions
7
+ class Clone < Base
8
+ RailsAdmin::Config::Actions.register(self)
9
+
10
+ register_instance_option :member do
11
+ true
12
+ end
13
+
14
+ register_instance_option :http_methods do
15
+ [:get]
16
+ end
17
+
18
+ register_instance_option :controller do
19
+ Proc.new do
20
+ action_config = RailsAdmin::Config::Actions.find(:clone, {controller: self, abstract_model: @abstract_model, object: @object })
21
+ model_cloner = RailsAdminClone::ModelCloner.new(@object)
22
+ custom_method = model_config.clone_config.custom_method
23
+
24
+ if custom_method.present?
25
+ @object = model_cloner.method_clone(custom_method)
26
+ else
27
+ @object = model_cloner.default_clone
28
+ end
29
+
30
+ @authorization_adapter && @authorization_adapter.attributes_for(:new, @abstract_model).each do |name, value|
31
+ @object.send("#{name}=", value)
32
+ end
33
+
34
+ if object_params = params[@abstract_model.to_param]
35
+ @object.set_attributes(@object.attributes.merge(object_params))
36
+ end
37
+
38
+ respond_to do |format|
39
+ format.html { render @action.template_name }
40
+ format.js { render @action.template_name, :layout => false }
41
+ end
42
+
43
+ end
44
+ end
45
+
46
+ register_instance_option :template_name do
47
+ :new
48
+ end
49
+
50
+ register_instance_option :link_icon do
51
+ 'icon-copy'
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,4 @@
1
+ module RailsAdminClone
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,109 @@
1
+ module RailsAdminClone
2
+ class ModelCloner
3
+
4
+ def initialize(original_model)
5
+ @original_model = original_model
6
+ end
7
+
8
+ def original_model
9
+ @original_model
10
+ end
11
+
12
+ def default_clone
13
+ new_object = self.clone_object(self.original_model)
14
+ self.clone_recursively(self.original_model, new_object)
15
+ end
16
+
17
+ def method_clone(method)
18
+ self.original_model.send(method)
19
+ end
20
+
21
+ protected
22
+
23
+ def clone_recursively(old_object, new_object)
24
+ new_object = clone_has_one(old_object, new_object)
25
+ new_object = clone_has_many(old_object, new_object)
26
+ new_object = clone_habtm(old_object, new_object)
27
+
28
+ new_object
29
+ end
30
+
31
+ # clone object without associations
32
+ def clone_object(old_object)
33
+ object = build_from(old_object)
34
+ attributes = old_object.attributes.select do |k,v|
35
+ ![object.class.primary_key, 'created_at', 'updated_at'].include?(k)
36
+ end
37
+
38
+ object.assign_attributes attributes, without_protection: true
39
+ object
40
+ end
41
+
42
+ # clone has_one associations
43
+ def clone_has_one(old_object, new_object)
44
+ old_object.class.reflect_on_all_associations(:has_one).each do |class_association|
45
+ association_name = class_association.name
46
+ old_association = old_object.send(association_name)
47
+
48
+ if old_association
49
+ # primary_key = association_name.to_s.singularize.camelize.constantize.try(:primary_key) || 'id'
50
+ primary_key = 'id'
51
+
52
+ attributes = old_association.attributes.select do |k,v|
53
+ ![primary_key, class_association.try(:foreign_key), class_association.try(:type), 'created_at', 'updated_at'].include?(k)
54
+ end
55
+
56
+ new_object.send(:"build_#{association_name}").tap do |new_association|
57
+ new_association.assign_attributes attributes, without_protection: true
58
+ new_association = self.clone_recursively(old_association, new_association)
59
+ end
60
+ end
61
+ end
62
+
63
+ new_object
64
+ end
65
+
66
+ # clone has_many associations
67
+ def clone_has_many(old_object, new_object)
68
+ old_object.class.reflect_on_all_associations(:has_many).each do |class_association|
69
+ association_name = class_association.name
70
+ # primary_key = association_name.to_s.singularize.camelize.constantize.try(:primary_key) || 'id'
71
+ primary_key = 'id'
72
+
73
+ old_object.send(association_name).each do |old_association|
74
+ attributes = old_association.attributes.select do |k,v|
75
+ ![primary_key, class_association.try(:foreign_key), class_association.try(:type), 'created_at', 'updated_at'].include?(k)
76
+ end
77
+
78
+ new_object.send(association_name).build.tap do |new_association|
79
+ new_association.assign_attributes attributes, without_protection: true
80
+ new_association = self.clone_recursively(old_association, new_association)
81
+ end
82
+ end
83
+ end
84
+
85
+ new_object
86
+ end
87
+
88
+ # clone has_and_belongs_to_many associtations
89
+ def clone_habtm(old_object, new_object)
90
+ old_object.class.reflect_on_all_associations(:has_and_belongs_to_many).each do |class_association|
91
+ association_name = class_association.name
92
+ method_ids = "#{association_name.to_s.singularize.to_sym}_ids"
93
+
94
+ new_object.send(method_ids, old_object.send(method_ids))
95
+ end
96
+
97
+ new_object
98
+ end
99
+
100
+ def build_from(object)
101
+ object.class.new
102
+ end
103
+
104
+ end
105
+ end
106
+
107
+
108
+
109
+
@@ -0,0 +1,16 @@
1
+ require 'rails_admin/config/sections/base'
2
+
3
+ module RailsAdmin
4
+ module Config
5
+ module Sections
6
+ # Configuration of the clone action
7
+ class CloneConfig < RailsAdmin::Config::Sections::Base
8
+
9
+ register_instance_option :custom_method do
10
+ nil
11
+ end
12
+
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module RailsAdminClone
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ require "rails_admin_clone/engine"
2
+
3
+ module RailsAdminClone
4
+ end
5
+
6
+ require 'rails_admin_clone/section'
7
+ require 'rails_admin/config/sections'
8
+ require 'rails_admin_clone/model_cloner'
9
+ require 'rails_admin_clone/action'
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_admin_clone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrea Dal Ponte
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rails_admin
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Rails Admin plugin to clone existing records
47
+ email:
48
+ - info@andreadalponte.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - config/locales/clone.en.yml
54
+ - config/locales/clone.it.yml
55
+ - lib/rails_admin_clone/action.rb
56
+ - lib/rails_admin_clone/engine.rb
57
+ - lib/rails_admin_clone/model_cloner.rb
58
+ - lib/rails_admin_clone/section.rb
59
+ - lib/rails_admin_clone/version.rb
60
+ - lib/rails_admin_clone.rb
61
+ - MIT-LICENSE
62
+ - Rakefile
63
+ homepage: https://github.com/dalpo/rails_admin_clone
64
+ licenses:
65
+ - MIT
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.25
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Rails Admin plugin to clone existing records
88
+ test_files: []