Skip to content

Instantly share code, notes, and snippets.

@mrdanadams
Created March 28, 2012 21:31
Show Gist options
  • Select an option

  • Save mrdanadams/2230763 to your computer and use it in GitHub Desktop.

Select an option

Save mrdanadams/2230763 to your computer and use it in GitHub Desktop.

Revisions

  1. mrdanadams revised this gist Mar 28, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,3 @@
    gem 'websolr-sunspot_rails'
    gem 'sunspot_rails'
    gem 'kaminari'
    gem 'sunspot_with_kaminari'
    gem 'sunspot_with_kaminari'
  2. mrdanadams created this gist Mar 28, 2012.
    3 changes: 3 additions & 0 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    gem 'websolr-sunspot_rails'
    gem 'kaminari'
    gem 'sunspot_with_kaminari'
    8 changes: 8 additions & 0 deletions cucumber_config.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # Solr faking.
    # see http://opensoul.org/blog/archives/2010/04/07/cucumber-and-sunspot/
    # Note: we need to do more than this if we start doing search tests in cucumber
    $original_sunspot_session = Sunspot.session

    Before("~@search") do
    Sunspot.session = Sunspot::Rails::StubSessionProxy.new($original_sunspot_session)
    end
    7 changes: 7 additions & 0 deletions results.html.erb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    <h3 class="summary"><%= t('.your-search-header'), start: @start, end: @end, total: pluralize(@total, 'result') %></h3>

    <% @results.each do |p| %>
    <div class="result"><!-- ... --></div>
    <% end %>

    <%= paginate @search, window: 1 %>
    17 changes: 17 additions & 0 deletions rspec_solr.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    RSpec.configure do |c|
    c.before :suite do
    module Sunspot
    def self.stub_session
    @sub_session ||= Sunspot::Rails::StubSessionProxy.new self.session
    end
    end

    end

    c.before :each do
    Sunspot.session = Sunspot.stub_session
    Sunspot.session = Sunspot.session.original_session if example.metadata[:solr]

    Sunspot.remove_all!
    end
    end
    2 changes: 2 additions & 0 deletions run.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    bundle exec rake sunspot:solr:start
    bundle exec rake sunspot:solr:stop
    24 changes: 24 additions & 0 deletions search_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    class SearchController < ApplicationController
    def search
    page_size = 10

    @search = Profile.solr_search do
    keywords params[:q] do
    boost_fields name: 10.0, user: 4.0
    end

    with :include_search, true
    paginate page: params[:page], per_page: page_size
    end

    @results = @search.results

    @total = @search.total
    @paging = @total > page_size
    @start = (@search.current_page - 1) * page_size + 1
    @end = [@start + page_size - 1, @total].min

    render (if @results.empty? then :no_results else :results end)
    end

    end
    14 changes: 14 additions & 0 deletions search_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    describe 'Search' do
    def commit
    Sunspot.commit
    end

    def search(q)
    visit "/search?q=#{q}"
    end

    it 'does something interesting', :solr do
    Something.create!
    commit
    search 'foobar'
    end
    16 changes: 16 additions & 0 deletions solr.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    # Note: ENV['SOLR_URL'] and ENV['WEBSOLR_URL'] override any values here
    # The defaults are same as what's below so you only need to override as desired.
    # Removed production since that should never be used in heroku.
    # See Sunspot::Rails::Configuration.solr_url

    development:
    solr:
    hostname: localhost
    port: 8982
    log_level: INFO

    test:
    solr:
    hostname: localhost
    port: 8981
    log_level: WARNING