Permalink
Browse files

check class hierarchy with is_a? in PredicateBuilder.expand

add changelog entry for #11945
  • Loading branch information...
Mik-die committed Aug 20, 2013
1 parent af3a69a commit c2e084ac486a715fb04d49e4c2c33f62b07535dd
@@ -1,3 +1,9 @@
+* Fix PredicateBuilder so polymorhic association keys in `where` clause can
+ also accept not only `ActiveRecord::Base` direct descendances (decorated
+ models, for example).
+
+ *Mikhail Dieterle*
+
* PostgreSQL adapter recognizes negative money values formatted with
parentheses (eg. `($1.25) # => -1.25`)).
Fixes #11899.
@@ -55,7 +55,7 @@ def self.expand(klass, table, column, value)
#
# For polymorphic relationships, find the foreign key and type:
# PriceEstimate.where(estimate_of: treasure)
- if klass && value.class < Base && reflection = klass.reflect_on_association(column.to_sym)
+ if klass && value.is_a?(Base) && reflection = klass.reflect_on_association(column.to_sym)
if reflection.polymorphic?
queries << build(table[reflection.foreign_type], value.class.base_class)
end
@@ -81,6 +81,31 @@ def test_polymorphic_sti_nested_where
assert_equal expected.to_sql, actual.to_sql
end
+ def test_decorated_polymorphic_where
+ treasure_decorator = Struct.new(:model) do
+ def self.method_missing(method, *args, &block)
+ Treasure.send(method, *args, &block)
+ end
+
+ def is_a?(klass)
+ model.is_a?(klass)
+ end
+
+ def method_missing(method, *args, &block)
+ model.send(method, *args, &block)
+ end
+ end
+
+ treasure = Treasure.new
+ treasure.id = 1
+ decorated_treasure = treasure_decorator.new(treasure)
+
+ expected = PriceEstimate.where(estimate_of_type: 'Treasure', estimate_of_id: 1)
+ actual = PriceEstimate.where(estimate_of: decorated_treasure)
+
+ assert_equal expected.to_sql, actual.to_sql
+ end
+
def test_aliased_attribute
expected = Topic.where(heading: 'The First Topic')
actual = Topic.where(title: 'The First Topic')

0 comments on commit c2e084a

Please sign in to comment.