Create SDG target component to render plain tags

This commit is contained in:
Senén Rodero Rodríguez
2021-01-28 13:22:56 +01:00
committed by taitus
parent 54843c1e53
commit cc2ce38d13
3 changed files with 108 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
<% if tags.any? %>
<ul class="sdg-target-tag-list">
<% tags.each do |tag| %>
<li><%= tag %></li>
<% end %>
</ul>
<% end %>

View File

@@ -0,0 +1,37 @@
class SDG::Targets::PlainTagListComponent < ApplicationComponent
include SDG::TagList
private
def record
record_or_name
end
def tags
[*target_tags, see_more_link].compact
end
def see_more_link
options = super(targets)
link_to(*options) if options.present?
end
def target_tags
targets.sort[0..(limit.to_i - 1)].map do |target|
tag.span(text(target), data: { code: target.code })
end
end
def targets
record.sdg_targets
end
def text(target)
"#{SDG::Target.model_name.human} #{target.code}"
end
def i18n_namespace
"targets"
end
end

View File

@@ -0,0 +1,64 @@
require "rails_helper"
describe SDG::Targets::PlainTagListComponent, type: :component do
let(:debate) do
create(:debate,
sdg_targets: [SDG::Target[1.1], SDG::Target[3.2], create(:sdg_local_target, code: "3.2.1")]
)
end
let(:component) { SDG::Targets::PlainTagListComponent.new(debate) }
before do
Setting["feature.sdg"] = true
Setting["sdg.process.debates"] = true
end
it "does not render when the feature is disabled" do
Setting["feature.sdg"] = false
render_inline component
expect(page).not_to have_css "li"
end
it "does not render when the SDG process feature is disabled" do
Setting["sdg.process.debates"] = false
render_inline component
expect(page).not_to have_css "li"
end
it "renders a list of targets" do
render_inline component
expect(page).to have_css "li", count: 3
end
it "renders tags for each target" do
render_inline component
expect(page).to have_css "li span[data-code='1.1']", text: "target 1.1"
expect(page).to have_css "li span[data-code='3.2']", text: "target 3.2"
expect(page).to have_css "li span[data-code='3.2.1']", text: "target 3.2.1"
end
it "orders targets by code" do
render_inline component
expect(page.first("li").text).to eq "target 1.1"
expect(page.all("li").last.text).to eq "target 3.2.1"
end
it "renders a link for more targets when out of limit" do
component = SDG::Targets::PlainTagListComponent.new(debate, limit: 1)
render_inline component
expect(page).to have_css "li", text: "target 1.1"
expect(page).to have_selector "a", count: 1
expect(page).to have_link "2+",
title: "2 more targets",
href: "/debates/#{debate.to_param}"
end
end