diff --git a/app/components/concerns/sdg/tag_list.rb b/app/components/concerns/sdg/tag_list.rb index 355005408..b936853d5 100644 --- a/app/components/concerns/sdg/tag_list.rb +++ b/app/components/concerns/sdg/tag_list.rb @@ -12,15 +12,8 @@ module SDG::TagList process.enabled? end - def see_more_link(collection) - count = count_out_of_limit(collection) - - if count > 0 - link_to "#{count}+", - polymorphic_path(record), - class: "more-#{i18n_namespace}", - title: t("sdg.#{i18n_namespace}.filter.more", count: count) - end + def see_more_link(association_name) + render Shared::SeeMoreLinkComponent.new(record, association_name, limit: limit) end def filter_text(goal_or_target) @@ -33,12 +26,6 @@ module SDG::TagList polymorphic_path(model, advanced_search: advanced_search) end - def count_out_of_limit(collection) - return 0 unless limit - - collection.size - limit - end - def process @process ||= SDG::ProcessEnabled.new(record_or_name) end diff --git a/app/components/sdg/goals/plain_tag_list_component.rb b/app/components/sdg/goals/plain_tag_list_component.rb index bb1bb6d0a..3d9a701e0 100644 --- a/app/components/sdg/goals/plain_tag_list_component.rb +++ b/app/components/sdg/goals/plain_tag_list_component.rb @@ -8,7 +8,7 @@ class SDG::Goals::PlainTagListComponent < ApplicationComponent end def tags - [*goal_tags, see_more_link(goals)].compact + [*goal_tags, see_more_link(:sdg_goals)].select(&:present?) end def goal_tags diff --git a/app/components/sdg/goals/tag_list_component.rb b/app/components/sdg/goals/tag_list_component.rb index 391e07f55..bc23ca456 100644 --- a/app/components/sdg/goals/tag_list_component.rb +++ b/app/components/sdg/goals/tag_list_component.rb @@ -8,7 +8,7 @@ class SDG::Goals::TagListComponent < ApplicationComponent end def links - [*goal_links, see_more_link(goals)] + [*goal_links, see_more_link(:sdg_goals)] end def goal_links diff --git a/app/components/sdg/targets/plain_tag_list_component.rb b/app/components/sdg/targets/plain_tag_list_component.rb index 73d73ed2a..2e1ba705b 100644 --- a/app/components/sdg/targets/plain_tag_list_component.rb +++ b/app/components/sdg/targets/plain_tag_list_component.rb @@ -8,7 +8,7 @@ class SDG::Targets::PlainTagListComponent < ApplicationComponent end def tags - [*target_tags, see_more_link(targets)].compact + [*target_tags, see_more_link(:sdg_targets)].select(&:present?) end def target_tags diff --git a/app/components/sdg/targets/tag_list_component.rb b/app/components/sdg/targets/tag_list_component.rb index cb79f80b6..7d340b2aa 100644 --- a/app/components/sdg/targets/tag_list_component.rb +++ b/app/components/sdg/targets/tag_list_component.rb @@ -8,7 +8,7 @@ class SDG::Targets::TagListComponent < ApplicationComponent end def links - [*target_links, see_more_link(targets)] + [*target_links, see_more_link(:sdg_targets)] end def target_links diff --git a/app/components/shared/see_more_link_component.html.erb b/app/components/shared/see_more_link_component.html.erb new file mode 100644 index 000000000..dd78b17a5 --- /dev/null +++ b/app/components/shared/see_more_link_component.html.erb @@ -0,0 +1,3 @@ +<% if count_out_of_limit > 0 %> + <%= link_to text, url, class: html_class, title: title %> +<% end %> diff --git a/app/components/shared/see_more_link_component.rb b/app/components/shared/see_more_link_component.rb new file mode 100644 index 000000000..339f66384 --- /dev/null +++ b/app/components/shared/see_more_link_component.rb @@ -0,0 +1,37 @@ +class Shared::SeeMoreLinkComponent < ApplicationComponent + attr_reader :record, :association_name, :limit + + def initialize(record, association_name, limit: nil) + @record = record + @association_name = association_name + @limit = limit + end + + private + + def text + "#{count_out_of_limit}+" + end + + def url + polymorphic_path(record) + end + + def title + t("#{i18n_namespace}.filter.more", count: count_out_of_limit) + end + + def count_out_of_limit + return 0 unless limit + + record.send(association_name).size - limit + end + + def i18n_namespace + association_name.to_s.tr("_", ".") + end + + def html_class + "more-#{i18n_namespace.split(".").last}" + end +end diff --git a/app/components/shared/tag_list_component.rb b/app/components/shared/tag_list_component.rb index 0c4bf4f91..79510718e 100644 --- a/app/components/shared/tag_list_component.rb +++ b/app/components/shared/tag_list_component.rb @@ -23,10 +23,7 @@ class Shared::TagListComponent < ApplicationComponent end def see_more_link - if taggable.tags_count_out_of_limit(limit) > 0 - link_to "#{taggable.tags_count_out_of_limit(limit)}+", - polymorphic_path(taggable) - end + render Shared::SeeMoreLinkComponent.new(taggable, :tags, limit: limit) end def taggables_path(taggable, tag_name) diff --git a/app/models/concerns/taggable.rb b/app/models/concerns/taggable.rb index c840dd97e..73889ae81 100644 --- a/app/models/concerns/taggable.rb +++ b/app/models/concerns/taggable.rb @@ -12,13 +12,6 @@ module Taggable tags.sort { |a, b| b.taggings_count <=> a.taggings_count }[0, limit] end - def tags_count_out_of_limit(limit = nil) - return 0 unless limit - - count = tags.size - limit - count < 0 ? 0 : count - end - def max_number_of_tags errors.add(:tag_list, :less_than_or_equal_to, count: 6) if tag_list.count > 6 end diff --git a/config/i18n-tasks.yml b/config/i18n-tasks.yml index 37a8abda1..dd76cacec 100644 --- a/config/i18n-tasks.yml +++ b/config/i18n-tasks.yml @@ -221,6 +221,7 @@ ignore_unused: - "sdg.goals.goal_*" - "sdg.*.filter.more.*" - "sdg_management.relations.index.filter*" + - "tags.filter.more.*" #### ## Exclude these keys from the `i18n-tasks eq-base" report: # ignore_eq_base: diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index a150efd5a..3985effb9 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -973,3 +973,8 @@ en: create: enqueue_remote_translation: Translations have been correctly requested. button: Translate page + tags: + filter: + more: + one: "One more tag" + other: "%{count} more tags" diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index c9e011657..9b0e15134 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -973,3 +973,8 @@ es: create: enqueue_remote_translation: Se han solicitado correctamente las traducciones. button: Traducir página + tags: + filter: + more: + one: "Una etiqueta más" + other: "%{count} etiquetas más"