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 = @search_terms.present? ? Debate.search(@search_terms) : Debate.all
|
||||||
@debates = @debates.tagged_with(@tag_filter) if @tag_filter
|
@debates = @debates.tagged_with(@tag_filter) if @tag_filter
|
||||||
@debates = @debates.page(params[:page]).for_render.send("sort_by_#{@current_order}")
|
@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)
|
set_debate_votes(@debates)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class ProposalsController < ApplicationController
|
|||||||
@proposals = @search_terms.present? ? Proposal.search(@search_terms) : Proposal.all
|
@proposals = @search_terms.present? ? Proposal.search(@search_terms) : Proposal.all
|
||||||
@proposals = @proposals.tagged_with(@tag_filter) if @tag_filter
|
@proposals = @proposals.tagged_with(@tag_filter) if @tag_filter
|
||||||
@proposals = @proposals.page(params[:page]).for_render.send("sort_by_#{@current_order}")
|
@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)
|
set_proposal_votes(@proposals)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
module TagsHelper
|
module TagsHelper
|
||||||
|
|
||||||
def taggable_path(taggable, tag_name)
|
def taggable_path(taggable_type, tag_name)
|
||||||
case taggable
|
case taggable_type
|
||||||
when 'debate'
|
when 'debate'
|
||||||
debates_path(tag: tag_name)
|
debates_path(tag: tag_name)
|
||||||
when 'proposal'
|
when 'proposal'
|
||||||
@@ -11,4 +11,18 @@ module TagsHelper
|
|||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
@@ -3,6 +3,9 @@
|
|||||||
<h3><%= t("shared.tags_cloud.tags") %></h3>
|
<h3><%= t("shared.tags_cloud.tags") %></h3>
|
||||||
<br>
|
<br>
|
||||||
<% tag_cloud @tag_cloud, %w[s m l] do |tag, css_class| %>
|
<% 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 %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user