class EasySearch::Setup
Public Class Methods
accepts a block that specifies the columns to search for each model
Example:
Setup.config do setup_tables { ... } strip_keywords { ... } end
# File lib/easy_search/setup.rb, line 31 def config(&block) return nil unless block_given? self.class_eval(&block) end
returns an array of keywords that serve as no benefit in a search
Example:
$> Setup.dull_keywords $> => ['a', 'and', 'but', 'the', ...]
# File lib/easy_search/setup.rb, line 19 def dull_keywords (@@dull_keywords ||= Defaults.dull_keywords).flatten.uniq end
this is the magic that makes ‘setup_tables’ work like it does. once the block is eval’d those missing methods (i.e. “users” and “projects”) will be caught here and the table_settings
hash will be updated with the key set to the table, and the value set to the columns. this is what allows the EasySearch
plugin to work generically for any rails application.
# File lib/easy_search/setup.rb, line 82 def method_missing(table, *columns) table_settings[table] = columns end
REQUIRED accepts a block that specifies the columns to search for each model
Example:
setup_tables do users :first_name, :last_name, :email projects :title, :description end
# File lib/easy_search/setup.rb, line 45 def setup_tables(&block) return nil unless block_given? self.class_eval(&block) self.table_settings end
OPTIONAL allows customization of the dull_keywords
setting (can be overwritten or appended)
Example:
DEFAULT_DULL_KEYWORDS = ['the', 'and', 'is'] 1) appending keywords to the default list strip_keywords do ['it', 'why', 'is'] end # => ['the', 'and', 'it', 'why', 'is'] 2) overwriting existing keywords strip_keywords(true) do ['something', 'whatever'] end # => ['something', 'whatever']
# File lib/easy_search/setup.rb, line 69 def strip_keywords(overwrite=false, &block) return nil unless block_given? raise(InvalidDullKeywordsType, InvalidDullKeywordsType.message) unless block.call.is_a?(Array) overwrite ? @@dull_keywords = block.call : @@dull_keywords = (self.dull_keywords << block.call) self.dull_keywords end
returns a hash with the keys as the models to be searched and the values as arrays of columns for the respective model
Example:
$> Setup.settings $> => {"users"=>[:first_name, :last_name, :email], "projects"=>[:title, :description]}
# File lib/easy_search/setup.rb, line 10 def table_settings @@table_settings ||= HashWithIndifferentAccess.new end