searchlogic
searchlogic copied to clipboard
boolean attribute returns [] when called with send Rails 2.3.5 / 2.4.19 + FIX
when i remove searchlogic gem everything is fine...
Shop.featured_message_blocked is a boolean column that in this case should be false
- User.first.shop.featured_message_blocked -> false
- User.first.shop.send(:featured_message_blocked) -> [](-> makes check_boxes in forms raise errors)
- User.first.shop.
__send__
(:featured_message_blocked) -> 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
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).
I just got bit by this bug too.
My use case was pretty simple, I thought:
- A boolean column :accepted_terms and a view like this:
- form_for @user do |f|
- f.fields_for :company do |f| = f.check_box :accepted_terms
But when it tried to render the form, it gave:
ActionView::TemplateError (undefined method `to_i' for #<0x0000000d7409e8>0x0000000d7409e8>
Here is a failing test case for the issue: http://github.com/jdfrens/searchlogicspike
Replacing !proxy_respond_to?(method) with !respond_to?(method) in lib/searchlogic/active_record/association_proxy.rb seems to fix this.
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