acts-as-joinable 0.2.3 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +2 -1
- data/Rakefile +1 -1
- data/lib/acts_as_joinable/core.rb +42 -14
- metadata +4 -4
data/README.markdown
CHANGED
@@ -100,4 +100,5 @@ You can always add columns to the relationship table, but the foundation is set.
|
|
100
100
|
|
101
101
|
http://rors.org/2008/10/26/dont-escape-in-strings
|
102
102
|
|
103
|
-
- Handle case where one side has_one and the other has_many
|
103
|
+
- Handle case where one side has_one and the other has_many
|
104
|
+
|
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ require 'rake/gempackagetask'
|
|
5
5
|
spec = Gem::Specification.new do |s|
|
6
6
|
s.name = "acts-as-joinable"
|
7
7
|
s.authors = ["Lance Pollard"]
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.6"
|
9
9
|
s.summary = "ActsAsJoinable: DRYing up Many-to-Many Relationships in ActiveRecord"
|
10
10
|
s.homepage = "http://github.com/viatropos/acts-as-joinable"
|
11
11
|
s.email = "lancejpollard@gmail.com"
|
@@ -2,6 +2,7 @@
|
|
2
2
|
# add changes so you can track when the join model changes
|
3
3
|
|
4
4
|
module ActsAsJoinable
|
5
|
+
|
5
6
|
module Core
|
6
7
|
def self.included(base)
|
7
8
|
base.send :include, ActsAsJoinable::Core::InstanceMethods
|
@@ -91,6 +92,7 @@ module ActsAsJoinable
|
|
91
92
|
# possible values of the context
|
92
93
|
values = [opts[:values] || opts[:value] || []].flatten.compact
|
93
94
|
value = values.first
|
95
|
+
status = opts[:status]
|
94
96
|
|
95
97
|
sql = opts[:conditions]
|
96
98
|
|
@@ -119,6 +121,7 @@ module ActsAsJoinable
|
|
119
121
|
plural_type = type.to_s.to_sym
|
120
122
|
end
|
121
123
|
class_name = opts[:class_name] || type.to_s.classify
|
124
|
+
source_type = opts[:source_type] || class_name
|
122
125
|
|
123
126
|
join_context = (context.blank? ? [singular_type] : Array(context)).map(&:to_s)
|
124
127
|
|
@@ -126,7 +129,7 @@ module ActsAsJoinable
|
|
126
129
|
:through => :relationships,
|
127
130
|
:class_name => class_name,
|
128
131
|
:source => :child,
|
129
|
-
:source_type =>
|
132
|
+
:source_type => source_type,
|
130
133
|
:conditions => sql
|
131
134
|
}
|
132
135
|
options[:extend] = extension if extension
|
@@ -134,6 +137,7 @@ module ActsAsJoinable
|
|
134
137
|
add_around_filters_for_join(options, "#{singular_type.to_s}_id".to_sym, before_add, :before_add)
|
135
138
|
add_around_filters_for_join(options, "#{singular_type.to_s}_id".to_sym, after_add, :after_add)
|
136
139
|
|
140
|
+
relationship_table = Relationship.quoted_table_name rescue nil
|
137
141
|
# relationships == [:parent, :child]
|
138
142
|
relationships.each do |relationship|
|
139
143
|
# Post.joins :tags, :as => :parent
|
@@ -141,26 +145,29 @@ module ActsAsJoinable
|
|
141
145
|
# relationship_table = `relationships`
|
142
146
|
relationship = opposite_for(relationship)
|
143
147
|
through_relationship = "#{relationship.to_s}_#{singular_type}_relationships".to_sym
|
144
|
-
relationship_table = Relationship.quoted_table_name rescue nil
|
145
148
|
|
146
149
|
options.merge!(:through => through_relationship, :source => relationship, :uniq => true)
|
147
150
|
|
148
151
|
join_value = value
|
149
152
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
# condition_string << "(#{relationship_table}.#{relationship}_type IN (?))"
|
153
|
+
through_conditions = {
|
154
|
+
"#{relationship}_type".to_sym => source_type
|
155
|
+
}
|
156
|
+
|
155
157
|
if join_context.include?(class_name.underscore)# && opposite_for(relationship).to_sym != :child
|
156
158
|
#conditions = [condition_string, [class_name]]
|
157
159
|
# if join_value
|
158
160
|
# condition_string << "(#{relationship_table}.value = ?)"
|
159
161
|
# conditions = [condition_string, join_value.to_s]
|
160
162
|
# end
|
163
|
+
if status
|
164
|
+
through_conditions = {:status => status}
|
165
|
+
end
|
161
166
|
else
|
162
|
-
|
163
|
-
|
167
|
+
#through_conditions = {}
|
168
|
+
through_conditions[:context] = join_context.dup
|
169
|
+
through_conditions[:value] = join_value unless join_value.blank?
|
170
|
+
through_conditions[:status] = status unless status.blank?
|
164
171
|
# condition_string << "(#{relationship_table}.context IN (?))"
|
165
172
|
# condition_string << " AND (#{relationship_table}.value = ?)" if join_value
|
166
173
|
# join_contexts = join_context.dup#[join_context, class_name.underscore].uniq
|
@@ -170,11 +177,11 @@ module ActsAsJoinable
|
|
170
177
|
|
171
178
|
through_options = {
|
172
179
|
:class_name => "Relationship",
|
173
|
-
:conditions =>
|
180
|
+
:conditions => through_conditions,
|
174
181
|
:as => opposite_for(relationship).to_sym
|
175
182
|
# :select => "#{relationship}_id, #{relationship}_type, id, #{opposite_for(relationship)}_id"
|
176
183
|
}
|
177
|
-
|
184
|
+
|
178
185
|
if association_type == :has_one
|
179
186
|
#options.delete(:after_add)
|
180
187
|
options.delete(:uniq)
|
@@ -194,7 +201,7 @@ module ActsAsJoinable
|
|
194
201
|
has_many through_relationship, through_options
|
195
202
|
end
|
196
203
|
|
197
|
-
add_association(relationship.to_s, plural_type, options, join_context, join_value, &block)
|
204
|
+
add_association(relationship.to_s, plural_type, singular_type, options, join_context, join_value, status, &block)
|
198
205
|
|
199
206
|
if association_type == :has_one
|
200
207
|
add_has_one(singular_type, plural_type, through_relationship, class_name, join_context, join_value, options)
|
@@ -325,19 +332,36 @@ module ActsAsJoinable
|
|
325
332
|
end
|
326
333
|
end
|
327
334
|
|
328
|
-
def add_association(relationship, plural_type, options, join_context, join_value, &block)
|
335
|
+
def add_association(relationship, plural_type, singular_type, options, join_context, join_value, status, &block)
|
329
336
|
eval_options = {}
|
337
|
+
|
330
338
|
eval_options[:context] = join_context.to_s unless join_context.to_s == options[:class_name].to_s.underscore.singularize
|
331
|
-
|
332
339
|
eval_options[:value] = join_value unless join_value.blank?
|
340
|
+
eval_options[:status] = status unless status.blank?
|
341
|
+
the_opposite = opposite_for(relationship.to_s)
|
333
342
|
# has_many :users, :through => :child_relationships
|
334
343
|
plural_relationship = "#{relationship}_#{plural_type}".to_sym
|
344
|
+
|
345
|
+
define_method "#{singular_type}_relationships" do |*args|
|
346
|
+
args = args.flatten.compact
|
347
|
+
options = args.extract_options!
|
348
|
+
ids = args.flatten.compact.map do |item|
|
349
|
+
item.respond_to?(:id) ? item.id : item
|
350
|
+
end
|
351
|
+
options["#{relationship}_id"] = ids
|
352
|
+
send("#{relationship}_#{singular_type}_relationships").all(:conditions => options)
|
353
|
+
end
|
354
|
+
|
335
355
|
unless has_association?(plural_relationship)
|
336
356
|
send(:has_many, plural_relationship, options) do
|
337
357
|
class_eval <<-EOF
|
338
358
|
def construct_join_attributes(associate)
|
339
359
|
super.merge(#{eval_options.inspect})
|
340
360
|
end
|
361
|
+
|
362
|
+
def relationships_for(*args)
|
363
|
+
@owner.send("#{singular_type}_relationships", *args)
|
364
|
+
end
|
341
365
|
EOF
|
342
366
|
end
|
343
367
|
end
|
@@ -348,6 +372,10 @@ module ActsAsJoinable
|
|
348
372
|
def construct_join_attributes(associate)
|
349
373
|
super.merge(#{eval_options.inspect})
|
350
374
|
end
|
375
|
+
|
376
|
+
def relationships_for(*args)
|
377
|
+
@owner.send("#{singular_type}_relationships", *args)
|
378
|
+
end
|
351
379
|
EOF
|
352
380
|
end
|
353
381
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts-as-joinable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 6
|
10
|
+
version: 0.2.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Lance Pollard
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-11-01 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|