views:

179

answers:

1

Hey all, I'mt not too familiar with searchlogic plugin for rails (I did view the railscasts but wasn't helpful in relation to the specific code below). Can anyone briefly describe how it is being used in the three methods below? Thanks for any response.

 def extract_order
@order_by = if params[:order].present?
  field = params[:order].gsub(".", "_")
  field = field.starts_with?('-') ? 'descend_by_'+field[1..-1] : 'ascend_by_'+field
  field.to_sym
else
  # Workaround
  'searchlogic'.to_sym
end
end

def find_resources
@search_conditions = params[:search_conditions] || {} # See http://www.binarylogic.com/2008/11/30/searchlogic-1-5-7-complex-searching-no-longer-a-problem/
@resources = @resource_model.send(@order_by).searchlogic(:conditions => @search_conditions) 
end

def apply_filters
f = filter_by
f.each do |filter_field|
  filter_constraints = params[filter_field.to_sym]
  if filter_constraints.present?
    # Apply searchlogic's scope
    @resources.send(filter_field,filter_constraints)
  end
end
end
A: 

the method apply_filter is not being called.

The method find_resources are using the content from @order_by (despite the method extract order is not called)

So the search in the resource model is being done using the params (probably user input) stored in the variable search condition and using the @order_by to say the order that it must use.

Note that your application is getting some parameter and changing the "." to "_" and getting a substring (1..-1, actually removing the first character, and using it as parameter for a scoped search (ascend_by_|descend_by_).
its a feature from searchlogic and you can use it as a dynamic finder: ascend_by_name_of_field.

IMO, it is looking messy. You are assuming that @order_by is not empty and that the function extract_order already ran. Another thing, actions that has no user interaction shouldnt be accessible.

VP
Thanks, but what do you think the value of filter_by is supposed to be? I was thinking that it was a built in keyword of searchlogic.
JohnMerlino
i just updated my answer
VP
Actually, you were right. I did leave out something: def filter_by %w(state_key_eq site_num_like school_enabled_eq district_enabled_eq books_enabled_eq) end
JohnMerlino