Class: Carnival::QueryService

Inherits:
Object
  • Object
show all
Defined in:
app/services/carnival/query_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, presenter, query_form) ⇒ QueryService

Returns a new instance of QueryService.



5
6
7
8
9
10
11
# File 'app/services/carnival/query_service.rb', line 5

def initialize(model, presenter, query_form)
  @model = model
  @presenter = presenter
  @query_form = query_form
  @total_records = 0
  @should_include_relation = !@model.is_a?(ActiveRecord::Relation)
end

Instance Attribute Details

#total_recordsObject

Returns the value of attribute total_records.



3
4
5
# File 'app/services/carnival/query_service.rb', line 3

def total_records
  @total_records
end

Instance Method Details

#advanced_search_query(records) ⇒ Object



64
65
66
67
68
69
70
# File 'app/services/carnival/query_service.rb', line 64

def advanced_search_query(records)
  if @query_form.advanced_search.present?
    @presenter.parse_advanced_search(records, @query_form.advanced_search)
  else
    records
  end
end

#date_period_query(records) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'app/services/carnival/query_service.rb', line 53

def date_period_query(records)
  date_filter_field = @presenter.date_filter_field
  if date_filter_field.present? && @query_form.date_period_from.present? && @query_form.date_period_from != "" && @query_form.date_period_to.present? && @query_form.date_period_to != ""
    from = DateTime.parse(@query_form.date_period_from).beginning_of_day
    to = DateTime.parse(@query_form.date_period_to).end_of_day
    records.where(@presenter.date_filter_field.name.to_sym => [from..to])
  else
    records
  end
end

#get_queryObject



13
14
15
16
# File 'app/services/carnival/query_service.rb', line 13

def get_query
  records = records_without_pagination
  page_query(records)
end

#includes_relations(records) ⇒ Object



80
81
82
83
84
85
86
# File 'app/services/carnival/query_service.rb', line 80

def includes_relations(records)
  if @should_include_relation and @presenter.join_tables.size > 0
    records.includes(*@presenter.join_tables)
  else
    records
  end
end

#order_query(records) ⇒ Object



76
77
78
# File 'app/services/carnival/query_service.rb', line 76

def order_query(records)
  records.order("#{sort_column} #{sort_direction}")
end

#page_countObject



26
27
28
# File 'app/services/carnival/query_service.rb', line 26

def page_count
  (total_records / @presenter.items_per_page.to_f).ceil
end

#page_query(records) ⇒ Object



72
73
74
# File 'app/services/carnival/query_service.rb', line 72

def page_query(records)
  records.paginate(page: @query_form.page, per_page: @presenter.items_per_page)
end

#records_without_paginationObject



30
31
32
# File 'app/services/carnival/query_service.rb', line 30

def records_without_pagination
  scope_query records_without_pagination_and_scope
end

#records_without_pagination_and_scopeObject



18
19
20
21
22
23
24
# File 'app/services/carnival/query_service.rb', line 18

def records_without_pagination_and_scope
  records = @model
  records = date_period_query(records)
  records = advanced_search_query(records)
  records = order_query(records)
  includes_relations(records)
end

#scope_query(records, scope = @query_form.scope) ⇒ Object



45
46
47
48
49
50
51
# File 'app/services/carnival/query_service.rb', line 45

def scope_query(records, scope = @query_form.scope)
  if scope.present? && scope.to_sym != :all
    records.send(scope)
  else
    records
  end
end

#scopes_numberObject



38
39
40
41
42
43
# File 'app/services/carnival/query_service.rb', line 38

def scopes_number
  records = records_without_pagination_and_scope
  Hash[@presenter.scopes.keys.map do |key|
    [key, scope_query(records, key).size]
  end]
end

#sort_columnObject



88
89
90
91
92
# File 'app/services/carnival/query_service.rb', line 88

def sort_column
  column = @query_form.sort_column
  sorter = Carnival::GenericDatatable::ColumnSorterCreator.create_sorter(@presenter, column)
  sorter.build_sort_string
end

#sort_directionObject



94
95
96
# File 'app/services/carnival/query_service.rb', line 94

def sort_direction
  @query_form.sort_direction
end