binarylogic / searchlogic

Searchlogic provides object based searching, common named scopes, and other useful tools.
http://rdoc.info/projects/binarylogic/searchlogic
MIT License
1.39k stars 133 forks source link

boolean attribute returns [] when called with send Rails 2.3.5 / 2.4.19 + FIX #84

Open grosser opened 14 years ago

grosser commented 14 years ago

when i remove searchlogic gem everything is fine...
Shop.featured_message_blocked is a boolean column that in this case should be false

because:
User.first.shop.respond_to?(:featured_message_blocked) -> true User.first.shop.proxy_respond_to?(:featured_message_blocked) -> false

fix: def send_with_searchlogic(method, *args) if !respond_to?(method) && !proxy_reflection.options[:polymorphic] && proxy_reflection.klass.condition?(method)

hotfix in case this issue does not get resolved:

# FIXES: http://github.com/binarylogic/searchlogic/issues/issue/84
class ActiveRecord::Associations::AssociationProxy
  def send(method, *args)
    if !respond_to?(method) && !proxy_reflection.options[:polymorphic] && proxy_reflection.klass.condition?(method)
      proxy_reflection.klass.send(method, *args)
    else
      send_without_searchlogic(method, *args)
    end
  end
end
h-lame commented 14 years ago

Just FYI: this is the same issue as #83.

The above fix means you'll never be able to chain simple boolean searches off associated objects. e.g. (assuming a User with has_many :shops)

User.first.shops.featured_message_blocked

wouldn't find all the shops that have featured_message_blocked any more. It'd probably error. The best solution would be to avoid collisions between the named_scopes on the class and the attribute methods on the instance, but it's not clear what a good choice for renaming them would be. (locally I've gone for XXX_is_true and XXX_is_false).

TylerRick commented 14 years ago

I just got bit by this bug too.

My use case was pretty simple, I thought:

But when it tried to render the form, it gave:

ActionView::TemplateError (undefined method `to_i' for #) on line #23 of app/views/users/edit.html.haml:
    searchlogic (2.4.19) lib/searchlogic/named_scopes/conditions.rb:81:in `method_missing'
    searchlogic (2.4.19) lib/searchlogic/named_scopes/association_conditions.rb:19:in `method_missing'
    searchlogic (2.4.19) lib/searchlogic/named_scopes/association_ordering.rb:27:in `method_missing'
    searchlogic (2.4.19) lib/searchlogic/named_scopes/ordering.rb:30:in `method_missing'
    searchlogic (2.4.19) lib/searchlogic/named_scopes/or_conditions.rb:28:in `method_missing'
    vendor/rails/activerecord/lib/active_record/base.rb:1980:in `method_missing'
    vendor/plugins/will_paginate/lib/will_paginate/finder.rb:170:in `method_missing_with_paginate'
    vendor/rails/activerecord/lib/active_record/named_scope.rb:181:in `block in method_missing'
    vendor/rails/activerecord/lib/active_record/base.rb:2164:in `with_scope'
    vendor/rails/activerecord/lib/active_record/named_scope.rb:113:in `with_scope'
    vendor/rails/activerecord/lib/active_record/named_scope.rb:174:in `method_missing'
    vendor/rails/actionpack/lib/action_view/helpers/form_helper.rb:899:in `check_box_checked?'
    vendor/rails/actionpack/lib/action_view/helpers/form_helper.rb:831:in `to_check_box_tag'
    vendor/rails/actionpack/lib/action_view/helpers/form_helper.rb:710:in `check_box'
    vendor/rails/actionpack/lib/action_view/helpers/form_helper.rb:1032:in `check_box'
    app/views/users/edit.html.haml:23:in `block (2 levels) in _run_haml_app47views47users47edit46html46haml'

I temporarily moved that field to a non-nested form_for block and it didn't give the error, which I thought was interesting. The monkey patch given here fixed it for me.

I don't really care if I can't chain boolean searches. That's preferable over breaking rendering of a check_box for a boolean field.

Rails 2.3.6

sjain commented 14 years ago

Here is a failing test case for the issue: http://github.com/jdfrens/searchlogicspike

sjain commented 14 years ago

Replacing !proxy_respond_to?(method) with !respond_to?(method) in lib/searchlogic/active_record/association_proxy.rb seems to fix this.

twalpole commented 14 years ago

I'm using the following hotfix to allow for chaining of boolean searches on collections - but allow my formtastic forms to work on singulars

FIXES: http://github.com/binarylogic/searchlogic/issues/issue/84

class ActiveRecord::Associations::AssociationProxy
  def send(method, *args)
    if  (proxy_reflection.collection? || !respond_to?(method)) && !proxy_respond_to?(method) && !proxy_reflection.options[:polymorphic] && proxy_reflection.klass.condition?(method)
      proxy_reflection.klass.send(method, *args)
    else
      send_without_searchlogic(method, *args)
    end
  end
end