st-elsewhere 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/st-elsewhere.rb +36 -15
- data/st-elsewhere.gemspec +2 -2
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.4
|
data/lib/st-elsewhere.rb
CHANGED
@@ -50,6 +50,10 @@ module StElsewhere
|
|
50
50
|
association_singular = association_id.to_s.singularize
|
51
51
|
association_plural = association_id.to_s
|
52
52
|
through_association_singular = through.to_s.singularize
|
53
|
+
my_class = self
|
54
|
+
my_foreign_key = self.to_s.foreign_key
|
55
|
+
target_association_class = association_singular.classify.constantize
|
56
|
+
target_association_foreign_key = association_singular.foreign_key
|
53
57
|
|
54
58
|
# Hospital#doctor_ids
|
55
59
|
define_method("#{association_singular}_ids") do
|
@@ -67,12 +71,14 @@ module StElsewhere
|
|
67
71
|
# Hospital#doctors=
|
68
72
|
define_method("#{association_plural}=") do |new_associations|
|
69
73
|
through_class = through.to_s.singularize.camelize.constantize
|
70
|
-
current_associations = self.send("#{
|
71
|
-
|
72
|
-
new_associations = new_associations - current_associations
|
74
|
+
current_associations = self.send("#{association_singular}_ids")
|
75
|
+
desired_associations = self.class.associations_to_association_ids(new_associations)
|
73
76
|
|
74
|
-
|
75
|
-
|
77
|
+
removed_target_associations = current_associations - desired_associations
|
78
|
+
new_target_associations = desired_associations - current_associations
|
79
|
+
|
80
|
+
self.send("remove_#{association_singular}_associations", through_class, removed_target_associations)
|
81
|
+
self.send("add_#{association_singular}_associations", through_class, association_id, new_target_associations)
|
76
82
|
end
|
77
83
|
|
78
84
|
# Hospital#doctor_ids=
|
@@ -81,19 +87,17 @@ module StElsewhere
|
|
81
87
|
end
|
82
88
|
|
83
89
|
# Hospital#remove_doctor_associations (private)
|
84
|
-
define_method("remove_#{association_singular}_associations") do |
|
85
|
-
|
86
|
-
|
87
|
-
|
90
|
+
define_method("remove_#{association_singular}_associations") do |through_class, removed_target_associations|
|
91
|
+
association_instances_to_remove =
|
92
|
+
through_class.send("find_all_by_#{my_foreign_key}_and_#{target_association_foreign_key}", self.id, removed_target_associations)
|
93
|
+
through_class.delete(association_instances_to_remove)
|
88
94
|
end
|
89
95
|
|
90
96
|
# Hospital#add_doctor_associations (private)
|
91
|
-
define_method("add_#{association_singular}_associations") do |through_class, association_id,
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
my_buddy_id = Fixnum.eql?(association.class) ? association : association.id
|
96
|
-
new_association = through_class.new(myself => self.id, my_buddy => my_buddy_id)
|
97
|
+
define_method("add_#{association_singular}_associations") do |through_class, association_id, target_association_ids|
|
98
|
+
targets_to_add = target_association_class.find(target_association_ids)
|
99
|
+
targets_to_add.each do |target_association|
|
100
|
+
new_association = through_class.new(my_foreign_key => self.id, target_association_foreign_key => target_association.id)
|
97
101
|
new_association.save
|
98
102
|
end
|
99
103
|
end
|
@@ -102,6 +106,23 @@ module StElsewhere
|
|
102
106
|
|
103
107
|
end
|
104
108
|
|
109
|
+
def associations_to_association_ids(associations)
|
110
|
+
ids = []
|
111
|
+
if associations && !associations.empty?
|
112
|
+
associations.reject!{|a| a.to_s.empty? }
|
113
|
+
association_class = associations.first.class.to_s
|
114
|
+
ids = case association_class
|
115
|
+
when "String"
|
116
|
+
associations
|
117
|
+
when "Fixnum"
|
118
|
+
associations.map{|a| a.to_i }
|
119
|
+
else
|
120
|
+
associations.map{|a| a.id}
|
121
|
+
end
|
122
|
+
end
|
123
|
+
ids
|
124
|
+
end
|
125
|
+
|
105
126
|
private :collection_accessor_methods_elsewhere
|
106
127
|
|
107
128
|
end
|
data/st-elsewhere.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{st-elsewhere}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Brian Doll"]
|
12
|
-
s.date = %q{2009-11-
|
12
|
+
s.date = %q{2009-11-17}
|
13
13
|
s.description = %q{This gem provides has_many_elsewhere, an ActiveRecord class method to support many to many relationships in Rails applications, across multiple database connections.}
|
14
14
|
s.email = %q{brian@emphaticsolutions.com}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: st-elsewhere
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Doll
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-17 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|