scopes tags by category or geozone
This commit is contained in:
@@ -73,7 +73,7 @@ module CommentableActions
|
||||
end
|
||||
|
||||
def tag_cloud
|
||||
TagCloud.new(resource_model).tags
|
||||
TagCloud.new(resource_model, params[:search]).tags
|
||||
end
|
||||
|
||||
def load_geozones
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
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
|
||||
@scope = scope
|
||||
end
|
||||
|
||||
def tags
|
||||
resource_model.last_week.tag_counts.
|
||||
where("lower(name) NOT IN (?)", category_names + geozone_names + default_blacklist)
|
||||
resource_model_scoped.
|
||||
last_week.tag_counts.
|
||||
where("lower(name) NOT IN (?)", category_names + geozone_names + default_blacklist).
|
||||
order("#{table_name}_count": :desc, name: :asc).
|
||||
limit(5)
|
||||
end
|
||||
@@ -21,6 +23,10 @@ class TagCloud
|
||||
Geozone.all.map {|geozone| geozone.name.downcase }
|
||||
end
|
||||
|
||||
def resource_model_scoped
|
||||
scope ? resource_model.search(scope) : resource_model
|
||||
end
|
||||
|
||||
def default_blacklist
|
||||
['']
|
||||
end
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
<% tag_cloud @tag_cloud, %w[s m l] do |tag, css_class| %>
|
||||
<%= 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)) %>)
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
@@ -141,4 +141,64 @@ feature 'Tags' do
|
||||
expect(page).to_not have_content 'Economía'
|
||||
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
|
||||
|
||||
@@ -55,6 +55,30 @@ describe TagCloud do
|
||||
expect(tag_names(tag_cloud)).to contain_exactly('parks', 'water')
|
||||
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
|
||||
3.times { create(:proposal, tag_list: 'participation') }
|
||||
create(:proposal, tag_list: 'corruption')
|
||||
|
||||
Reference in New Issue
Block a user