duplicate_it 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/duplicate_it.rb +71 -0
- metadata +46 -0
data/lib/duplicate_it.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
module DuplicateIt
|
2
|
+
def self.included(base)
|
3
|
+
base.extend(ClassMethods)
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
#Test method
|
8
|
+
def duplicate_it(*args)
|
9
|
+
puts "------- from duplicate_it --------"
|
10
|
+
puts args.inspect
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def duplicate
|
15
|
+
create_duplicate_of_self(self)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
#Creates a duplicate record from an existing record, ignoring 'ignorable_attributes'
|
21
|
+
def create_duplicate_record(record)
|
22
|
+
ignorable_attributes = ["id", "type", "created_at", "updated_at"]
|
23
|
+
record_attributes = record.attributes
|
24
|
+
ignorable_attributes.each { |ignorable_attribute| record_attributes.delete(ignorable_attribute) }
|
25
|
+
|
26
|
+
duplicate_record = record.class.new(record_attributes)
|
27
|
+
if duplicate_record.save
|
28
|
+
return duplicate_record
|
29
|
+
else
|
30
|
+
puts "Error saving #{duplicate_record}"
|
31
|
+
puts "Error trace: #{duplicate_record.errros.inspect}"
|
32
|
+
end
|
33
|
+
return nil
|
34
|
+
end
|
35
|
+
|
36
|
+
#Creates a duplicate of self
|
37
|
+
def create_duplicate_of_self(record)
|
38
|
+
# Approach-I: Using FILO technique to create object starting form last node children
|
39
|
+
# if record.class.reflect_on_all_associations(:has_many).size > 0
|
40
|
+
# record.class.reflect_on_all_associations(:has_many).each do |has_many_reflection|
|
41
|
+
# children_records = record.send(has_many_reflection.name).collect { |has_many_record| create_duplicate_of_self(has_many_record) }
|
42
|
+
# duplicate_record = create_duplicate_record(record)
|
43
|
+
# puts "===================== #{duplicate_record.errors.inspect}"
|
44
|
+
# duplicate_record.send("#{has_many_reflection.name}=", children_records) unless duplicate_record.nil?
|
45
|
+
# return duplicate_record
|
46
|
+
# end
|
47
|
+
# else
|
48
|
+
# return create_duplicate_record(record)
|
49
|
+
# end
|
50
|
+
|
51
|
+
|
52
|
+
# Approach-II : Using FIFO technique to create object starting from trunk and then traversing to children node
|
53
|
+
duplicate_record = create_duplicate_record(record)
|
54
|
+
unless duplicate_record.nil?
|
55
|
+
record.class.reflect_on_all_associations(:has_many).each do |has_many_reflection|
|
56
|
+
if record.send(has_many_reflection.name).count > 0
|
57
|
+
record.send(has_many_reflection.name).each do |has_many_record|
|
58
|
+
duplicate_record.send("#{has_many_reflection.name}=", (duplicate_record.send("#{has_many_reflection.name}") + [create_duplicate_of_self(has_many_record)]))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
return duplicate_record
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
#Adding to ActiveRecord::Base
|
69
|
+
class ActiveRecord::Base
|
70
|
+
include DuplicateIt
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: duplicate_it
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Praveen Kumar Sinha
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-04 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: A simple gem to dulicate a model instance, including its 'has_many' assocaited
|
15
|
+
objects
|
16
|
+
email: praveen.kumar.sinha@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/duplicate_it.rb
|
22
|
+
homepage: http://rubygems.org/gems/duplicate_it
|
23
|
+
licenses: []
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.13
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: DuplicateIt!
|
46
|
+
test_files: []
|