Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion app/controllers/api/subcollections/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,26 @@ def unassign_tags_resource(type, id, data)
end

def tags_query_resource(object)
object ? object.tags.where(Tag.arel_table[:name].matches "#{Api::BaseController::TAG_NAMESPACE}%") : {}
return {} unless object

tags = object.tags.where(Tag.arel_table[:name].matches("#{Api::BaseController::TAG_NAMESPACE}%"))

tag_attrs = params['tag_attributes'].to_s.split(',').map(&:strip).reject(&:blank?)
extra_attrs = tag_attrs - %w[id name href]
return tags if extra_attrs.blank?

tags.includes(:classification).map do |tag|
tag.as_json.tap do |hash|
hash['id_value'] = tag.id
extra_attrs.each do |attr|
if attr == "description"
hash[attr] = tag.classification&.description
elsif tag.respond_to?(attr)
hash[attr] = tag.public_send(attr)
end
end
end
end
end

def tags_assign_resource(object, _type, id = nil, data = nil)
Expand Down
56 changes: 56 additions & 0 deletions spec/requests/categories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,62 @@
expect(response).to have_http_status(:ok)
end

context "tag_attributes parameter" do
let!(:classification) { FactoryBot.create(:classification_tag, :description => "New York") }
let!(:category) { FactoryBot.create(:category, :children => [classification]) }

before { api_basic_authorize collection_action_identifier(:categories, :read, :get) }

it "returns tag description when tag_attributes includes description" do
get api_categories_url, :params => {
:expand => "resources,tags",
:attributes => "id,name,description",
:tag_attributes => "id,name,description"
}

expect(response).to have_http_status(:ok)
tags = response.parsed_body["resources"]
.detect { |r| r["id"] == category.id.to_s }
.fetch("tags", [])

expect(tags).not_to be_empty
tag = tags.detect { |t| t["name"].end_with?("/#{classification.name}") }

expect(tag).to be_present
expect(tag["description"]).to eq("New York")
expect(tag["id_value"]).to be_present
end

it "does not return description when tag_attributes is not specified" do
get api_categories_url, :params => {
:expand => "resources,tags",
:attributes => "id,name,description"
}

expect(response).to have_http_status(:ok)
tags = response.parsed_body["resources"]
.detect { |r| r["id"] == category.id.to_s }
.fetch("tags", [])

expect(tags.first).not_to have_key("description")
end

it "does not return categorization when tag_attributes only includes description" do
get api_categories_url, :params => {
:expand => "resources,tags",
:attributes => "id,name,description",
:tag_attributes => "id,name,description"
}

expect(response).to have_http_status(:ok)
tags = response.parsed_body["resources"]
.detect { |r| r["id"] == category.id.to_s }
.fetch("tags", [])

expect(tags.first).not_to have_key("categorization")
end
end

context "with an appropriate role" do
it "can create a category" do
api_basic_authorize collection_action_identifier(:categories, :create)
Expand Down
Loading