acts-as-joinable 0.1.6.5 → 0.1.6.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/Rakefile +1 -1
  2. data/lib/acts_as_joinable/core.rb +65 -0
  3. metadata +4 -4
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.1.6.5"
8
+ s.version = "0.1.6.8"
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"
@@ -156,6 +156,9 @@ module ActsAsJoinable
156
156
 
157
157
  if association_type == :has_one
158
158
  add_has_one(singular_type, plural_type, through_relationship, class_name, join_context, join_value, options)
159
+ add_class_relationship_method(singular_type)
160
+ else
161
+ add_class_relationship_method(plural_type)
159
162
  end
160
163
 
161
164
  if nestable
@@ -177,12 +180,74 @@ module ActsAsJoinable
177
180
  super(*args)
178
181
  initialize_acts_as_joinable_on_core
179
182
  end
183
+
184
+ def find_joined_with_join(model, conditions = {})
185
+ options = join_conditions(model, conditions.delete(:relationship) || {})
186
+ options.merge!(conditions)
187
+ source_type.all(options)
188
+ end
189
+
190
+ def find_joined(model, conditions = {})
191
+ join_conditions(model, conditions.delete(:relationship) || {}) do |kind, source_type, relationship_conditions|
192
+ ids = ActsAsJoinable::Relationship.select_attributes("#{opposite_for(kind)}_id", relationship_conditions).uniq
193
+ source_type.all(:conditions => {:id => ids}.merge(conditions))
194
+ end
195
+ end
196
+
197
+ def find_from_joined(model, conditions = {})
198
+ action_from_joined(:all, model, conditions)
199
+ end
200
+
201
+ def count_from_joined(model, conditions)
202
+ action_from_joined(:count, model, conditions)
203
+ end
204
+
205
+ def action_from_joined(action, model, conditions)
206
+ join_conditions(model, conditions.delete(:relationship) || {}) do |kind, source_type, relationship_conditions|
207
+ ids = ActsAsJoinable::Relationship.select_attributes("#{kind}_id", relationship_conditions).uniq
208
+ send(action, :conditions => {:id => ids}.merge(conditions))
209
+ end
210
+ end
211
+
212
+ # def join_scope(name, conditions = {}, &block)
213
+ #add_class_relationship_method(method)
214
+ # end
215
+
216
+ def join_conditions(model, conditions = {}, &block)
217
+ relationship = self.reflect_on_all_associations.detect {|a| a.name == model.to_sym}
218
+ if relationship
219
+ options = relationship.options
220
+ else
221
+ options = {:source => "parent", :source_type => model.to_s.camelize}
222
+ end
223
+ kind = conditions[:as] || options[:source].to_s
224
+ source_type = options[:source_type].constantize
225
+
226
+ result = conditions.reverse_merge(
227
+ "#{opposite_for(kind)}_type" => related_classes.map(&:name),
228
+ "#{kind}_type" => source_type.related_classes.map(&:name)
229
+ )
230
+
231
+ if block_given?
232
+ yield(kind, source_type, result)
233
+ else
234
+ result
235
+ end
236
+ end
180
237
 
181
238
  private
182
239
  def opposite_for(role)
183
240
  role.to_s == "parent" ? "child" : "parent"
184
241
  end
185
242
 
243
+ def add_class_relationship_method(method)
244
+ class_eval <<-EOF
245
+ def self.#{method}(options = {})
246
+ find_joined(:#{method.to_s}, options)
247
+ end
248
+ EOF
249
+ end
250
+
186
251
  def has_association?(name)
187
252
  self.reflect_on_all_associations.map(&:name).include?(name.to_sym)
188
253
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts-as-joinable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 85
4
+ hash: 79
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
9
  - 6
10
- - 5
11
- version: 0.1.6.5
10
+ - 8
11
+ version: 0.1.6.8
12
12
  platform: ruby
13
13
  authors:
14
14
  - Lance Pollard
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-10-04 00:00:00 -05:00
19
+ date: 2010-10-05 00:00:00 -05:00
20
20
  default_executable:
21
21
  dependencies: []
22
22