Class: OWS::ApplicationQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/ows/application_query.rb

Direct Known Subclasses

ApplicationQuery

Constant Summary collapse

DEFAULT_PER_PAGE =
50

Instance Method Summary collapse

Constructor Details

#initializeApplicationQuery

Returns a new instance of ApplicationQuery.



10
11
12
# File 'lib/ows/application_query.rb', line 10

def initialize
  init_relation
end

Instance Method Details

#allObject



18
19
20
# File 'lib/ows/application_query.rb', line 18

def all
  includes.relation
end

#between_dates(column, start_date, finish_date = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/ows/application_query.rb', line 46

def between_dates(column, start_date, finish_date = nil)
  start_date = Date.current if start_date.blank?
  finish_date = start_date if finish_date.blank?
  range_date =
    start_date.to_date.beginning_of_day..finish_date.to_date.end_of_day

  @relation = relation.where(column => range_date)

  self
end

#build(attributes = {}) ⇒ Object



28
29
30
# File 'lib/ows/application_query.rb', line 28

def build(attributes = {})
  all.new(attributes)
end

#by_id(id) ⇒ Object



57
58
59
60
61
# File 'lib/ows/application_query.rb', line 57

def by_id(id)
  @relation = relation.where(id: id) if id.present?

  self
end

#decorateObject



67
68
69
70
71
# File 'lib/ows/application_query.rb', line 67

def decorate
  @relation = ActiveDecorator::Decorator.instance.decorate(relation)

  self
end

#distinctObject



22
23
24
25
26
# File 'lib/ows/application_query.rb', line 22

def distinct
  @relation = relation.distinct

  self
end

#includesObject



63
64
65
# File 'lib/ows/application_query.rb', line 63

def includes
  self
end

#init_relationObject

Raises:

  • (NotImplementedError)


14
15
16
# File 'lib/ows/application_query.rb', line 14

def init_relation
  raise NotImplementedError, 'subclass did not define #init_relation'
end

#paginate(page = 1) ⇒ Object



32
33
34
# File 'lib/ows/application_query.rb', line 32

def paginate(page = 1)
  all.page(page).per(current_per_page)
end

#search_ilike_for(colums, term) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/ows/application_query.rb', line 36

def search_ilike_for(colums, term)
  return self unless term

  params = { t: "%#{term.to_s.downcase}%" }
  colums = colums.map { |colum| "unaccent(#{colum}) ILIKE unaccent(:t)" }
  @relation = relation.where(colums.join(' OR '), params)

  self
end