From 54eeff41b726d2605a55cc4d99817ecb2bc05425 Mon Sep 17 00:00:00 2001 From: Alberto Garcia Cabeza Date: Mon, 24 Aug 2015 12:54:26 +0200 Subject: [PATCH 1/3] Adds search form to sidebar --- app/assets/stylesheets/participacion.scss | 26 +++++++++++++++++++++-- app/views/debates/index.html.erb | 13 ++++++++++++ app/views/shared/_search_form.html.erb | 17 +++++++++++++++ config/locales/en.yml | 5 +++++ config/locales/es.yml | 7 +++++- 5 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 app/views/shared/_search_form.html.erb diff --git a/app/assets/stylesheets/participacion.scss b/app/assets/stylesheets/participacion.scss index 73176293c..6294c9ca8 100644 --- a/app/assets/stylesheets/participacion.scss +++ b/app/assets/stylesheets/participacion.scss @@ -10,7 +10,7 @@ // 08. Forms // 09. Alerts // 10. User account -// 11. Filters +// 11. Filters & search // 12. Official levels // 13. Pagination // 14. Tables @@ -133,9 +133,14 @@ h6 { .button { font-size: rem-calc(13); + font-family: $font-family-sans-serif !important; padding: rem-calc(15) rem-calc(32); } +.postfix.button { + padding: 0; +} + .clear { clear: both; } @@ -985,7 +990,7 @@ img.initialjs-avatar { top: -9px; } -// 11. Filters +// 11. Filters & search // - - - - - - - - - - - - - - - - - - - - - - - - - .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 // - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/views/debates/index.html.erb b/app/views/debates/index.html.erb index bcd74445c..2657857bf 100644 --- a/app/views/debates/index.html.erb +++ b/app/views/debates/index.html.erb @@ -37,6 +37,18 @@ + +
+
+

+ <%= t("debates.index.search_results", + number: "N", + search_term: "búsqueda").html_safe%> +

+
+
+ +
<%= render @debates %> @@ -45,6 +57,7 @@
diff --git a/app/views/shared/_search_form.html.erb b/app/views/shared/_search_form.html.erb new file mode 100644 index 000000000..0e3af9a03 --- /dev/null +++ b/app/views/shared/_search_form.html.erb @@ -0,0 +1,17 @@ +
+ +

<%= t("shared.search_form.search_title") %>

+
+ +
diff --git a/config/locales/en.yml b/config/locales/en.yml index 832e8ad4e..ce683310b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -79,6 +79,7 @@ en: filter_topic: one: "You are seeing one debate with the topic '%{topic}'" other: "You are seeing %{count} debates with the topic '%{topic}'" + search_results: "Showing %{number} debates containing '%{search_term}'" debate: debate: Debate comments: @@ -193,6 +194,10 @@ en: flag: Flag as inappropriate unflag: Undo flag collective: Collective + search_form: + search_title: Search + search_button: Search + search_placeholder: "Search..." mailer: comment: subject: Someone has commented on your debate diff --git a/config/locales/es.yml b/config/locales/es.yml index 96b1cb6df..e6e8ab462 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -79,6 +79,7 @@ es: filter_topic: one: "Estás viendo un debate con el tema ''%{topic}''" other: "Estás viendo %{count} debates con el tema '%{topic}'" + search_results: "Mostrando %{number} debates que contienen '%{search_term}'" debate: debate: Debate comments: @@ -189,10 +190,14 @@ es: debate: "el código secreto no coincide con la imagen" shared: tags_cloud: - tags: Temas + tags: Temas flag: Denunciar como inapropiado unflag: Deshacer denuncia collective: Colectivo + search_form: + search_title: Buscar + search_button: Buscar + search_placeholder: "Buscar..." mailer: comment: subject: Alguien ha comentado en tu debate From 771f44c22bf428a4143d34ed5c0c5e6c72f245d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Sat, 5 Sep 2015 15:05:56 +0200 Subject: [PATCH 2/3] adds self.search to Debate --- app/models/debate.rb | 4 ++++ spec/models/debate_spec.rb | 29 ++++++++++++++++++++++++++++- spec/models/user_spec.rb | 2 +- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/models/debate.rb b/app/models/debate.rb index ed0fc17b8..8471be20d 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -128,6 +128,10 @@ class Debate < ActiveRecord::Base self.hot_score = (age_in_units**3 + weighted_score * 1000).round end + def self.search(terms) + terms.present? ? where("title ILIKE ? OR description ILIKE ?", "%#{terms}%", "%#{terms}%") : none + end + protected def sanitize_description diff --git a/spec/models/debate_spec.rb b/spec/models/debate_spec.rb index 537cc303c..25e878d18 100644 --- a/spec/models/debate_spec.rb +++ b/spec/models/debate_spec.rb @@ -239,6 +239,33 @@ describe Debate do 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 -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 \ No newline at end of file diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 87d0994bb..282d8d615 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -249,7 +249,7 @@ describe User do expect(search.first).to eq(user1) 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) end end From b020795d3d0e12a0fab148bed23819fd3dcb9a72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Sat, 5 Sep 2015 15:20:46 +0200 Subject: [PATCH 3/3] adds search to debates index --- app/controllers/debates_controller.rb | 7 +++++- app/views/debates/index.html.erb | 32 +++++++++++--------------- app/views/shared/_search_form.html.erb | 22 ++++++++++-------- config/locales/en.yml | 8 ++++--- config/locales/es.yml | 8 ++++--- spec/features/debates_spec.rb | 19 +++++++++++++++ 6 files changed, 61 insertions(+), 35 deletions(-) diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index 68570dd70..1c5696304 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -1,13 +1,14 @@ class DebatesController < ApplicationController before_action :parse_order, only: :index before_action :parse_tag_filter, only: :index + before_action :parse_search_terms, only: :index before_action :authenticate_user!, except: [:index, :show] load_and_authorize_resource respond_to :html, :js 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.page(params[:page]).for_render.send("sort_by_#{@order}") @tag_cloud = Debate.tag_counts.order(taggings_count: :desc, name: :asc).limit(20) @@ -92,4 +93,8 @@ class DebatesController < ApplicationController end end + def parse_search_terms + @search_terms = params[:search] if params[:search].present? + end + end diff --git a/app/views/debates/index.html.erb b/app/views/debates/index.html.erb index 2657857bf..e23a45f5c 100644 --- a/app/views/debates/index.html.erb +++ b/app/views/debates/index.html.erb @@ -3,18 +3,24 @@
- <% if @tag_filter %> + <% if @search_terms %>
-

- <%= t("debates.index.filter_topic", - count: @debates.size, - topic: @tag_filter) %> +

+ <%= page_entries_info @debates %> + <%= t("debates.index.search_results", count: @debates.size, search_term: @search_terms) %> +

+
+ <% elsif @tag_filter %> +
+

+ <%= page_entries_info @debates %> + <%= t("debates.index.filter_topic", count: @debates.size, topic: @tag_filter) %>

<% end %> -
- <% if @tag_filter %> +
+ <% if @tag_filter || @search_terms %>
<%= t("debates.index.select_order") %>
@@ -37,18 +43,6 @@
- -
-
-

- <%= t("debates.index.search_results", - number: "N", - search_term: "búsqueda").html_safe%> -

-
-
- -
<%= render @debates %> diff --git a/app/views/shared/_search_form.html.erb b/app/views/shared/_search_form.html.erb index 0e3af9a03..e6f7dda0e 100644 --- a/app/views/shared/_search_form.html.erb +++ b/app/views/shared/_search_form.html.erb @@ -3,15 +3,19 @@

<%= t("shared.search_form.search_title") %>


-
-
-
- "> -
-
- <%= t("shared.search_form.search_button") %> + <%= form_tag debates_path, method: :get do %> +
+
+ +
+ "> +
+
+ +
+
-
+ <% end %>
-
+
\ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index ce683310b..50bc610b1 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -77,9 +77,11 @@ en: most_commented: most commented random: random filter_topic: - one: "You are seeing one debate with the topic '%{topic}'" - other: "You are seeing %{count} debates with the topic '%{topic}'" - search_results: "Showing %{number} debates containing '%{search_term}'" + one: " with the topic '%{topic}'" + other: " with the topic '%{topic}'" + search_results: + one: " containing '%{search_term}'" + other: " containing '%{search_term}'" debate: debate: Debate comments: diff --git a/config/locales/es.yml b/config/locales/es.yml index e6e8ab462..430a9a363 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -77,9 +77,11 @@ es: most_commented: "más comentados" random: "aleatorio" filter_topic: - one: "Estás viendo un debate con el tema ''%{topic}''" - other: "Estás viendo %{count} debates con el tema '%{topic}'" - search_results: "Mostrando %{number} debates que contienen '%{search_term}'" + one: " con el tema '%{topic}'" + other: " con el tema '%{topic}'" + search_results: + one: " que contiene '%{search_term}'" + other: " que contienen '%{search_term}'" debate: debate: Debate comments: diff --git a/spec/features/debates_spec.rb b/spec/features/debates_spec.rb index 5a51b96bf..86903960f 100644 --- a/spec/features/debates_spec.rb +++ b/spec/features/debates_spec.rb @@ -441,4 +441,23 @@ feature 'Debates' do expect(debates_first_time).to_not eq(debates_second_time) 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