Module: SearchAble
- Extended by:
- ActiveSupport::Concern
- Included in:
- AuditLog, Cron::Tab, Notification
- Defined in:
- lib/app/models/concerns/search_able.rb
Overview
Public: Add search and sort text to an object
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#after_search_text ⇒ Object
Place holder to call to allow for work to be done after we gather up search text fields.
-
#before_search_text ⇒ Object
Place holder to call to allow for work to be done before we gather up search text fields.
- #method_missing(method, *args) ⇒ Object
- #respond_to?(method, include_private = false) ⇒ Boolean
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#search_fields ⇒ Object
Internal: Which fields to add to search text.
-
#sort_fields ⇒ Object
Internal: Which fields to add to sort on.
-
#update_search_and_sort_text ⇒ Object
Internal: Update the search and sort text.
-
#update_text(fields, field_to_update) ⇒ Object
Internal: Update the search text.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/app/models/concerns/search_able.rb', line 45 def method_missing(method, *args) if method.to_s.start_with? 'sorted_' send(method.to_s.sub(/^sorted_/, '')).asc(:sort_text) else super end end |
Class Method Details
.included(base) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/app/models/concerns/search_able.rb', line 9 def self.included(base) base.class_eval do # # Fields # field :search_text, type: String field :sort_text, type: String # # Call backs # before_save :update_search_and_sort_text index({ search_text: Mongo::Index::ASCENDING }, { background: true }) index({ sort_text: Mongo::Index::ASCENDING }, { background: true }) end end |
Instance Method Details
#after_search_text ⇒ Object
Place holder to call to allow for work to be done after we gather up search text fields
69 |
# File 'lib/app/models/concerns/search_able.rb', line 69 def after_search_text; end |
#before_search_text ⇒ Object
Place holder to call to allow for work to be done before we gather up search text fields
64 |
# File 'lib/app/models/concerns/search_able.rb', line 64 def before_search_text; end |
#respond_to?(method, include_private = false) ⇒ Boolean
57 58 59 |
# File 'lib/app/models/concerns/search_able.rb', line 57 def respond_to?(method, include_private = false) super || method.to_s.start_with?('sorted_') end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
53 54 55 |
# File 'lib/app/models/concerns/search_able.rb', line 53 def respond_to_missing?(method_name, include_private = false) super || method_name.to_s.start_with?('sorted_') end |
#search_fields ⇒ Object
Internal: Which fields to add to search text
Examples
search_fields
# => ['name', 'email', 'code']
Return which fields should be added to search
122 123 124 |
# File 'lib/app/models/concerns/search_able.rb', line 122 def search_fields %w[name] end |
#sort_fields ⇒ Object
Internal: Which fields to add to sort on
Examples
sort_fields
# => ['name', 'email']
Return which fields should be added to sort
136 137 138 |
# File 'lib/app/models/concerns/search_able.rb', line 136 def sort_fields search_fields end |
#update_search_and_sort_text ⇒ Object
Internal: Update the search and sort text
Call before validation to update, changes are persisted with the object.
76 77 78 79 80 81 82 83 |
# File 'lib/app/models/concerns/search_able.rb', line 76 def update_search_and_sort_text return if destroyed? before_search_text update_text(search_fields, :search_text) update_text(sort_fields, :sort_text) after_search_text end |
#update_text(fields, field_to_update) ⇒ Object
Internal: Update the search text
Examples
update_search_text
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/app/models/concerns/search_able.rb', line 92 def update_text(fields, field_to_update) items = fields.reject { |field| send(field.to_sym).blank? }.collect do |field| value = send(field.to_sym) case value when String value.downcase when Integer, Float value.to_s.rjust(4, '0') when Array value.empty? ? nil : value.join(' ').downcase when Hash value.inspect else value.to_s end end send "#{field_to_update}=", items.compact.join(' ') end |