Create SDG target component to render plain tags
This commit is contained in:
committed by
taitus
parent
54843c1e53
commit
cc2ce38d13
@@ -0,0 +1,7 @@
|
||||
<% if tags.any? %>
|
||||
<ul class="sdg-target-tag-list">
|
||||
<% tags.each do |tag| %>
|
||||
<li><%= tag %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% end %>
|
||||
37
app/components/sdg/targets/plain_tag_list_component.rb
Normal file
37
app/components/sdg/targets/plain_tag_list_component.rb
Normal 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
|
||||
64
spec/components/sdg/targets/plain_tag_list_component_spec.rb
Normal file
64
spec/components/sdg/targets/plain_tag_list_component_spec.rb
Normal 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
|
||||
Reference in New Issue
Block a user