Extract component to render "see more" link
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
3
app/components/shared/see_more_link_component.html.erb
Normal file
3
app/components/shared/see_more_link_component.html.erb
Normal file
@@ -0,0 +1,3 @@
|
||||
<% if count_out_of_limit > 0 %>
|
||||
<%= link_to text, url, class: html_class, title: title %>
|
||||
<% end %>
|
||||
37
app/components/shared/see_more_link_component.rb
Normal file
37
app/components/shared/see_more_link_component.rb
Normal 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
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user