Class: Carnival::QueryService
- Inherits:
-
Object
- Object
- Carnival::QueryService
- Defined in:
- app/services/carnival/query_service.rb
Instance Attribute Summary collapse
-
#total_records ⇒ Object
Returns the value of attribute total_records.
Instance Method Summary collapse
- #advanced_search_query(records) ⇒ Object
- #date_period_query(records) ⇒ Object
- #get_query ⇒ Object
- #includes_relations(records) ⇒ Object
-
#initialize(model, presenter, query_form) ⇒ QueryService
constructor
A new instance of QueryService.
- #order_query(records) ⇒ Object
- #page_count ⇒ Object
- #page_query(records) ⇒ Object
- #records_without_pagination ⇒ Object
- #records_without_pagination_and_scope ⇒ Object
- #scope_query(records, scope = @query_form.scope) ⇒ Object
- #scopes_number ⇒ Object
- #sort_column ⇒ Object
- #sort_direction ⇒ Object
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_records ⇒ Object
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
65 66 67 68 69 70 71 |
# File 'app/services/carnival/query_service.rb', line 65 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
54 55 56 57 58 59 60 61 62 63 |
# File 'app/services/carnival/query_service.rb', line 54 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_query ⇒ Object
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
81 82 83 84 85 86 87 |
# File 'app/services/carnival/query_service.rb', line 81 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
77 78 79 |
# File 'app/services/carnival/query_service.rb', line 77 def order_query(records) records.order("#{sort_column} #{sort_direction}") end |
#page_count ⇒ Object
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
73 74 75 |
# File 'app/services/carnival/query_service.rb', line 73 def page_query(records) records.paginate(page: @query_form.page, per_page: @presenter.items_per_page) end |
#records_without_pagination ⇒ Object
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_scope ⇒ Object
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
46 47 48 49 50 51 52 |
# File 'app/services/carnival/query_service.rb', line 46 def scope_query(records, scope = @query_form.scope) if scope.present? && scope.to_sym != :all records.send(scope) else records end end |
#scopes_number ⇒ Object
38 39 40 41 42 43 44 |
# 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_column ⇒ Object
89 90 91 92 93 |
# File 'app/services/carnival/query_service.rb', line 89 def sort_column column = @query_form.sort_column sorter = Carnival::GenericDatatable::ColumnSorterCreator.create_sorter(@presenter, column) sorter.build_sort_string end |
#sort_direction ⇒ Object
95 96 97 |
# File 'app/services/carnival/query_service.rb', line 95 def sort_direction @query_form.sort_direction end |