acts-as-joinable 0.2.2 → 0.2.3

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.
Files changed (3) hide show
  1. data/Rakefile +1 -1
  2. data/lib/acts_as_joinable/core.rb +11 -7
  3. metadata +3 -3
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.2"
8
+ s.version = "0.2.3"
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"
@@ -1,5 +1,6 @@
1
1
  # http://github.com/pluginaweek/preferences/blob/master/lib/preferences.rb
2
2
  # add changes so you can track when the join model changes
3
+
3
4
  module ActsAsJoinable
4
5
  module Core
5
6
  def self.included(base)
@@ -66,8 +67,9 @@ module ActsAsJoinable
66
67
  relationships = [opts[:as] || :parent].flatten.map(&:to_sym)
67
68
  end
68
69
 
69
- before_add = opts[:before_add]
70
- after_add = opts[:after_add]
70
+ before_add = opts[:before_add]
71
+ after_add = opts[:after_add]
72
+ extension = opts[:extend]
71
73
 
72
74
  opts[:class_name] ||= opts[:source].to_s.camelize if opts[:source]
73
75
 
@@ -127,6 +129,7 @@ module ActsAsJoinable
127
129
  :source_type => class_name,
128
130
  :conditions => sql
129
131
  }
132
+ options[:extend] = extension if extension
130
133
 
131
134
  add_around_filters_for_join(options, "#{singular_type.to_s}_id".to_sym, before_add, :before_add)
132
135
  add_around_filters_for_join(options, "#{singular_type.to_s}_id".to_sym, after_add, :after_add)
@@ -244,7 +247,8 @@ module ActsAsJoinable
244
247
  def action_from_joined(action, model, conditions)
245
248
  join_conditions(model, conditions.delete(:relationship) || {}) do |kind, source_type, relationship_conditions|
246
249
  ids = Relationship.select_attributes("#{kind}_id", relationship_conditions).uniq.compact.map(&:to_i)
247
- send(action, :conditions => {:id => ids}.merge(conditions))
250
+ conditions.reverse_merge!(:id => ids)
251
+ send(action, :conditions => conditions)
248
252
  end
249
253
  end
250
254
 
@@ -259,13 +263,13 @@ module ActsAsJoinable
259
263
  else
260
264
  options = {:source => "parent", :source_type => model.to_s.camelize}
261
265
  end
262
- kind = conditions[:as] || options[:source].to_s
266
+ kind = conditions.delete(:as) || options[:source].to_s
263
267
  source_type = options[:source_type].constantize
264
-
268
+
265
269
  result = conditions.reverse_merge(
266
270
  "#{opposite_for(kind)}_type" => related_classes.map(&:name),
267
271
  "#{kind}_type" => source_type.related_classes.map(&:name)
268
- )
272
+ ).stringify_keys
269
273
 
270
274
  if block_given?
271
275
  yield(opposite_for(kind), source_type, result)
@@ -296,7 +300,7 @@ module ActsAsJoinable
296
300
  send(plural_type)[-1]#.last
297
301
  end
298
302
  define_method "#{singular_type}=" do |value|
299
- return value if send(plural_type).map(&:id).include?(value.id)
303
+ return value if value && value.respond_to?(:id) && send(plural_type).map(&:id).include?(value.id)
300
304
 
301
305
  send(plural_type) << value unless value.blank?
302
306
 
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: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 2
10
- version: 0.2.2
9
+ - 3
10
+ version: 0.2.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Lance Pollard