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_query(records) ⇒ Object
- #records_without_pagination ⇒ Object
- #records_without_pagination_and_scope ⇒ Object
- #scope_query(records, scope = @query_form.scope) ⇒ Object
- #scopes_number ⇒ Object
- #search_query(records) ⇒ 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.
4 5 6 |
# File 'app/services/carnival/query_service.rb', line 4 def total_records @total_records end |
Instance Method Details
#advanced_search_query(records) ⇒ Object
73 74 75 76 77 78 79 |
# File 'app/services/carnival/query_service.rb', line 73 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
52 53 54 55 56 57 58 59 |
# File 'app/services/carnival/query_service.rb', line 52 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 != "") records.where("#{@presenter.table_name}.#{date_filter_field.name} between ? and ?", "#{@query_form.date_period_from} 00:00:00", "#{@query_form.date_period_to} 23:59:59") 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
89 90 91 92 93 94 95 |
# File 'app/services/carnival/query_service.rb', line 89 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
85 86 87 |
# File 'app/services/carnival/query_service.rb', line 85 def order_query(records) records.order("#{sort_column} #{sort_direction}") end |
#page_query(records) ⇒ Object
81 82 83 |
# File 'app/services/carnival/query_service.rb', line 81 def page_query(records) records.paginate(page: @query_form.page, per_page: @presenter.items_per_page) end |
#records_without_pagination ⇒ Object
27 28 29 |
# File 'app/services/carnival/query_service.rb', line 27 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 25 |
# File 'app/services/carnival/query_service.rb', line 18 def records_without_pagination_and_scope records = @model records = date_period_query(records) records = search_query(records) records = advanced_search_query(records) records = order_query(records) includes_relations(records) end |
#scope_query(records, scope = @query_form.scope) ⇒ Object
44 45 46 47 48 49 50 |
# File 'app/services/carnival/query_service.rb', line 44 def scope_query(records, scope = @query_form.scope) if(scope.present? && scope.to_sym != :all) records = records.send(scope) else records end end |
#scopes_number ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'app/services/carnival/query_service.rb', line 35 def scopes_number records = records_without_pagination_and_scope scopes = {} @presenter.scopes.each do |key, index| scopes[key] = scope_query(records, key).size end scopes end |
#search_query(records) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/services/carnival/query_service.rb', line 61 def search_query(records) if @query_form.search_term.present? and @presenter.searchable_fields.size > 0 filters = @presenter.searchable_fields.map do |key, field| " #{key.to_s} like :search" end records = includes_relations(records) if @should_include_relation records.where(filters.join(" or "), search: "%#{@query_form.search_term}%") else records end end |
#sort_column ⇒ Object
97 98 99 100 101 |
# File 'app/services/carnival/query_service.rb', line 97 def sort_column column = @query_form.sort_column sorter = Carnival::GenericDatatable::ColumnSorterCreator.create_sorter(@presenter, column) sorter.build_sort_string end |
#sort_direction ⇒ Object
103 104 105 |
# File 'app/services/carnival/query_service.rb', line 103 def sort_direction @query_form.sort_direction end |