Class: Effective::EventSearch

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/effective/event_search.rb

Constant Summary collapse

ORDERS =
['Sort by Published Date', 'Sort by Event Date']

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#categoryObject

Returns the value of attribute category.



11
12
13
# File 'app/models/effective/event_search.rb', line 11

def category
  @category
end

#current_userObject

Returns the value of attribute current_user.



7
8
9
# File 'app/models/effective/event_search.rb', line 7

def current_user
  @current_user
end

#orderObject

Returns the value of attribute order.



12
13
14
# File 'app/models/effective/event_search.rb', line 12

def order
  @order
end

#termObject

Returns the value of attribute term.



10
11
12
# File 'app/models/effective/event_search.rb', line 10

def term
  @term
end

#unpublishedObject

Returns the value of attribute unpublished.



8
9
10
# File 'app/models/effective/event_search.rb', line 8

def unpublished
  @unpublished
end

Instance Method Details

#collectionObject

Base collection to search.



18
19
20
# File 'app/models/effective/event_search.rb', line 18

def collection
  Event.events(user: current_user, unpublished: unpublished).upcoming
end

#eventsObject

The unpaginated results of the search



40
41
42
# File 'app/models/effective/event_search.rb', line 40

def events
  @events || collection
end

#per_pageObject



22
23
24
# File 'app/models/effective/event_search.rb', line 22

def per_page
  (EffectiveEvents.per_page || 24).to_i
end

#present?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'app/models/effective/event_search.rb', line 26

def present?
  term.present? || category.present? || order.present?
end

#results(page: nil) ⇒ Object

The paginated results



45
46
47
48
49
50
# File 'app/models/effective/event_search.rb', line 45

def results(page: nil)
  page = (page || 1).to_i
  offset = [(page - 1), 0].max * per_page

  events.limit(per_page).offset(offset)
end

#search!Object

Search and assigns the collection Assigns the entire collection() if there are no search terms Otherwise validate the search terms



33
34
35
36
37
# File 'app/models/effective/event_search.rb', line 33

def search!
  @events = build_collection()
  @events = @events.none if present? && !valid?
  @events
end