ruby on rails, searchlogic and refactoring
Posted
by JohnMerlino
on Stack Overflow
See other posts from Stack Overflow
or by JohnMerlino
Published on 2010-05-06T20:23:05Z
Indexed on
2010/05/06
20:28 UTC
Read the original article
Hit count: 443
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
© Stack Overflow or respective owner