Fix searching models by id/created_at/updated_at attributes.

This commit is contained in:
evazion
2019-09-02 13:38:00 -05:00
parent 64eb6dbb2a
commit 3e85ce8110
2 changed files with 13 additions and 6 deletions

View File

@@ -174,12 +174,8 @@ class ApplicationRecord < ActiveRecord::Base
def search(params = {})
params ||= {}
q = all
q = search_attribute(:id, params)
q = search_attribute(:created_at, params) if attribute_names.include?("created_at")
q = search_attribute(:updated_at, params) if attribute_names.include?("updated_at")
q
default_attributes = (attribute_names & %w[id created_at updated_at])
search_attributes(params, *default_attributes)
end
end
end

View File

@@ -181,4 +181,15 @@ class ApplicationControllerTest < ActionDispatch::IntegrationTest
end
end
end
context "all index methods" do
should "support searching by the id attribute" do
tags = create_list(:tag, 2, post_count: 42)
get tags_path(format: :json), params: { search: { id: tags.first.id } }
assert_response :success
assert_equal(1, response.parsed_body.size)
assert_equal(tags.first.id, response.parsed_body.first.fetch("id"))
end
end
end