Skip to content

Commit

Permalink
Merge 52c5d82 into 3555937
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanl committed Jun 19, 2013
2 parents 3555937 + 52c5d82 commit c23a6bd
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
25 changes: 21 additions & 4 deletions lib/searchlight/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,23 @@ def searches(*attribute_names)
end

eval_string = attribute_names.map { |attribute_name|
<<-UNICORN_BILE
def search_#{attribute_name}
search.where('#{attribute_name}' => public_send("#{attribute_name}"))
model_class = model_class_for(search_target)
if model_class_for(search_target).columns_hash.keys.include?(attribute_name.to_s)

<<-UNICORN_BILE
def search_#{attribute_name}
search.where('#{attribute_name}' => public_send("#{attribute_name}"))
end
UNICORN_BILE
else
<<-MERMAID_TEARS
def search_#{attribute_name}
raise Searchlight::Adapters::ActiveRecord::UndefinedColumn,
"Class `#{model_class}` has no column `#{attribute_name}`; please define `search_#{attribute_name}` on `\#{self.class}` to clarify what you intend to search for"
end
MERMAID_TEARS
end
UNICORN_BILE

}.join

@ar_searches_module.module_eval(eval_string, __FILE__, __LINE__)
Expand All @@ -49,10 +61,15 @@ def convert_to_relation
self.search_target = (active_record_version >= 4) ? search_target.all : search_target.scoped
end

def model_class_for(target)
is_active_record_class?(target) ? target : target.engine
end

def active_record_version
::ActiveRecord::VERSION::MAJOR.to_i
end

UndefinedColumn = Class.new(StandardError)
end
end
end
Expand Down
48 changes: 36 additions & 12 deletions spec/searchlight/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,46 @@

shared_examples "search classes with an ActiveRecord target" do

before :each do
search_class.searches :elephants
end
context "when the base model has a column matching the search term" do

it "adds search methods to the search class" do
expect(search_class.new).to respond_to(:search_elephants)
end
before :each do
MockActiveRecord.stub(:columns_hash).and_return({'elephants' => 'column info...'})
search_class.searches :elephants
end

it "adds search methods to the search class" do
expect(search_class.new).to respond_to(:search_elephants)
end

it "defines search methods that call `where` on the search target" do
search_instance.results
expect(search_instance.search.called_methods).to include(:where)
end

it "sets arguments properly in the defined method" do
search_instance.search.should_receive(:where).with('elephants' => 'yes, please')
search_instance.search_elephants
end

it "defines search methods that call where on the search target" do
search_instance.results
expect(search_instance.search.called_methods).to include(:where)
end

it "sets arguments properly in the defined method" do
search_instance.search.should_receive(:where).with('elephants' => 'yes, please')
search_instance.search_elephants
context "when the base model has no column matching the search term" do

before :each do
MockActiveRecord.stub(:columns_hash).and_return({})
search_class.searches :elephants
end

it "adds search methods to the search class" do
expect(search_class.new).to respond_to(:search_elephants)
end

it "defines search methods to raise an exception" do
expect { search_instance.results }.to raise_error(
Searchlight::Adapters::ActiveRecord::UndefinedColumn
)
end

end

end
Expand Down
4 changes: 4 additions & 0 deletions spec/support/mock_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def is_a?(thing)
thing == ::ActiveRecord::Relation ? true : super
end

def engine
MockActiveRecord
end

end

module Namespaced
Expand Down

0 comments on commit c23a6bd

Please sign in to comment.