Merge pull request #366 from AyuntamientoMadrid/search-debates

Search debates
This commit is contained in:
Raimond Garcia
2015-09-05 20:19:37 +02:00
10 changed files with 136 additions and 17 deletions

View File

@@ -10,7 +10,7 @@
// 08. Forms // 08. Forms
// 09. Alerts // 09. Alerts
// 10. User account // 10. User account
// 11. Filters // 11. Filters & search
// 12. Official levels // 12. Official levels
// 13. Pagination // 13. Pagination
// 14. Tables // 14. Tables
@@ -133,9 +133,14 @@ h6 {
.button { .button {
font-size: rem-calc(13); font-size: rem-calc(13);
font-family: $font-family-sans-serif !important;
padding: rem-calc(15) rem-calc(32); padding: rem-calc(15) rem-calc(32);
} }
.postfix.button {
padding: 0;
}
.clear { .clear {
clear: both; clear: both;
} }
@@ -985,7 +990,7 @@ img.initialjs-avatar {
top: -9px; top: -9px;
} }
// 11. Filters // 11. Filters & search
// - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - -
.filters { .filters {
@@ -1018,6 +1023,23 @@ img.initialjs-avatar {
} }
} }
.search-results {
@extend .filters;
}
.search-form {
h3 {
border-top: 1px solid $votes-border;
display: inline-block;
font-family: $font-family-sans-serif;
font-size: rem-calc(16);
margin: -1px 0 rem-calc(12);
padding-top: rem-calc(6);
text-transform: uppercase;
}
}
// 12. Officials levels // 12. Officials levels
// - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - -

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

@@ -128,6 +128,10 @@ class Debate < ActiveRecord::Base
self.hot_score = (age_in_units**3 + weighted_score * 1000).round self.hot_score = (age_in_units**3 + weighted_score * 1000).round
end end
def self.search(terms)
terms.present? ? where("title ILIKE ? OR description ILIKE ?", "%#{terms}%", "%#{terms}%") : none
end
protected protected
def sanitize_description def sanitize_description

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>
@@ -45,6 +51,7 @@
<div class="small-12 medium-3 column"> <div class="small-12 medium-3 column">
<aside class="sidebar" role="complementary"> <aside class="sidebar" role="complementary">
<%= link_to t("debates.index.start_debate"), new_debate_path, class: 'button radius expand' %> <%= link_to t("debates.index.start_debate"), new_debate_path, class: 'button radius expand' %>
<%= render "shared/search_form" %>
<%= render "shared/tag_cloud" %> <%= render "shared/tag_cloud" %>
</aside> </aside>
</div> </div>

View File

@@ -0,0 +1,21 @@
<div class="search-form">
<div class="sidebar-divider"></div>
<h3><%= t("shared.search_form.search_title") %></h3>
<br>
<div class="row">
<%= form_tag debates_path, method: :get do %>
<div class="small-12 columns">
<div class="row collapse">
<div class="small-9 medium-12 large-9 columns">
<input type="text" name="search" placeholder="<%= t("shared.search_form.search_placeholder") %>">
</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>
<% end %>
</div>
</div>

View File

@@ -77,8 +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:
one: " containing '%{search_term}'"
other: " containing '%{search_term}'"
debate: debate:
debate: Debate debate: Debate
comments: comments:
@@ -193,6 +196,10 @@ en:
flag: Flag as inappropriate flag: Flag as inappropriate
unflag: Undo flag unflag: Undo flag
collective: Collective collective: Collective
search_form:
search_title: Search
search_button: Search
search_placeholder: "Search..."
mailer: mailer:
comment: comment:
subject: Someone has commented on your debate subject: Someone has commented on your debate

View File

@@ -77,8 +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:
one: " que contiene '%{search_term}'"
other: " que contienen '%{search_term}'"
debate: debate:
debate: Debate debate: Debate
comments: comments:
@@ -189,10 +192,14 @@ es:
debate: "el código secreto no coincide con la imagen" debate: "el código secreto no coincide con la imagen"
shared: shared:
tags_cloud: tags_cloud:
tags: Temas tags: Temas
flag: Denunciar como inapropiado flag: Denunciar como inapropiado
unflag: Deshacer denuncia unflag: Deshacer denuncia
collective: Colectivo collective: Colectivo
search_form:
search_title: Buscar
search_button: Buscar
search_placeholder: "Buscar..."
mailer: mailer:
comment: comment:
subject: Alguien ha comentado en tu debate subject: Alguien ha comentado en tu debate

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

View File

@@ -239,6 +239,33 @@ describe Debate do
end end
describe "self.search" do
it "find debates by title" do
debate1 = create(:debate, title: "Karpov vs Kasparov")
create(:debate, title: "Bird vs Magic")
search = Debate.search("Kasparov")
expect(search.size).to eq(1)
expect(search.first).to eq(debate1)
end
it "find debates by description" do
debate1 = create(:debate, description: "...chess masters...")
create(:debate, description: "...basket masters...")
search = Debate.search("chess")
expect(search.size).to eq(1)
expect(search.first).to eq(debate1)
end
it "find debates by title and description" do
create(:debate, title: "Karpov vs Kasparov", description: "...played like Gauss...")
create(:debate, title: "Euler vs Gauss", description: "...math masters...")
search = Debate.search("Gauss")
expect(search.size).to eq(2)
end
it "returns no results if no search term provided" do
expect(Debate.search(" ").size).to eq(0)
end
end
end end

View File

@@ -249,7 +249,7 @@ describe User do
expect(search.first).to eq(user1) expect(search.first).to eq(user1)
end end
it "returns no results if no email provided" do it "returns no results if no search term provided" do
expect(User.search(" ").size).to eq(0) expect(User.search(" ").size).to eq(0)
end end
end end