adds search to debates index

This commit is contained in:
Juanjo Bazán
2015-09-05 15:20:46 +02:00
parent 771f44c22b
commit b020795d3d
6 changed files with 61 additions and 35 deletions

View File

@@ -1,13 +1,14 @@
class DebatesController < ApplicationController class DebatesController < ApplicationController
before_action :parse_order, only: :index before_action :parse_order, only: :index
before_action :parse_tag_filter, only: :index before_action :parse_tag_filter, only: :index
before_action :parse_search_terms, only: :index
before_action :authenticate_user!, except: [:index, :show] before_action :authenticate_user!, except: [:index, :show]
load_and_authorize_resource load_and_authorize_resource
respond_to :html, :js respond_to :html, :js
def index def index
@debates = 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_#{@order}") @debates = @debates.page(params[:page]).for_render.send("sort_by_#{@order}")
@tag_cloud = Debate.tag_counts.order(taggings_count: :desc, name: :asc).limit(20) @tag_cloud = Debate.tag_counts.order(taggings_count: :desc, name: :asc).limit(20)
@@ -92,4 +93,8 @@ class DebatesController < ApplicationController
end end
end end
def parse_search_terms
@search_terms = params[:search] if params[:search].present?
end
end end

View File

@@ -3,18 +3,24 @@
<div class="filters row"> <div class="filters row">
<div class="small-12 medium-9 column"> <div class="small-12 medium-9 column">
<% if @tag_filter %> <% if @search_terms %>
<div class="inline-block small-12 medium-8" > <div class="inline-block small-12 medium-8" >
<h2> <h2>
<%= t("debates.index.filter_topic", <%= page_entries_info @debates %>
count: @debates.size, <%= t("debates.index.search_results", count: @debates.size, search_term: @search_terms) %>
topic: @tag_filter) %> </h2>
</div>
<% elsif @tag_filter %>
<div class="inline-block small-12 medium-8" >
<h2>
<%= page_entries_info @debates %>
<%= t("debates.index.filter_topic", count: @debates.size, topic: @tag_filter) %>
</h2> </h2>
</div> </div>
<% end %> <% end %>
<div class="inline-block <%= 'right' if @tag_filter %>"> <div class="inline-block <%= 'right' if (@tag_filter || @search_terms) %>">
<% if @tag_filter %> <% if @tag_filter || @search_terms %>
<h6 class="inline-block"> <h6 class="inline-block">
<%= t("debates.index.select_order") %> <%= t("debates.index.select_order") %>
</h6> </h6>
@@ -37,18 +43,6 @@
</div> </div>
</div> </div>
<!-- Search results -->
<div class="filters row">
<div class="small-12 column">
<h2>
<%= t("debates.index.search_results",
number: "N",
search_term: "búsqueda").html_safe%>
</h2>
</div>
</div>
<!-- /. Search results -->
<div class="row"> <div class="row">
<div id="debates" class="debates-list small-12 medium-9 column js-order-<%= @order.dasherize %>"> <div id="debates" class="debates-list small-12 medium-9 column js-order-<%= @order.dasherize %>">
<%= render @debates %> <%= render @debates %>

View File

@@ -3,15 +3,19 @@
<h3><%= t("shared.search_form.search_title") %></h3> <h3><%= t("shared.search_form.search_title") %></h3>
<br> <br>
<div class="row"> <div class="row">
<div class="small-12 columns"> <%= form_tag debates_path, method: :get do %>
<div class="row collapse"> <div class="small-12 columns">
<div class="small-9 medium-12 large-9 columns"> <div class="row collapse">
<input type="text" placeholder="<%= t("shared.search_form.search_placeholder") %>">
</div> <div class="small-9 medium-12 large-9 columns">
<div class="small-3 medium-12 large-3 columns"> <input type="text" name="search" placeholder="<%= t("shared.search_form.search_placeholder") %>">
<a href="#" class="button warning postfix"><%= t("shared.search_form.search_button") %></a> </div>
<div class="small-3 medium-12 large-3 columns">
<input type="submit" class="button warning postfix" value="<%= t('shared.search_form.search_button') %>">
</div>
</div> </div>
</div> </div>
</div> <% end %>
</div> </div>
</div> </div>

View File

@@ -77,9 +77,11 @@ en:
most_commented: most commented most_commented: most commented
random: random random: random
filter_topic: filter_topic:
one: "You are seeing one debate with the topic '%{topic}'" one: " with the topic '%{topic}'"
other: "You are seeing %{count} debates with the topic '%{topic}'" other: " with the topic '%{topic}'"
search_results: "Showing %{number} debates containing '%{search_term}'" search_results:
one: " containing '%{search_term}'"
other: " containing '%{search_term}'"
debate: debate:
debate: Debate debate: Debate
comments: comments:

View File

@@ -77,9 +77,11 @@ es:
most_commented: "más comentados" most_commented: "más comentados"
random: "aleatorio" random: "aleatorio"
filter_topic: filter_topic:
one: "Estás viendo un debate con el tema ''%{topic}''" one: " con el tema '%{topic}'"
other: "Estás viendo %{count} debates con el tema '%{topic}'" other: " con el tema '%{topic}'"
search_results: "Mostrando %{number} debates que contienen '%{search_term}'" search_results:
one: " que contiene '%{search_term}'"
other: " que contienen '%{search_term}'"
debate: debate:
debate: Debate debate: Debate
comments: comments:

View File

@@ -441,4 +441,23 @@ feature 'Debates' do
expect(debates_first_time).to_not eq(debates_second_time) expect(debates_first_time).to_not eq(debates_second_time)
end end
end end
scenario 'Debate index search' do
debate1 = create(:debate, title: "Show me what you got")
debate2 = create(:debate, title: "Get Schwifty")
debate3 = create(:debate, description: "Unity")
debate4 = create(:debate, description: "Schwifty in here")
visit debates_path
fill_in "search", with: "Schwifty"
click_button "Search"
within("#debates") do
expect(page).to have_css('.debate', count: 2)
expect(page).to have_content(debate2.title)
expect(page).to have_content(debate4.title)
expect(page).to_not have_content(debate1.title)
expect(page).to_not have_content(debate3.title)
end
end
end end