move_associations 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 juarlex
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/README.rdoc ADDED
@@ -0,0 +1,30 @@
1
+ = MoveAssociations
2
+
3
+ This gem extends ActiveRecord to move associated records based on has_many and has_one associations.
4
+
5
+ = Getting started
6
+
7
+ In your Gemfile:
8
+
9
+ gem "move_associations"
10
+
11
+ = Example usage
12
+
13
+ class City
14
+ validates_presence_of :name
15
+ belongs_to :state
16
+ has_many :addresses
17
+ has_one :person
18
+ has_many :institutions
19
+ end
20
+
21
+ @city = City.find(10)
22
+ @city.move_association(:addresses, 15) => # It move the associated records in addresses table from city with ID 10 to city with ID 15.
23
+
24
+ @city = City.find(10)
25
+ @city.move_associations(15) => # It moves all the associated records from city with ID 10 to city with ID 15 using the
26
+ # has_many and has_one relationships.
27
+
28
+ = Copyright
29
+
30
+ Copyright (c) 2010 MonsterLabs. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "move_associations"
8
+ gem.summary = %Q{A gem for ActiveRecord to move associated records based on has_many and has_one associations. }
9
+ gem.description = %Q{This gem extends ActiveRecord to move associated records based on has_many and has_one associations. }
10
+ gem.email = "alex@mosterlabs.com.mx"
11
+ gem.homepage = "http://github.com/monsterlabs/move_associations"
12
+ gem.authors = ["Alejandro Juarez"]
13
+ gem.add_development_dependency "shoulda"
14
+ gem.add_dependency "activerecord", "~> 3.0.0"
15
+ gem.add_dependency "activesupport", "~> 3.0.0"
16
+ gem.files = FileList["[A-Z]*", "{lib}/**/*"]
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/test_*.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ task :default => :test
46
+
47
+ require 'rake/rdoctask'
48
+ Rake::RDocTask.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "move_associations #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,15 @@
1
+ module MoveAssociations
2
+ module ClassMethods
3
+ def destroy_all_with_empty_associations
4
+ all.each do |record|
5
+ record.destroy_if_associations_are_empty
6
+ end
7
+ end
8
+
9
+ def associations_to_move
10
+ (self.reflect_on_all_associations(:has_many) + self.reflect_on_all_associations(:has_one)).collect do |association|
11
+ association unless association.options.has_key? :through
12
+ end.compact
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,35 @@
1
+ module MoveAssociations
2
+ module InstanceMethods
3
+ def destroy_if_associations_are_empty
4
+ destroy unless has_associated_records?
5
+ end
6
+
7
+ def move_association(association_name, new_id)
8
+ if self.respond_to? association_name
9
+ association_records(association_name).each { |record| record.update_attributes(self.class.name.foreign_key => new_id) }
10
+ end
11
+ end
12
+
13
+ def move_associations(new_id)
14
+ self.class.associations_to_move.collect do |association|
15
+ move_association(association.name, new_id)
16
+ end
17
+ end
18
+
19
+ def has_associated_records?
20
+ associated_records_size > 0
21
+ end
22
+
23
+ def association_records(association_name)
24
+ [send(association_name.to_sym)].flatten.compact
25
+ end
26
+
27
+ def associated_records_size
28
+ associated_records = 0
29
+ self.class.associations_to_move.collect do |association|
30
+ associated_records += association_records(association.name).size
31
+ end
32
+ associated_records
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,13 @@
1
+ module MoveAssociations
2
+ class Railtie < Rails::Railtie
3
+ railtie_name :move_associations
4
+ initializer "move_associations.active_record" do |app|
5
+ if defined? ::ActiveRecord
6
+ ::ActiveRecord::Base.class_eval do
7
+ extend MoveAssociations::ClassMethods
8
+ end
9
+ ::ActiveRecord::Base.send(:include, MoveAssociations::InstanceMethods)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module MoveAssociations
2
+ autoload :ClassMethods, 'move_associations/class_methods'
3
+ autoload :InstanceMethods, 'move_associations/instance_methods'
4
+ autoload :Railtie, 'move_associations/railtie'
5
+ end
6
+
7
+ if defined?(::Rails::Railtie)
8
+ require 'move_associations/railtie'
9
+ end
10
+
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'move_associations'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestMoveAssociations < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: move_associations
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Alejandro Juarez
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-25 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: activerecord
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 7
44
+ segments:
45
+ - 3
46
+ - 0
47
+ - 0
48
+ version: 3.0.0
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: activesupport
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 7
60
+ segments:
61
+ - 3
62
+ - 0
63
+ - 0
64
+ version: 3.0.0
65
+ type: :runtime
66
+ version_requirements: *id003
67
+ description: "This gem extends ActiveRecord to move associated records based on has_many and has_one associations. "
68
+ email: alex@mosterlabs.com.mx
69
+ executables: []
70
+
71
+ extensions: []
72
+
73
+ extra_rdoc_files:
74
+ - LICENSE
75
+ - README.rdoc
76
+ files:
77
+ - LICENSE
78
+ - README.rdoc
79
+ - Rakefile
80
+ - VERSION
81
+ - lib/move_associations.rb
82
+ - lib/move_associations/class_methods.rb
83
+ - lib/move_associations/instance_methods.rb
84
+ - lib/move_associations/railtie.rb
85
+ - test/helper.rb
86
+ - test/test_move_associations.rb
87
+ has_rdoc: true
88
+ homepage: http://github.com/monsterlabs/move_associations
89
+ licenses: []
90
+
91
+ post_install_message:
92
+ rdoc_options:
93
+ - --charset=UTF-8
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ hash: 3
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ requirements: []
115
+
116
+ rubyforge_project:
117
+ rubygems_version: 1.3.7
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: A gem for ActiveRecord to move associated records based on has_many and has_one associations.
121
+ test_files:
122
+ - test/helper.rb
123
+ - test/test_move_associations.rb