scopes tags by category or geozone

This commit is contained in:
rgarcia
2016-01-27 14:15:12 +01:00
parent 0175cd2c68
commit 5d4b6fae92
5 changed files with 96 additions and 6 deletions

View File

@@ -73,7 +73,7 @@ module CommentableActions
end end
def tag_cloud def tag_cloud
TagCloud.new(resource_model).tags TagCloud.new(resource_model, params[:search]).tags
end end
def load_geozones def load_geozones

View File

@@ -1,14 +1,16 @@
class TagCloud class TagCloud
attr_accessor :resource_model attr_accessor :resource_model, :scope
def initialize(resource_model) def initialize(resource_model, scope=nil)
@resource_model = resource_model @resource_model = resource_model
@scope = scope
end end
def tags def tags
resource_model.last_week.tag_counts. resource_model_scoped.
where("lower(name) NOT IN (?)", category_names + geozone_names + default_blacklist) last_week.tag_counts.
where("lower(name) NOT IN (?)", category_names + geozone_names + default_blacklist).
order("#{table_name}_count": :desc, name: :asc). order("#{table_name}_count": :desc, name: :asc).
limit(5) limit(5)
end end
@@ -21,6 +23,10 @@ class TagCloud
Geozone.all.map {|geozone| geozone.name.downcase } Geozone.all.map {|geozone| geozone.name.downcase }
end end
def resource_model_scoped
scope ? resource_model.search(scope) : resource_model
end
def default_blacklist def default_blacklist
[''] ['']
end end

View File

@@ -5,7 +5,7 @@
<% 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 taggable_path(taggable, tag.name), class: css_class do %> <%= link_to taggable_path(taggable, tag.name), class: css_class do %>
<%= tag.name %> <span class="tag"><%= tag.name %></span>
(<%= tag.send(taggable_counter_field(taggable)) %>) (<%= tag.send(taggable_counter_field(taggable)) %>)
<% end %> <% end %>
<% end %> <% end %>

View File

@@ -141,4 +141,64 @@ feature 'Tags' do
expect(page).to_not have_content 'Economía' expect(page).to_not have_content 'Economía'
end end
context 'Tag cloud' do
scenario 'Proposals' do
earth = create(:proposal, tag_list: 'Medio Ambiente')
money = create(:proposal, tag_list: 'Economía')
visit proposals_path
within "#tag-cloud" do
expect(page).to have_content "Medio Ambiente"
expect(page).to have_content "Economía"
end
end
scenario 'Debates' do
earth = create(:debate, tag_list: 'Medio Ambiente')
money = create(:debate, tag_list: 'Economía')
visit debates_path
within "#tag-cloud" do
expect(page).to have_content "Medio Ambiente"
expect(page).to have_content "Economía"
end
end
scenario "scoped by category" do
create(:tag, kind: 'category', name: 'Medio Ambiente')
create(:tag, kind: 'category', name: 'Economía')
earth = create(:proposal, tag_list: 'Medio Ambiente, Agua')
money = create(:proposal, tag_list: 'Economía, Corrupción')
visit proposals_path(search: 'Economía')
within "#tag-cloud" do
expect(page).to have_css(".tag", count: 1)
expect(page).to have_content "Corrupción"
expect(page).to_not have_content "Economía"
end
end
scenario "scoped by district" do
create(:geozone, name: 'Madrid')
create(:geozone, name: 'Barcelona')
earth = create(:proposal, tag_list: 'Madrid, Agua')
money = create(:proposal, tag_list: 'Barcelona, Playa')
visit proposals_path(search: 'Barcelona')
within "#tag-cloud" do
expect(page).to have_css(".tag", count: 1)
expect(page).to have_content "Playa"
expect(page).to_not have_content "Agua"
end
end
end
end end

View File

@@ -55,6 +55,30 @@ describe TagCloud do
expect(tag_names(tag_cloud)).to contain_exactly('parks', 'water') expect(tag_names(tag_cloud)).to contain_exactly('parks', 'water')
end end
it "returns tags scoped by category" do
create(:tag, kind: 'category', name: 'Education')
create(:tag, kind: 'category', name: 'Participation')
create(:proposal, tag_list: 'education, parks')
create(:proposal, tag_list: 'participation, water')
tag_cloud = TagCloud.new(Proposal, 'Education')
expect(tag_names(tag_cloud)).to contain_exactly('parks')
end
it "returns tags scoped by geozone" do
create(:geozone, name: 'California')
create(:geozone, name: 'New York')
create(:proposal, tag_list: 'parks, California')
create(:proposal, tag_list: 'water, New York')
tag_cloud = TagCloud.new(Proposal, 'California')
expect(tag_names(tag_cloud)).to contain_exactly('parks')
end
it "orders tags by count" do it "orders tags by count" do
3.times { create(:proposal, tag_list: 'participation') } 3.times { create(:proposal, tag_list: 'participation') }
create(:proposal, tag_list: 'corruption') create(:proposal, tag_list: 'corruption')