Extract component to render "see more" link

This commit is contained in:
Javi Martín
2021-02-02 01:43:21 +01:00
parent 6112016773
commit ad56b01a9a
12 changed files with 58 additions and 30 deletions

View File

@@ -12,15 +12,8 @@ module SDG::TagList
process.enabled? process.enabled?
end end
def see_more_link(collection) def see_more_link(association_name)
count = count_out_of_limit(collection) render Shared::SeeMoreLinkComponent.new(record, association_name, limit: limit)
if count > 0
link_to "#{count}+",
polymorphic_path(record),
class: "more-#{i18n_namespace}",
title: t("sdg.#{i18n_namespace}.filter.more", count: count)
end
end end
def filter_text(goal_or_target) def filter_text(goal_or_target)
@@ -33,12 +26,6 @@ module SDG::TagList
polymorphic_path(model, advanced_search: advanced_search) polymorphic_path(model, advanced_search: advanced_search)
end end
def count_out_of_limit(collection)
return 0 unless limit
collection.size - limit
end
def process def process
@process ||= SDG::ProcessEnabled.new(record_or_name) @process ||= SDG::ProcessEnabled.new(record_or_name)
end end

View File

@@ -8,7 +8,7 @@ class SDG::Goals::PlainTagListComponent < ApplicationComponent
end end
def tags def tags
[*goal_tags, see_more_link(goals)].compact [*goal_tags, see_more_link(:sdg_goals)].select(&:present?)
end end
def goal_tags def goal_tags

View File

@@ -8,7 +8,7 @@ class SDG::Goals::TagListComponent < ApplicationComponent
end end
def links def links
[*goal_links, see_more_link(goals)] [*goal_links, see_more_link(:sdg_goals)]
end end
def goal_links def goal_links

View File

@@ -8,7 +8,7 @@ class SDG::Targets::PlainTagListComponent < ApplicationComponent
end end
def tags def tags
[*target_tags, see_more_link(targets)].compact [*target_tags, see_more_link(:sdg_targets)].select(&:present?)
end end
def target_tags def target_tags

View File

@@ -8,7 +8,7 @@ class SDG::Targets::TagListComponent < ApplicationComponent
end end
def links def links
[*target_links, see_more_link(targets)] [*target_links, see_more_link(:sdg_targets)]
end end
def target_links def target_links

View File

@@ -0,0 +1,3 @@
<% if count_out_of_limit > 0 %>
<%= link_to text, url, class: html_class, title: title %>
<% end %>

View File

@@ -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

View File

@@ -23,10 +23,7 @@ class Shared::TagListComponent < ApplicationComponent
end end
def see_more_link def see_more_link
if taggable.tags_count_out_of_limit(limit) > 0 render Shared::SeeMoreLinkComponent.new(taggable, :tags, limit: limit)
link_to "#{taggable.tags_count_out_of_limit(limit)}+",
polymorphic_path(taggable)
end
end end
def taggables_path(taggable, tag_name) def taggables_path(taggable, tag_name)

View File

@@ -12,13 +12,6 @@ module Taggable
tags.sort { |a, b| b.taggings_count <=> a.taggings_count }[0, limit] tags.sort { |a, b| b.taggings_count <=> a.taggings_count }[0, limit]
end 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 def max_number_of_tags
errors.add(:tag_list, :less_than_or_equal_to, count: 6) if tag_list.count > 6 errors.add(:tag_list, :less_than_or_equal_to, count: 6) if tag_list.count > 6
end end

View File

@@ -221,6 +221,7 @@ ignore_unused:
- "sdg.goals.goal_*" - "sdg.goals.goal_*"
- "sdg.*.filter.more.*" - "sdg.*.filter.more.*"
- "sdg_management.relations.index.filter*" - "sdg_management.relations.index.filter*"
- "tags.filter.more.*"
#### ####
## Exclude these keys from the `i18n-tasks eq-base" report: ## Exclude these keys from the `i18n-tasks eq-base" report:
# ignore_eq_base: # ignore_eq_base:

View File

@@ -973,3 +973,8 @@ en:
create: create:
enqueue_remote_translation: Translations have been correctly requested. enqueue_remote_translation: Translations have been correctly requested.
button: Translate page button: Translate page
tags:
filter:
more:
one: "One more tag"
other: "%{count} more tags"

View File

@@ -973,3 +973,8 @@ es:
create: create:
enqueue_remote_translation: Se han solicitado correctamente las traducciones. enqueue_remote_translation: Se han solicitado correctamente las traducciones.
button: Traducir página button: Traducir página
tags:
filter:
more:
one: "Una etiqueta más"
other: "%{count} etiquetas más"