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:
@@ -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
|
||||
|
||||
|
||||
@@ -426,7 +426,7 @@ class Artist < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
params = {} if params.blank?
|
||||
|
||||
case params[:name]
|
||||
@@ -496,10 +496,6 @@ class Artist < ApplicationRecord
|
||||
q = q.unbanned
|
||||
end
|
||||
|
||||
if params[:id].present?
|
||||
q = q.where("artists.id in (?)", params[:id].split(",").map(&:to_i))
|
||||
end
|
||||
|
||||
if params[:creator_name].present?
|
||||
q = q.where("artists.creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name].tr(" ", "_").mb_chars.downcase)
|
||||
end
|
||||
|
||||
@@ -32,7 +32,7 @@ class ArtistCommentary < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
params = {} if params.blank?
|
||||
|
||||
if params[:text_matches].present?
|
||||
|
||||
@@ -14,7 +14,7 @@ class ArtistVersion < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
return q if params.blank?
|
||||
|
||||
if params[:name].present?
|
||||
|
||||
@@ -23,11 +23,7 @@ class BulkUpdateRequest < ApplicationRecord
|
||||
|
||||
module SearchMethods
|
||||
def search(params = {})
|
||||
q = where("true")
|
||||
|
||||
if params[:id].present?
|
||||
q = q.where("id in (?)", params[:id].split(",").map(&:to_i))
|
||||
end
|
||||
q = super
|
||||
|
||||
if params[:user_name].present?
|
||||
q = q.where(user_id: User.name_to_id(params[:user_name]))
|
||||
|
||||
@@ -92,16 +92,12 @@ class Comment < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
|
||||
if params[:body_matches].present?
|
||||
q = q.body_matches(params[:body_matches])
|
||||
end
|
||||
|
||||
if params[:id].present?
|
||||
q = q.where("id in (?)", params[:id].split(",").map(&:to_i))
|
||||
end
|
||||
|
||||
if params[:post_id].present?
|
||||
q = q.where("post_id in (?)", params[:post_id].split(",").map(&:to_i))
|
||||
end
|
||||
|
||||
@@ -186,7 +186,7 @@ class Dmail < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
return q if params.blank?
|
||||
|
||||
if params[:title_matches].present?
|
||||
|
||||
@@ -34,7 +34,7 @@ class FavoriteGroup < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
params = {} if params.blank?
|
||||
|
||||
if params[:creator_id].present?
|
||||
|
||||
@@ -65,7 +65,8 @@ class ForumPost < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = permitted
|
||||
q = super
|
||||
q = q.permitted
|
||||
return q if params.blank?
|
||||
|
||||
if params[:creator_id].present?
|
||||
|
||||
@@ -73,11 +73,8 @@ class ForumTopic < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = permitted
|
||||
|
||||
if params[:id].present?
|
||||
q = q.where(id: params[:id].split(",").map(&:to_i))
|
||||
end
|
||||
q = super
|
||||
q = q.permitted
|
||||
|
||||
if params[:mod_only].present?
|
||||
q = q.where("min_level >= ?", MIN_LEVELS[:Moderator])
|
||||
|
||||
@@ -42,7 +42,7 @@ class Note < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
return q if params.blank?
|
||||
|
||||
if params[:body_matches].present?
|
||||
|
||||
@@ -50,17 +50,13 @@ class Pool < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
params = {} if params.blank?
|
||||
|
||||
if params[:name_matches].present?
|
||||
q = q.name_matches(params[:name_matches])
|
||||
end
|
||||
|
||||
if params[:id].present?
|
||||
q = q.where("pools.id in (?)", params[:id].split(","))
|
||||
end
|
||||
|
||||
if params[:description_matches].present?
|
||||
q = q.where("lower(pools.description) like ? escape E'\\\\'", "%" + params[:description_matches].mb_chars.downcase.to_escaped_for_sql_like + "%")
|
||||
end
|
||||
|
||||
@@ -15,7 +15,7 @@ class PoolArchive < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
return q if params.blank?
|
||||
|
||||
if params[:updater_id].present?
|
||||
|
||||
@@ -13,7 +13,7 @@ class PoolVersion < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
return q if params.blank?
|
||||
|
||||
if params[:updater_id].present?
|
||||
|
||||
@@ -44,7 +44,8 @@ class PostAppeal < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = order("post_appeals.id desc")
|
||||
q = super
|
||||
q = q.order("post_appeals.id desc")
|
||||
return q if params.blank?
|
||||
|
||||
if params[:reason_matches].present?
|
||||
|
||||
@@ -26,7 +26,7 @@ class PostArchive < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
params = {} if params.blank?
|
||||
|
||||
if params[:updater_name].present?
|
||||
|
||||
@@ -66,7 +66,8 @@ class PostFlag < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = order("post_flags.id desc")
|
||||
q = super
|
||||
q = q.order("post_flags.id desc")
|
||||
return q if params.blank?
|
||||
|
||||
if params[:reason_matches].present?
|
||||
|
||||
@@ -89,7 +89,7 @@ class PostReplacement < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params = {})
|
||||
q = all
|
||||
q = super
|
||||
|
||||
if params[:creator_id].present?
|
||||
q = q.where(creator_id: params[:creator_id].split(",").map(&:to_i))
|
||||
@@ -99,10 +99,6 @@ class PostReplacement < ApplicationRecord
|
||||
q = q.where(creator_id: User.name_to_id(params[:creator_name]))
|
||||
end
|
||||
|
||||
if params[:id].present?
|
||||
q = q.where(id: params[:id].split(",").map(&:to_i))
|
||||
end
|
||||
|
||||
if params[:post_id].present?
|
||||
q = q.where(post_id: params[:post_id].split(",").map(&:to_i))
|
||||
end
|
||||
|
||||
@@ -14,7 +14,7 @@ class PostVersion < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
params = {} if params.blank?
|
||||
|
||||
if params[:updater_name].present?
|
||||
|
||||
@@ -838,7 +838,7 @@ class Tag < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
params = {} if params.blank?
|
||||
|
||||
if params[:fuzzy_name_matches].present?
|
||||
|
||||
@@ -73,11 +73,7 @@ class TagRelationship < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = all
|
||||
|
||||
if params[:id].present?
|
||||
q = q.where(id: params[:id].split(",").map(&:to_i))
|
||||
end
|
||||
q = super
|
||||
|
||||
if params[:name_matches].present?
|
||||
q = q.name_matches(params[:name_matches])
|
||||
|
||||
@@ -39,7 +39,7 @@ class TagSubscription < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
params = {} if params.blank?
|
||||
|
||||
if params[:creator_id]
|
||||
|
||||
@@ -495,7 +495,7 @@ class Upload < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
return q if params.blank?
|
||||
|
||||
if params[:uploader_id].present?
|
||||
|
||||
@@ -813,7 +813,7 @@ class User < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
return q if params.blank?
|
||||
|
||||
if params[:name].present?
|
||||
@@ -836,10 +836,6 @@ class User < ApplicationRecord
|
||||
q = q.where("level = ?", params[:level].to_i)
|
||||
end
|
||||
|
||||
if params[:id].present?
|
||||
q = q.where("id in (?)", params[:id].split(",").map(&:to_i))
|
||||
end
|
||||
|
||||
bitprefs_length = BOOLEAN_ATTRIBUTES.length
|
||||
bitprefs_include = nil
|
||||
bitprefs_exclude = nil
|
||||
|
||||
@@ -44,7 +44,7 @@ class UserFeedback < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
return q if params.blank?
|
||||
|
||||
if params[:user_id].present?
|
||||
|
||||
@@ -60,7 +60,7 @@ class WikiPage < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params = {})
|
||||
q = where("true")
|
||||
q = super
|
||||
params = {} if params.blank?
|
||||
|
||||
if params[:title].present?
|
||||
|
||||
@@ -11,7 +11,7 @@ class WikiPageVersion < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = where("true")
|
||||
q = super
|
||||
return q if params.blank?
|
||||
|
||||
if params[:updater_id].present?
|
||||
|
||||
19
test/unit/application_record.rb
Normal file
19
test/unit/application_record.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ApplicationRecordTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@tags = FactoryGirl.create_list(:tag, 3, post_count: 1)
|
||||
end
|
||||
|
||||
context "ApplicationRecord#search" do
|
||||
should "support the id param" do
|
||||
assert_equal([@tags.first], Tag.search(id: @tags.first.id))
|
||||
end
|
||||
|
||||
should "support ranges in the id param" do
|
||||
assert_equal(@tags.reverse, Tag.search(id: ">=1"))
|
||||
assert_equal(@tags.reverse, Tag.search(id: "#{@tags[0].id}..#{@tags[2].id}"))
|
||||
assert_equal(@tags.reverse, Tag.search(id: @tags.map(&:id).join(",")))
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user