Merge pull request #275 from mlovic/filter-topic

Allow debates to be filtered by topic
This commit is contained in:
Raimond Garcia
2015-08-28 17:41:38 +02:00
8 changed files with 88 additions and 24 deletions

View File

@@ -33,6 +33,7 @@ var initialize_modules = function() {
App.Stats.initialize(); App.Stats.initialize();
App.LocaleSwitcher.initialize(); App.LocaleSwitcher.initialize();
App.DebatesOrderSelector.initialize(); App.DebatesOrderSelector.initialize();
App.DebatesTagFilter.initialize();
}; };
$(function(){ $(function(){

View File

@@ -0,0 +1,17 @@
App.DebatesTagFilter =
href_with_params: (query_params) ->
loc = window.location
loc.protocol + "//" + loc.hostname +
(if loc.port then ':' + loc.port else '') +
loc.pathname +
loc.hash +
'?' + $.param(query_params)
initialize: ->
$('.js-tag-filter').on 'change', ->
query_params = window.getQueryParameters()
query_params['tag'] = $(this).val()
window.location.assign(App.DebatesTagFilter.href_with_params(query_params))

View File

@@ -1,5 +1,5 @@
class DebatesController < ApplicationController class DebatesController < ApplicationController
before_action :parse_order, only: :index before_action :parse_order, :parse_tag_filter, only: :index
before_action :authenticate_user!, except: [:index, :show] before_action :authenticate_user!, except: [:index, :show]
load_and_authorize_resource load_and_authorize_resource
@@ -77,4 +77,9 @@ class DebatesController < ApplicationController
@order = @valid_orders.include?(params[:order]) ? params[:order] : 'created_at' @order = @valid_orders.include?(params[:order]) ? params[:order] : 'created_at'
end end
def parse_tag_filter
valid_tags = ActsAsTaggableOn::Tag.all.map(&:name)
@tag_filter = params[:tag] if valid_tags.include?(params[:tag])
end
end end

View File

@@ -1,5 +0,0 @@
<form class="inline-block">
<select class="js-order-selector" name="order-selector">
<%= available_options_for_order_selector(@valid_orders, @order) %>
</select>
</form>

View File

@@ -1,25 +1,37 @@
<section role="main"> <section role="main">
<!-- Filters -->
<div class="filters row"> <div class="filters row">
<div class="small-12 column"> <div class="small-9 column">
<h2><%= t("debates.index.showing") %></h2>
<%= render 'order_selector' %>
</div>
</div>
<!-- /. Filters -->
<!-- Filter topic results --> <div class="inline-block" >
<div class="filters row"> <% if @tag_filter %>
<div class="small-12 column">
<h2> <h2>
<%= t("debates.index.filter_topic", <%= t("debates.index.filter_topic",
number: "N", number: @debates.size,
topic: "topic").html_safe %> topic: @tag_filter).html_safe %>
</h2> </h2>
<% else %>
<h2><%= t("debates.index.select_topic") %></h2>
<form class="inline-block">
<select class="js-tag-filter" name="tag-filter">
<option value="all" selected="selected"><%= t("debates.index.all") %></option>
<%= options_from_collection_for_select(ActsAsTaggableOn::Tag.all, :name, :name) %>
</select>
</form>
<% end %>
</div>
<div class="inline-block right">
<h6 class="inline-block"><%= t("debates.index.select_order") %></h6>
<form class="inline-block">
<select class="js-order-selector" name="order-selector">
<%= available_options_for_order_selector(@valid_orders, @order) %>
</select>
</form>
</div>
</div> </div>
</div> </div>
<!-- /. Filter topic results -->
<div class="row"> <div class="row">
<div id="debates" class="debates-list small-12 medium-9 column"> <div id="debates" class="debates-list small-12 medium-9 column">

View File

@@ -56,12 +56,14 @@ en:
debates: debates:
index: index:
create_debate: Create a debate create_debate: Create a debate
showing: You are seeing debates select_order: Order by
orders: orders:
created_at: newest created_at: newest
total_votes: most voted total_votes: most voted
likes: best rated likes: best rated
select_topic: "Filter by topic:"
filter_topic: "You are seeing %{number} debates with the topic '%{topic}'" filter_topic: "You are seeing %{number} debates with the topic '%{topic}'"
all: All
debate: debate:
debate: Debate debate: Debate
comments: comments:

View File

@@ -56,12 +56,14 @@ es:
debates: debates:
index: index:
create_debate: Crea un debate create_debate: Crea un debate
showing: "Estás viendo los debates" select_order: Ordenar por
orders: orders:
created_at: "más nuevos" created_at: "más nuevos"
total_votes: "más votados" total_votes: "más votados"
likes: mejor valorados likes: mejor valorados
select_topic: "Filtrar por tema:"
filter_topic: "Estás viendo %{number} debates con el tema '%{topic}'" filter_topic: "Estás viendo %{number} debates con el tema '%{topic}'"
all: Todos
debate: debate:
debate: Debate debate: Debate
comments: comments:

View File

@@ -407,4 +407,34 @@ feature 'Debates' do
expect(@most_liked_debate.title).to appear_before(@most_voted_debate.title) expect(@most_liked_debate.title).to appear_before(@most_voted_debate.title)
end end
end end
feature 'Debates can be filtered by tags', :js do
let!(:debate1) { create(:debate, tag_list: ["Deporte", "Corrupción"]) }
let!(:debate2) { create(:debate, tag_list: ["Deporte", "Fiestas populares"]) }
let!(:debate3) { create(:debate, tag_list: ["Corrupción", "Fiestas populares"]) }
scenario 'By default no tag filter is applied' do
visit debates_path
expect(page).to have_content('Filter by topic')
expect(page).not_to have_content('with the topic')
expect(page).to have_selector('#debates .debate', count: 3)
end
scenario 'Debates are filtered by single tag' do
visit debates_path
select('Deporte', from: 'tag-filter')
expect(page).not_to have_content('Filter by topic')
expect(page).not_to have_select('tag-filter')
expect(page).to have_content('with the topic')
expect(current_url).to include('tag=Deporte')
expect(page).to have_selector('#debates .debate', count: 2)
expect(page).to_not have_content(debate3.title)
expect(page).to have_content(debate1.title)
expect(page).to have_content(debate2.title)
end
end
end end