简体   繁体   English

Ruby on Rails,SearchLogic和重构

[英]ruby on rails, searchlogic and refactoring

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). 我对Rails的searchlogic插件不太熟悉(我确实查看过railscasts,但对下面的特定代码没有帮助)。 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

the method apply_filter is not being called. 未调用apply_filter方法。

The method find_resources are using the content from @order_by (despite the method extract order is not called) find_resources方法使用@order_by中的内容(尽管未调用方法提取顺序)

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. 因此,使用存储在变量搜索条件中的参数(可能是用户输入)并使用@order_by表示必须使用的顺序来完成资源模型中的搜索。

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_). 到“ _”并得到一个子字符串(1 ..- 1,实际上删除了第一个字符,并将其用作范围搜索的参数(ascend_by_ | descend_by_)。
its a feature from searchlogic and you can use it as a dynamic finder: ascend_by_name_of_field. 它是searchlogic的一项功能,您可以将其用作动态查找器: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. 您假设@order_by不为空,并且函数extract_order已经运行。 Another thing, actions that has no user interaction shouldnt be accessible. 另一件事,没有用户交互的动作不应该被访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM