diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb new file mode 100644 index 0000000..6825e7d --- /dev/null +++ b/app/controllers/search_controller.rb @@ -0,0 +1,12 @@ +class SearchController < ApplicationController + + def results + @search_parameter = params[:search][:parameter] + if @search_parameter.blank? + redirect_to root_path + else + @results = Zone.search(@search_parameter) + end + end + +end diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb new file mode 100644 index 0000000..b3ce20a --- /dev/null +++ b/app/helpers/search_helper.rb @@ -0,0 +1,2 @@ +module SearchHelper +end diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index d0c7cbe..f9d9c3f 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -10,6 +10,10 @@ #Header %h1 BIND DLZ on Rails + #search-bar{:style => "float: right"} + - form_for( :search, :url => { :controller => :search, :action => :results } ) do |f| + = f.text_field :parameter + = submit_tag "Search" - if current_user #Nav %ul diff --git a/app/views/search/results.html.haml b/app/views/search/results.html.haml index 9afadc8..8f47f41 100644 --- a/app/views/search/results.html.haml +++ b/app/views/search/results.html.haml @@ -3,6 +3,6 @@ %h2.underline Zones -- unless @zones.blank? - - for zone in @zones do +- unless @results.blank? + - for zone in @results do = link_to zone.name, zone_path( zone ) \ No newline at end of file diff --git a/spec/controllers/search_controller_spec.rb b/spec/controllers/search_controller_spec.rb new file mode 100644 index 0000000..b6660e0 --- /dev/null +++ b/spec/controllers/search_controller_spec.rb @@ -0,0 +1,24 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +describe SearchController do + fixtures :all + + before(:each) do + session[:user_id] = users(:admin).id + end + + it "should return results when searched legally" do + post :results, :search => { :parameter => 'exa' } + + assigns['results'].should_not be_nil + response.should render_template('results') + end + + it "should redirect to the index page when nothing has been searched for" do + post :results, :search => { :parameter => "" } + + response.should be_redirect + response.should redirect_to( root_path ) + end + +end diff --git a/spec/helpers/search_helper_spec.rb b/spec/helpers/search_helper_spec.rb new file mode 100644 index 0000000..f3bd415 --- /dev/null +++ b/spec/helpers/search_helper_spec.rb @@ -0,0 +1,11 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +describe SearchHelper do + + #Delete this example and add some real ones or delete this file + it "should include the SearchHelper" do + included_modules = self.metaclass.send :included_modules + included_modules.should include(SearchHelper) + end + +end