From 54843c1e5373d62ca64ddfe307905fa8183bf12f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Thu, 28 Jan 2021 11:53:09 +0100 Subject: [PATCH] Create SDG goal component to render plain tags --- .../goals/plain_tag_list_component.html.erb | 7 +++ .../sdg/goals/plain_tag_list_component.rb | 33 +++++++++++ .../goals/plain_tag_list_component_spec.rb | 56 +++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 app/components/sdg/goals/plain_tag_list_component.html.erb create mode 100644 app/components/sdg/goals/plain_tag_list_component.rb create mode 100644 spec/components/sdg/goals/plain_tag_list_component_spec.rb diff --git a/app/components/sdg/goals/plain_tag_list_component.html.erb b/app/components/sdg/goals/plain_tag_list_component.html.erb new file mode 100644 index 000000000..b16483315 --- /dev/null +++ b/app/components/sdg/goals/plain_tag_list_component.html.erb @@ -0,0 +1,7 @@ +<% if tags.any? %> + +<% end %> diff --git a/app/components/sdg/goals/plain_tag_list_component.rb b/app/components/sdg/goals/plain_tag_list_component.rb new file mode 100644 index 000000000..8b60b692b --- /dev/null +++ b/app/components/sdg/goals/plain_tag_list_component.rb @@ -0,0 +1,33 @@ +class SDG::Goals::PlainTagListComponent < ApplicationComponent + include SDG::TagList + + private + + def record + record_or_name + end + + def tags + [*goal_tags, see_more_link].compact + end + + def see_more_link + options = super(goals) + + link_to(*options) if options.present? + end + + def goal_tags + goals.order(:code).limit(limit).map do |goal| + render SDG::Goals::IconComponent.new(goal) + end + end + + def goals + record.sdg_goals + end + + def i18n_namespace + "goals" + end +end diff --git a/spec/components/sdg/goals/plain_tag_list_component_spec.rb b/spec/components/sdg/goals/plain_tag_list_component_spec.rb new file mode 100644 index 000000000..0106848d0 --- /dev/null +++ b/spec/components/sdg/goals/plain_tag_list_component_spec.rb @@ -0,0 +1,56 @@ +require "rails_helper" + +describe SDG::Goals::PlainTagListComponent, type: :component do + let(:debate) { create(:debate, sdg_goals: [SDG::Goal[1], SDG::Goal[3]]) } + let(:component) { SDG::Goals::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 goals" do + render_inline component + + expect(page).to have_css "li", count: 2 + end + + it "renders icons for each goal" do + render_inline component + + expect(page).to have_selector ".sdg-goal-icon", count: 2 + end + + it "orders goals by code" do + render_inline component + + expect(page.first(".sdg-goal-icon")[:alt]).to eq "1. No Poverty" + end + + it "renders a link for more goals when out of limit" do + component = SDG::Goals::PlainTagListComponent.new(debate, limit: 1) + + render_inline component + + expect(page).to have_selector ".sdg-goal-icon" + expect(page).to have_link "1+", + title: "One more goal", + href: "/debates/#{debate.to_param}" + end +end