Implements tag_cloud using the new custom counters

This commit is contained in:
kikito
2015-09-17 16:55:16 +02:00
parent 8ff8a1df7f
commit f9101b4970
4 changed files with 22 additions and 5 deletions

View File

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

View File

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

View File

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

View File

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