Update tag search and suggest (#15810)

* add needed fields to tag search

* specs

* add badge to tags suggest response (#15840)

* Add badge to tags suggest response

* Update tags_spec.rb

Co-authored-by: Jeremy Friesen <jeremy.n.friesen@gmail.com>

* limit tag search badge to only badge_image

* only unpack badge image if badge exists

Co-authored-by: Dwight Scott <dwight@forem.com>
Co-authored-by: Jeremy Friesen <jeremy.n.friesen@gmail.com>
This commit is contained in:
Suzanne Aitchison 2022-01-05 07:51:17 +00:00 committed by GitHub
parent f67fbb1a7e
commit 54eaee899e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 6 deletions

View file

@ -4,7 +4,8 @@ class TagsController < ApplicationController
after_action :verify_authorized
ATTRIBUTES_FOR_SERIALIZATION = %i[id name bg_color_hex text_color_hex].freeze
INDEX_API_ATTRIBUTES = %i[name rules_html].freeze
INDEX_API_ATTRIBUTES = %i[name rules_html short_summary bg_color_hex badge_id].freeze
TAGS_ALLOWED_PARAMS = %i[
wiki_body_markdown
rules_markdown
@ -55,7 +56,7 @@ class TagsController < ApplicationController
def suggest
skip_authorization
tags = Tag.supported.order(hotness_score: :desc).limit(100).select(INDEX_API_ATTRIBUTES)
render json: tags.as_json(only: INDEX_API_ATTRIBUTES)
render json: tags, only: INDEX_API_ATTRIBUTES, include: [badge: { only: [:badge_image] }]
end
private

View file

@ -1,5 +1,10 @@
module Search
class TagSerializer < ApplicationSerializer
attributes :id, :name, :hotness_score, :supported, :short_summary, :rules_html
attributes :id, :name, :hotness_score, :supported, :short_summary, :rules_html, :bg_color_hex
attribute :badge do |tag|
if tag.badge
{ badge_image: tag.badge.badge_image }
end
end
end
end

View file

@ -1,6 +1,6 @@
module Search
class Tag
ATTRIBUTES = %i[id name hotness_score rules_html supported short_summary].freeze
ATTRIBUTES = %i[id name hotness_score rules_html supported short_summary bg_color_hex badge_id].freeze
def self.search_documents(term)
results = ::Tag.search_by_name(term).supported.reorder(hotness_score: :desc).select(*ATTRIBUTES)

View file

@ -21,7 +21,8 @@ RSpec.describe "Tags", type: :request, proper_status: true do
describe "GET /tags/suggest" do
it "returns a JSON representation of the top tags", :aggregate_failures do
tag = create(:tag)
badge = create(:badge)
tag = create(:tag, badge: badge)
get suggest_tags_path
@ -30,6 +31,10 @@ RSpec.describe "Tags", type: :request, proper_status: true do
response_tag = JSON.parse(response.body).first
expect(response_tag["name"]).to eq(tag.name)
expect(response_tag).to have_key("rules_html")
expect(response_tag).to have_key("short_summary")
expect(response_tag).to have_key("bg_color_hex")
expect(response_tag).to have_key("badge")
expect(response_tag["badge"]).to have_key("badge_image")
end
end

View file

@ -14,7 +14,7 @@ RSpec.describe Search::Tag, type: :service do
result = described_class.search_documents(tag.name)
expect(result.first.keys).to match_array(
%i[id name hotness_score rules_html supported short_summary],
%i[id name hotness_score rules_html supported short_summary badge bg_color_hex],
)
end