Skip to content

Commit

Permalink
Merge pull request #15 from mnarayan01/master
Browse files Browse the repository at this point in the history
Gracefully handle eager-loading of a Search prior to DB creation
  • Loading branch information
nathanl committed Aug 22, 2013
2 parents 69b27b1 + 6c7f5bd commit 14c3701
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/searchlight/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def searches(*attribute_names)

eval_string = attribute_names.map { |attribute_name|
model_class = model_class_for(search_target)
if model_class_for(search_target).columns_hash.keys.include?(attribute_name.to_s)
if model_has_db_attribute?(attribute_name.to_s)

<<-UNICORN_BILE
def search_#{attribute_name}
Expand All @@ -40,6 +40,25 @@ def search_#{attribute_name}

@ar_searches_module.module_eval(eval_string, __FILE__, __LINE__)
end

# The idea here is to provide a means to allow users to bypass the check if it causes problems (e.g. during
# `rake assets:precompile` if the DB has yet to be created). To bypass this, a user could monkey patch as
# follows:
#
# module Searchlight::Adapters::ActiveRecord::Search
# def model_has_db_attribute?(attribute_name)
# model_class_for(search_target).columns_hash.keys.include?(attribute_name)
# rescue StandardError
# true
# end
# end
#
# Alternatively, they could monkey-patch Searchlight::Adapters::ActiveRecord::Search::model_has_db_attribute
# to simply always return true, though they would then not get the benefit of the improved error messaging.
#
def model_has_db_attribute?(attribute_name)
model_class_for(search_target).columns_hash.keys.include?(attribute_name)
end
end

protected
Expand Down

0 comments on commit 14c3701

Please sign in to comment.