ruby on Rails、searchlogic 和重构

发布于 2024-08-31 13:35:06 字数 886 浏览 7 评论 0原文

我不太熟悉rails 的searchlogic 插件(我确实查看了railscast,但对于下面的具体代码没有帮助)。谁能简单描述一下下面三种方法是如何使用的?感谢您的任何回复。

 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话墙 2024-09-07 13:35:06

apply_filter 方法没有被调用。

find_resources 方法正在使用 @order_by 中的内容(尽管未调用方法 extract order),

因此资源模型中的搜索是使用存储在变量搜索条件中的参数(可能是用户输入)并使用 @order_by 来完成的说出它必须使用的顺序。

请注意,您的应用程序正在获取一些参数并更改“。”到“_”并获取子字符串(1..-1,实际上删除第一个字符,并将其用作范围搜索的参数(ascend_by_|descend_by_)。
它是 searchlogic 的一项功能,您可以将其用作动态查找器:ascend_by_name_of_field。

IMO,它看起来很乱。您假设 @order_by 不为空并且该函数
extract_order 已经运行。另一件事是,没有用户交互的操作不应该是可访问的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文