record_merge 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +13 -0
- data/config/routes.rb +2 -0
- data/lib/record_merge.rb +67 -0
- data/lib/record_merge/engine.rb +10 -0
- data/lib/record_merge/version.rb +3 -0
- data/lib/tasks/record_merge_tasks.rake +4 -0
- metadata +135 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 072c171f62a8e64f4d7e425e13d2dc9aa49270a2
|
4
|
+
data.tar.gz: ff7f35771aa556971d7a10a806680cd944b546be
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a25b7e29c7c579960ea38c2f685dee6ff44eff73b4af5853f5b6d75bccc7d31c7562176fc3c0a437a800c7c3d9843e72c82d2f343994ab4d21163e6b1824cd3d
|
7
|
+
data.tar.gz: 7d5f96163be2ead94d1549773b50b902acec1875ddef5d0d460d3e1f8ce0590326162e3ecc72ffc6b85a0cad654b25c1fd436a81035c87e834a266b6ad49d152
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 William Porter
|
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
data/config/routes.rb
ADDED
data/lib/record_merge.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require "record_merge/engine"
|
2
|
+
|
3
|
+
module RecordMerge
|
4
|
+
|
5
|
+
def self.merge(destination, source, options={})
|
6
|
+
attributes = options[:attributes] || [] # Attributes on model
|
7
|
+
only = options[:only] || [] # Only these relationships
|
8
|
+
except = options[:except] || [] # Except these relationships
|
9
|
+
copy_all_relations = options[:copy_all_relations] || true
|
10
|
+
delete_source = options[:delete_source] || true # Delete the source on complete
|
11
|
+
|
12
|
+
# Sanitize the values
|
13
|
+
attributes = sanitize_attributes(attributes)
|
14
|
+
|
15
|
+
raise AssertionError.new("source and destination classes don't match") if destination.class.base_class != source.class.base_class
|
16
|
+
raise AssertionError.new("can't provide both only and except params") if only.any? && except.any?
|
17
|
+
|
18
|
+
ActiveRecord::Base.transaction do
|
19
|
+
attributes.each do |attribute|
|
20
|
+
eval("destination.#{attribute} = source.#{attribute}")
|
21
|
+
end
|
22
|
+
|
23
|
+
if copy_all_relations || only.any? || except.any?
|
24
|
+
relations = source.class.reflections
|
25
|
+
|
26
|
+
relations.each do |relation|
|
27
|
+
relation_name = relation[0]
|
28
|
+
next if except.include?(relation_name.to_sym) && except.any?
|
29
|
+
next if only.include?(relation_name.to_sym) && only.any?
|
30
|
+
|
31
|
+
if source.send(relation_name)
|
32
|
+
case relation[1].class.name
|
33
|
+
when "ActiveRecord::Reflection::HasOneReflection"
|
34
|
+
source.send(relation_name).update("#{relation[1].foreign_key}": destination.id)
|
35
|
+
when "ActiveRecord::Reflection::HasManyReflection"
|
36
|
+
source.send(relation_name).update_all("#{relation[1].foreign_key}": destination.id)
|
37
|
+
when "ActiveRecord::Reflection::BelongsToReflection"
|
38
|
+
destination.update("#{relation_name}": source.send(relation_name))
|
39
|
+
when "ActiveRecord::Reflection::HasAndBelongsToManyReflection"
|
40
|
+
destination.update("#{relation_name}": source.send(relation_name))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
source.destroy! if delete_source
|
47
|
+
|
48
|
+
destination.save!
|
49
|
+
end
|
50
|
+
|
51
|
+
destination
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def self.sanitize_attributes(attributes)
|
57
|
+
if attributes.include?(:id)
|
58
|
+
puts "Cannot modify the ID of the destination."
|
59
|
+
attributes.delete(:id)
|
60
|
+
end
|
61
|
+
attributes
|
62
|
+
end
|
63
|
+
|
64
|
+
class AssertionError < StandardError ; end
|
65
|
+
end
|
66
|
+
|
67
|
+
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: record_merge
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- William Porter
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pg
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: factory_girl_rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: awesome_print
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: ''
|
98
|
+
email:
|
99
|
+
- wp@papercloud.com.au
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- MIT-LICENSE
|
105
|
+
- Rakefile
|
106
|
+
- config/routes.rb
|
107
|
+
- lib/record_merge.rb
|
108
|
+
- lib/record_merge/engine.rb
|
109
|
+
- lib/record_merge/version.rb
|
110
|
+
- lib/tasks/record_merge_tasks.rake
|
111
|
+
homepage: http://papercloud.com.au
|
112
|
+
licenses:
|
113
|
+
- MIT
|
114
|
+
metadata: {}
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 2.4.5
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: Merges ActiveRecord recods
|
135
|
+
test_files: []
|