Module: EffectiveSearchSearch
- Extended by:
- ActiveSupport::Concern
- Included in:
- Effective::Search
- Defined in:
- app/models/concerns/effective_search_search.rb
Overview
EffectiveSearchSearch Mark your search model with include EffectiveSearchSearch
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#authorized?(result) ⇒ Boolean
View helper.
-
#body(result) ⇒ Object
View helper.
-
#path(result) ⇒ Object
View helper.
- #per_page ⇒ Object
-
#permalinks ⇒ Object
This is Set #2 of results we display on the search page.
- #present? ⇒ Boolean
-
#results(page: nil) ⇒ Object
The paginated results.
-
#scrubs ⇒ Object
Scrub the body content with these regexes.
-
#search! ⇒ Object
Search and assigns the collection Assigns the entire collection() if there are no search terms Otherwise validate the search terms.
-
#search_contents ⇒ Object
This is Set #1 of results we display on the search page.
-
#search_results ⇒ Object
The unpaginated results of the search.
-
#searchable_types ⇒ Object
Limit the search to certain searchable types.
-
#title(result) ⇒ Object
View helper.
- #to_s ⇒ Object
-
#unsearchable_types ⇒ Object
These classes should not be searched.
Instance Method Details
#authorized?(result) ⇒ Boolean
View helper
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/models/concerns/effective_search_search.rb', line 81 def (result) searchable = result.try(:searchable) || result return false if searchable.blank? return false if searchable.try(:draft?) return false if searchable.try(:archived?) if view_context.present? return EffectiveResources.(view_context, :show, searchable) end true end |
#body(result) ⇒ Object
View helper
106 107 108 109 110 |
# File 'app/models/concerns/effective_search_search.rb', line 106 def body(result) content = (result.try(:pg_search_highlight) || result.try(:rich_text_body) || result).to_s scrubs.each { |pattern| content.gsub!(pattern, '') } content.html_safe end |
#path(result) ⇒ Object
View helper
101 102 103 |
# File 'app/models/concerns/effective_search_search.rb', line 101 def path(result) effective_path(result) || polymorphic_path(result) || raise("unknown path for #{result.searchable_type}") end |
#per_page ⇒ Object
38 39 40 |
# File 'app/models/concerns/effective_search_search.rb', line 38 def per_page 24 end |
#permalinks ⇒ Object
This is Set #2 of results we display on the search page
62 63 64 65 |
# File 'app/models/concerns/effective_search_search.rb', line 62 def permalinks return [] unless present? @permalinks ||= Effective::Resource.new(Effective::Permalink.deep).search_any(term, columns: [:title, :summary]) end |
#present? ⇒ Boolean
42 43 44 |
# File 'app/models/concerns/effective_search_search.rb', line 42 def present? term.present? end |
#results(page: nil) ⇒ Object
The paginated results
73 74 75 76 77 78 |
# File 'app/models/concerns/effective_search_search.rb', line 73 def results(page: nil) page = (page || 1).to_i offset = [(page - 1), 0].max * per_page search_results.limit(per_page).offset(offset) end |
#scrubs ⇒ Object
Scrub the body content with these regexes
113 114 115 116 117 118 119 |
# File 'app/models/concerns/effective_search_search.rb', line 113 def scrubs [ /(<p>)?\[posts\scategory=\"(.*?)\"\](<\/p>)?/, /(<p>)?\[permalinks\stag=\"(.*?)\"\](<\/p>)?/, /\[picflow\s+gallery="[^"]*"\]/ ] end |
#search! ⇒ Object
Search and assigns the collection Assigns the entire collection() if there are no search terms Otherwise validate the search terms
49 50 51 52 53 |
# File 'app/models/concerns/effective_search_search.rb', line 49 def search! @search_results = build_collection() @search_results = @search_results.none if present? && !valid? @search_results end |
#search_contents ⇒ Object
This is Set #1 of results we display on the search page
56 57 58 59 |
# File 'app/models/concerns/effective_search_search.rb', line 56 def search_contents return [] unless present? @search_contents ||= Effective::Resource.new(Effective::SearchContent.deep).search_any(term, columns: [:keywords]) end |
#search_results ⇒ Object
The unpaginated results of the search
68 69 70 |
# File 'app/models/concerns/effective_search_search.rb', line 68 def search_results @search_results || [] end |
#searchable_types ⇒ Object
Limit the search to certain searchable types. Return nil for all ['Effective::Page', 'Effective::Post']
29 30 31 |
# File 'app/models/concerns/effective_search_search.rb', line 29 def searchable_types nil end |
#title(result) ⇒ Object
View helper
96 97 98 |
# File 'app/models/concerns/effective_search_search.rb', line 96 def title(result) (result.try(:searchable).try(:title) || result.try(:title) || result).to_s.html_safe end |
#to_s ⇒ Object
23 24 25 |
# File 'app/models/concerns/effective_search_search.rb', line 23 def to_s term.to_s end |
#unsearchable_types ⇒ Object
These classes should not be searched
34 35 36 |
# File 'app/models/concerns/effective_search_search.rb', line 34 def unsearchable_types ['Effective::Permalink', 'Effective::SearchContent'] end |