record_merge 0.0.1

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.
@@ -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
@@ -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.
@@ -0,0 +1,13 @@
1
+ require "bundler"
2
+ require 'rake'
3
+ Bundler.setup
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ def cmd(command)
7
+ puts command
8
+ raise unless system command
9
+ end
10
+
11
+ # Import all our rake tasks
12
+ FileList['tasks/**/*.rake'].each { |task| import task }
13
+
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -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
+
@@ -0,0 +1,10 @@
1
+ module RecordMerge
2
+ class Engine < ::Rails::Engine
3
+ config.generators do |g|
4
+ g.test_framework :rspec, :fixture => false
5
+ g.fixture_replacement :factory_girl, :dir => 'spec/factories'
6
+ g.assets false
7
+ g.helper false
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module RecordMerge
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :record_merge do
3
+ # # Task goes here
4
+ # end
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: []