Class: Workarea::WishList::PublicSearch

Inherits:
Object
  • Object
show all
Defined in:
app/models/workarea/wish_list/public_search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, location = '') ⇒ PublicSearch

Returns a new instance of PublicSearch.



5
6
7
8
# File 'app/models/workarea/wish_list/public_search.rb', line 5

def initialize(query, location = '')
  @query    = query
  @location = location
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



3
4
5
# File 'app/models/workarea/wish_list/public_search.rb', line 3

def location
  @location
end

#queryObject (readonly)

Returns the value of attribute query.



3
4
5
# File 'app/models/workarea/wish_list/public_search.rb', line 3

def query
  @query
end

Instance Method Details

#resultsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/workarea/wish_list/public_search.rb', line 10

def results
  regex = /^#{Regexp.quote(query)}/i
  criteria = [
    { name: regex },
    { first_name: regex },
    { last_name: regex },
    { email: query } # we don't want partial matching on email
  ]

  if location.present?
    loc_regex = /.*#{Regexp.quote(location)}.*/i
    Workarea::WishList.where(
      privacy: 'public',
      location: loc_regex,
      '$or' => criteria
    )
  else
    Workarea::WishList.where(
      privacy: 'public',
      '$or' => criteria
    )
  end.named
end