Split goal tag list component

This way we'll be able to add targets as well. Besides, having two
classes in the CSS selector will allows to overwrite styles such as
`.debate-show ul li`.

On the other hand, we need to add a stylesheet for the filters as well.
This commit is contained in:
Javi Martín
2021-01-23 15:51:23 +01:00
parent a7727bfcfe
commit 7b3fcf6cb5
8 changed files with 89 additions and 53 deletions

View File

@@ -0,0 +1,6 @@
.sdg-goal-filter-links {
.sdg-goal-tag-list {
@extend %sdg-goal-list;
}
}

View File

@@ -1,7 +0,0 @@
.sdg-goal-tag-list {
@extend %sdg-goal-list;
.more-goals {
@extend %tag;
}
}

View File

@@ -0,0 +1,10 @@
.sdg-tag-list {
.sdg-goal-tag-list {
@extend %sdg-goal-list;
.more-goals {
@extend %tag;
}
}
}

View File

@@ -0,0 +1,50 @@
module SDG::TagList
extend ActiveSupport::Concern
attr_reader :record_or_name, :limit
delegate :link_list, to: :helpers
def initialize(record_or_name, limit: nil)
@record_or_name = record_or_name
@limit = limit
end
def render?
process.enabled?
end
def see_more_link(collection)
count = count_out_of_limit(collection)
if count > 0
[
"#{count}+",
polymorphic_path(record),
class: "more-#{i18n_namespace}", title: t("sdg.#{i18n_namespace}.filter.more", count: count)
]
end
end
def filter_text(goal_or_target)
t("sdg.#{i18n_namespace}.filter.link",
resources: model.model_name.human(count: :other),
code: goal_or_target.code)
end
def index_by(advanced_search)
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
def model
process.name.constantize
end
end

View File

@@ -1,24 +1,18 @@
class SDG::Goals::TagListComponent < ApplicationComponent
attr_reader :record_or_name, :limit
delegate :link_list, to: :helpers
def initialize(record_or_name, limit: nil)
@record_or_name = record_or_name
@limit = limit
end
def render?
process.enabled?
end
include SDG::TagList
private
def record
record_or_name if record_or_name.respond_to?(:sdg_goals)
end
def links
[*goal_links, see_more_link]
[*goal_links, see_more_link(goals)]
end
def goal_links
goals.limit(limit).map do |goal|
goals.order(:code).limit(limit).map do |goal|
[
render(SDG::Goals::IconComponent.new(goal)),
index_by_goal(goal),
@@ -28,42 +22,14 @@ class SDG::Goals::TagListComponent < ApplicationComponent
end
def goals
if record_or_name.respond_to?(:sdg_goals)
record_or_name.sdg_goals.order(:code)
else
SDG::Goal.order(:code)
end
end
def see_more_link
return unless limit && count_out_of_limit > 0
[
"#{count_out_of_limit}+",
polymorphic_path(record_or_name),
class: "more-goals", title: t("sdg.goals.filter.more", count: count_out_of_limit)
]
record&.sdg_goals || SDG::Goal.all
end
def index_by_goal(goal)
polymorphic_path(model, advanced_search: { goal: goal.code })
index_by(goal: goal.code)
end
def filter_text(goal)
t("sdg.goals.filter.link",
resources: model.model_name.human(count: :other),
code: goal.code)
end
def count_out_of_limit
goals.size - limit
end
def model
process.name.constantize
end
def process
@process ||= SDG::ProcessEnabled.new(record_or_name)
def i18n_namespace
"goals"
end
end

View File

@@ -0,0 +1,3 @@
<div class="sdg-tag-list">
<%= render SDG::Goals::TagListComponent.new(record, limit: limit) %>
</div>

View File

@@ -0,0 +1,8 @@
class SDG::TagListComponent < ApplicationComponent
attr_reader :record, :limit
def initialize(record, limit: nil)
@record = record
@limit = limit
end
end

View File

@@ -17,4 +17,4 @@
</ul>
<% end %>
<%= render SDG::Goals::TagListComponent.new(taggable, limit: limit) %>
<%= render SDG::TagListComponent.new(taggable, limit: limit) %>