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

Instance Method Details

#authorized?(result) ⇒ Boolean

View helper

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/concerns/effective_search_search.rb', line 81

def authorized?(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.authorized?(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_pageObject



38
39
40
# File 'app/models/concerns/effective_search_search.rb', line 38

def per_page
  24
end

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

Returns:

  • (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

#scrubsObject

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_contentsObject

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_resultsObject

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_typesObject

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_sObject



23
24
25
# File 'app/models/concerns/effective_search_search.rb', line 23

def to_s
  term.to_s
end

#unsearchable_typesObject

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