Commit 78b068d905d909e58c3162ed972d755fc9397107

Authored by Rafael Martins
1 parent 9eda279e

Small fix and tests for ActsAsSearchable

lib/acts_as_searchable.rb
... ... @@ -7,6 +7,7 @@ module ActsAsSearchable
7 7 return if !ACTS_AS_SEARCHABLE_ENABLED
8 8  
9 9 if (!options[:fields])
  10 + options[:additional_fields] ||= []
10 11 options[:additional_fields] |= [{:schema_name => :string}]
11 12 else
12 13 options[:fields] << {:schema_name => :string}
... ...
test/unit/acts_as_searchable_test.rb 0 → 100644
... ... @@ -0,0 +1,46 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +
  4 +class ActsAsSearchableTest < ActiveSupport::TestCase
  5 +
  6 + def setup
  7 + @test_model = Class.new ActiveRecord::Base
  8 + end
  9 +
  10 + def silent
  11 + # http://mentalized.net/journal/2010/04/02/suppress_warnings_from_ruby/
  12 + original_verbosity = $VERBOSE
  13 + $VERBOSE = nil
  14 + result = yield
  15 + $VERBOSE = original_verbosity
  16 + return result
  17 + end
  18 +
  19 + should 'be enabled by default' do
  20 + assert ActsAsSearchable::ClassMethods::ACTS_AS_SEARCHABLE_ENABLED, true
  21 + end
  22 +
  23 + should 'not be searchable when disabled' do
  24 + # suppress warning about already initialized constant
  25 + silent { ActsAsSearchable::ClassMethods::ACTS_AS_SEARCHABLE_ENABLED = false }
  26 +
  27 + @test_model.expects(:acts_as_solr).never
  28 + @test_model.acts_as_searchable
  29 + end
  30 +
  31 + should 'correctly pass options to search engine' do
  32 + options = {:fields => [{:name => :text}]}
  33 + @test_model.expects(:acts_as_solr).with(options)
  34 + @test_model.acts_as_searchable options
  35 + end
  36 +
  37 + should 'always include schema name as a field' do
  38 + @test_model.expects(:acts_as_solr).with(has_entry(:fields, [{:field1 => :text}, {:schema_name => :string}]))
  39 + @test_model.acts_as_searchable :fields => [{:field1 => :text}]
  40 + # ...even with no fields
  41 + @test_model = Class.new ActiveRecord::Base
  42 + @test_model.expects(:acts_as_solr).with(has_entry(:additional_fields, [{:schema_name => :string}]))
  43 + @test_model.acts_as_searchable
  44 + end
  45 +
  46 +end
... ...