From 201ad4f0f33e191b09bdb1b640897613a3368eac Mon Sep 17 00:00:00 2001 From: Jesper Kjeldgaard Date: Sat, 18 Jan 2014 22:02:06 +0000 Subject: [PATCH] Implement basic idea search [#22] --- Gemfile | 2 ++ Gemfile.lock | 5 ++++- .../stylesheets/bootstrap_ext/_forms.css.sass | 10 ++++++++++ app/controllers/search_controller.rb | 10 ++++++++++ app/models/search.rb | 14 ++++++++++++++ app/views/application/_header.html.haml | 1 + app/views/application/_search.html.haml | 4 ++++ app/views/ideas/_idea_maxi.html.haml | 6 +++--- app/views/search/index.html.haml | 5 +++++ config/routes.rb | 2 ++ features/ideas/search.feature | 15 +++++++++++++++ features/step_definitions/search_steps.rb | 13 +++++++++++++ 12 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 app/controllers/search_controller.rb create mode 100644 app/models/search.rb create mode 100644 app/views/application/_search.html.haml create mode 100644 app/views/search/index.html.haml create mode 100644 features/ideas/search.feature create mode 100644 features/step_definitions/search_steps.rb diff --git a/Gemfile b/Gemfile index 0b48004..7039160 100644 --- a/Gemfile +++ b/Gemfile @@ -101,6 +101,8 @@ gem 'animation' # asset minification gem 'uglifier', '>= 1.0.3' +# Search +gem 'textacular', '~> 3.0' group :development do # better generators diff --git a/Gemfile.lock b/Gemfile.lock index 50d2b63..17399f2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,7 +7,7 @@ GIT activerecord (>= 3.2.0, < 5.0) GEM - remote: http://yarp.dev/ + remote: https://rubygems.org/ specs: actionmailer (4.0.2) actionpack (= 4.0.2) @@ -396,6 +396,8 @@ GEM tins (~> 0.8) terminal-notifier-guard (1.5.3) text (1.2.3) + textacular (3.1.0) + activerecord (>= 3.0, < 4.1) thin (1.6.1) daemons (>= 1.0.9) eventmachine (>= 1.0.0) @@ -511,6 +513,7 @@ DEPENDENCIES state_machine taps terminal-notifier-guard + textacular (~> 3.0) thin timecop turbolinks diff --git a/app/assets/stylesheets/bootstrap_ext/_forms.css.sass b/app/assets/stylesheets/bootstrap_ext/_forms.css.sass index f64e7f7..1100aca 100644 --- a/app/assets/stylesheets/bootstrap_ext/_forms.css.sass +++ b/app/assets/stylesheets/bootstrap_ext/_forms.css.sass @@ -22,3 +22,13 @@ select, &:focus border-color: $focus_color @include box-shadow(inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px $focus_color) + +.form-search + margin-top: 5px + margin-bottom: 0 + + input[type=text] + height: 30px + + &:focus + @extend .input-large diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb new file mode 100644 index 0000000..10f2e12 --- /dev/null +++ b/app/controllers/search_controller.rb @@ -0,0 +1,10 @@ +# encoding: UTF-8 +class SearchController < ApplicationController + include Traits::RequiresLogin + + before_filter :require_account! + + def index + @search = Search.new(params[:search].fetch(:query, nil)) + end +end diff --git a/app/models/search.rb b/app/models/search.rb new file mode 100644 index 0000000..d859bdf --- /dev/null +++ b/app/models/search.rb @@ -0,0 +1,14 @@ +class Search + include ActiveModel::Conversion + extend ActiveModel::Naming + + attr_accessor :query + + def initialize(query = nil) + @query = query + end + + def ideas + Idea.basic_search(query) + end +end diff --git a/app/views/application/_header.html.haml b/app/views/application/_header.html.haml index c36048d..8a7bd6c 100644 --- a/app/views/application/_header.html.haml +++ b/app/views/application/_header.html.haml @@ -22,6 +22,7 @@ %a{href: path, title: s_('Tooltip|Show stories you are following'), :'data-placement' => 'bottom'} %i.fa.fa-bookmark-o %span= _('Bookmarks') + %li= render 'search' %ul.nav.pull-right#current_user - if current_account diff --git a/app/views/application/_search.html.haml b/app/views/application/_search.html.haml new file mode 100644 index 0000000..ac86c0e --- /dev/null +++ b/app/views/application/_search.html.haml @@ -0,0 +1,4 @@ += form_for(@search || Search.new, method: :get, html: { class: 'form-search' }) do |f| + .input-append + = f.text_field :query, class: 'input-medium search-query', placeholder: 'Search' + = f.submit 'Go', class: 'btn' diff --git a/app/views/ideas/_idea_maxi.html.haml b/app/views/ideas/_idea_maxi.html.haml index a2a00e9..0e04ffa 100644 --- a/app/views/ideas/_idea_maxi.html.haml +++ b/app/views/ideas/_idea_maxi.html.haml @@ -14,7 +14,7 @@ .idea.idea-maxi.af-drop-hilite{ class:classes } .meta.top.af-mutable - = render 'idea_rating', idea:idea + = render 'ideas/idea_rating', idea:idea .category.muted.pull-left.pull-left-spaced = idea.category || _('No category') .state.muted.pull-right.pull-right-spaced{ title: s_('Tooltip|The current progress on this idea'), :'data-container' => '.container' }< @@ -48,7 +48,7 @@ != pipeline_render idea.metrics .meta.bottom.af-mutable - = render 'idea_people', idea:idea + = render 'ideas/idea_people', idea:idea .actions.pull-right %ul @@ -65,4 +65,4 @@ = can_link_to idea, data: { confirm: _('Are you sure?') }, method: :delete, auth:[:destroy,idea], title: s_('Tooltip|Delete Idea') do %i.fa.fa-trash-o<> %li - = render 'bookmark_link', idea:idea + = render 'ideas/bookmark_link', idea:idea diff --git a/app/views/search/index.html.haml b/app/views/search/index.html.haml new file mode 100644 index 0000000..466e03b --- /dev/null +++ b/app/views/search/index.html.haml @@ -0,0 +1,5 @@ +- @search.ideas.each do |idea| + = render partial: 'ideas/idea_maxi', locals: { :idea => idea } + +- if @search.ideas.empty? + %h3.text-center No ideas matches #{@search.query.inspect} diff --git a/config/routes.rb b/config/routes.rb index 3bec4f9..78e2d2c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,6 +13,8 @@ resources :attachments, only: [:create, :destroy, :show] end + get '/search', controller: :search, action: :index + resources :comments do resources :votes, only: [:create, :destroy] resources :attachments, only: [:create, :destroy, :show] diff --git a/features/ideas/search.feature b/features/ideas/search.feature new file mode 100644 index 0000000..a441c3f --- /dev/null +++ b/features/ideas/search.feature @@ -0,0 +1,15 @@ +Feature: Idea search + As a user + In order to find existing ideas + I want to be able to search all ideas + + Background: + Given a submitted idea "A good idea for the future" + Given a submitted idea "Idea about something else" + Given a user named "Ursula" + And I sign in as "Ursula" + + Scenario: Query contains one result + When I search for "future" + Then I should see "A good idea for the future" + And I should not see "Idea about something else" diff --git a/features/step_definitions/search_steps.rb b/features/step_definitions/search_steps.rb new file mode 100644 index 0000000..b7f4cd9 --- /dev/null +++ b/features/step_definitions/search_steps.rb @@ -0,0 +1,13 @@ +When(/^I search for "(.*?)"$/) do |query| + visit root_url + fill_in 'search_query', with: query + click_button 'Go' +end + +Then(/^I should see "(.*?)"$/) do |text| + page.should have_text(text) +end + +Then(/^I should not see "(.*?)"$/) do |text| + page.should_not have_text(text) +end