Class: Interage::ApplicationQuery

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

Direct Known Subclasses

ApplicationQuery

Constant Summary collapse

PER_PAGE =
50

Instance Method Summary collapse

Constructor Details

#initializeApplicationQuery

Returns a new instance of ApplicationQuery.



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

def initialize
  init_relation
end

Instance Method Details

#allObject



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

def all
  includes.relation
end

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



50
51
52
53
54
55
56
57
58
59
# File 'lib/interage/application_query.rb', line 50

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/interage/application_query.rb', line 28

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

#by_id(id) ⇒ Object



61
62
63
64
65
# File 'lib/interage/application_query.rb', line 61

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

  self
end

#decorateObject



71
72
73
74
75
# File 'lib/interage/application_query.rb', line 71

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

  self
end

#distinctObject



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

def distinct
  @relation = relation.distinct

  self
end

#find(id) ⇒ Object



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

def find(id)
  all.find_by(id: id)
end

#includesObject



67
68
69
# File 'lib/interage/application_query.rb', line 67

def includes
  self
end

#init_relationObject

Raises:

  • (NotImplementedError)


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

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

#paginate(page = 1) ⇒ Object



36
37
38
# File 'lib/interage/application_query.rb', line 36

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

#search_ilike_for(colums, term) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/interage/application_query.rb', line 40

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