Fix #3430: Accept the search[id] param in all controllers.

* Allow every controller to take the `search[id]` param.

* Parse the `search[id]` param the same way that the `id:<N>` metatag is
  parsed. So `search[id]=1,2,3`, `search[id]=<42`, `search[id]=1..10`, for
  example, are all accepted.
This commit is contained in:
evazion
2017-12-17 16:58:34 -06:00
parent 49577e3fac
commit 0ca726802f
28 changed files with 72 additions and 58 deletions

View File

@@ -1,6 +1,29 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
concerning :SearchMethods do
class_methods do
# range: "5", ">5", "<5", ">=5", "<=5", "5..10", "5,6,7"
def attribute_matches(attribute, range)
return all if range.blank?
column = column_for_attribute(attribute)
qualified_column = "#{table_name}.#{column.name}"
parsed_range = Tag.parse_helper(range, :integer)
PostQueryBuilder.new(nil).add_range_relation(parsed_range, qualified_column, self)
end
def search(params = {})
params ||= {}
q = all
q = q.attribute_matches(:id, params[:id])
q
end
end
end
module ApiMethods
extend ActiveSupport::Concern