Module: Searchtastic::ActiveRecord
- Defined in:
- lib/searchtastic.rb
Instance Method Summary collapse
-
#fields ⇒ Object
Array of fields upon which it’s possible to search.
-
#is_searchable? ⇒ Boolean
Callable on an instance or the call itself to detect whether .search() will have meaningful results.
-
#search(filter) ⇒ Object
Called on an ActiveRelation containing the model.
-
#searchable_by(*fields) ⇒ Object
Sets the searchable fields for a given model class User < ActiveRecord::Base search_fields %w(name email bio) end.
Instance Method Details
#fields ⇒ Object
Array of fields upon which it’s possible to search
9 10 11 |
# File 'lib/searchtastic.rb', line 9 def fields @fields || [] end |
#is_searchable? ⇒ Boolean
Callable on an instance or the call itself to detect whether .search() will have meaningful results
User.search(@filter) if User.is_searchable?
41 42 43 |
# File 'lib/searchtastic.rb', line 41 def is_searchable? self.fields.count > 0 end |
#search(filter) ⇒ Object
Called on an ActiveRelation containing the model
filter = "Pete"
@filtered_models = User.search(filter)
27 28 29 30 31 32 33 34 35 |
# File 'lib/searchtastic.rb', line 27 def search filter if filter && is_searchable? filter = [filter, Chronic.parse(filter).strftime("%Y-%m-%d")] rescue filter handle_joins(self.fields, scoped.select("DISTINCT(`#{self.table_name}`.`id`), `#{self.table_name}`.*")) .where(build_filter(filter, self.fields)) else scoped end end |
#searchable_by(*fields) ⇒ Object
Sets the searchable fields for a given model
class User < ActiveRecord::Base
search_fields %w(name email bio)
end
18 19 20 |
# File 'lib/searchtastic.rb', line 18 def searchable_by *fields self.fields = process_fields(fields) end |