Implements tag_cloud using the new custom counters
This commit is contained in:
@@ -12,7 +12,7 @@ class DebatesController < ApplicationController
|
||||
@debates = @search_terms.present? ? Debate.search(@search_terms) : Debate.all
|
||||
@debates = @debates.tagged_with(@tag_filter) if @tag_filter
|
||||
@debates = @debates.page(params[:page]).for_render.send("sort_by_#{@current_order}")
|
||||
@tag_cloud = Debate.tag_counts.order(taggings_count: :desc, name: :asc).limit(20)
|
||||
@tag_cloud = Debate.tag_counts.order(debates_count: :desc, name: :asc).limit(20)
|
||||
set_debate_votes(@debates)
|
||||
end
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ class ProposalsController < ApplicationController
|
||||
@proposals = @search_terms.present? ? Proposal.search(@search_terms) : Proposal.all
|
||||
@proposals = @proposals.tagged_with(@tag_filter) if @tag_filter
|
||||
@proposals = @proposals.page(params[:page]).for_render.send("sort_by_#{@current_order}")
|
||||
@tag_cloud = Proposal.tag_counts.order(taggings_count: :desc, name: :asc).limit(20)
|
||||
@tag_cloud = Proposal.tag_counts.order(proposals_count: :desc, name: :asc).limit(20)
|
||||
set_proposal_votes(@proposals)
|
||||
end
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
module TagsHelper
|
||||
|
||||
def taggable_path(taggable, tag_name)
|
||||
case taggable
|
||||
def taggable_path(taggable_type, tag_name)
|
||||
case taggable_type
|
||||
when 'debate'
|
||||
debates_path(tag: tag_name)
|
||||
when 'proposal'
|
||||
@@ -11,4 +11,18 @@ module TagsHelper
|
||||
end
|
||||
end
|
||||
|
||||
def taggable_counter_field(taggable_type)
|
||||
"#{taggable_type.underscore.pluralize}_count"
|
||||
end
|
||||
|
||||
def tag_cloud(tags, classes, counter_field = :taggings_count)
|
||||
return [] if tags.empty?
|
||||
|
||||
max_count = tags.sort_by(&counter_field).last.send(counter_field).to_f
|
||||
|
||||
tags.each do |tag|
|
||||
index = ((tag.send(counter_field) / max_count) * (classes.size - 1))
|
||||
yield tag, classes[index.nan? ? 0 : index.round]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
<h3><%= t("shared.tags_cloud.tags") %></h3>
|
||||
<br>
|
||||
<% tag_cloud @tag_cloud, %w[s m l] do |tag, css_class| %>
|
||||
<%= link_to sanitize("#{tag.name} <span class='label round info'>#{tag.taggings_count}</span>"), taggable_path(taggable, tag.name), class: css_class %>
|
||||
<%= link_to taggable_path(taggable, tag.name), class: css_class do %>
|
||||
<%= tag.name %>
|
||||
<span class='label round info'><%= tag.send(taggable_counter_field(taggable)) %></span>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user