diff --git a/app/components/sdg/targets/plain_tag_list_component.html.erb b/app/components/sdg/targets/plain_tag_list_component.html.erb new file mode 100644 index 000000000..ab0c51246 --- /dev/null +++ b/app/components/sdg/targets/plain_tag_list_component.html.erb @@ -0,0 +1,7 @@ +<% if tags.any? %> + +<% end %> diff --git a/app/components/sdg/targets/plain_tag_list_component.rb b/app/components/sdg/targets/plain_tag_list_component.rb new file mode 100644 index 000000000..b24792cb2 --- /dev/null +++ b/app/components/sdg/targets/plain_tag_list_component.rb @@ -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 diff --git a/spec/components/sdg/targets/plain_tag_list_component_spec.rb b/spec/components/sdg/targets/plain_tag_list_component_spec.rb new file mode 100644 index 000000000..b80cd6849 --- /dev/null +++ b/spec/components/sdg/targets/plain_tag_list_component_spec.rb @@ -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