Class: Activity::ResourceList
- Inherits:
-
BaseService
- Object
- BaseService
- Activity::ResourceList
- Defined in:
- app/services/activity/resource_list.rb
Overview
Service object: resource list.
Defined Under Namespace
Classes: BaseFormatter, CreditMemoFormatter, CreditMemoRetriever, InvoiceFormatter, InvoiceRetriever, OpportunityFormatter, OpportunityRetriever, OrderFormatter, OrderRetriever, PurchaseOrderFormatter, PurchaseOrderRetriever, QuoteFormatter, QuoteRetriever, ResourceRetriever, RmaFormatter, RmaRetriever, RoomConfigurationFormatter, RoomConfigurationRetriever, SupportCaseFormatter, SupportCaseRetriever
Instance Attribute Summary collapse
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#party ⇒ Object
readonly
Returns the value of attribute party.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Attributes inherited from BaseService
Class Method Summary collapse
-
.build_formatter(r) ⇒ BaseFormatter
Picks the formatter for a resource, falling back to BaseFormatter.
Instance Method Summary collapse
-
#process(party, options = {}) ⇒ Hash
Grouped results with pagination info.
Methods inherited from BaseService
#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger
Constructor Details
This class inherits a constructor from BaseService
Instance Attribute Details
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
5 6 7 |
# File 'app/services/activity/resource_list.rb', line 5 def filters @filters end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
5 6 7 |
# File 'app/services/activity/resource_list.rb', line 5 def page @page end |
#party ⇒ Object (readonly)
Returns the value of attribute party.
5 6 7 |
# File 'app/services/activity/resource_list.rb', line 5 def party @party end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
5 6 7 |
# File 'app/services/activity/resource_list.rb', line 5 def per_page @per_page end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
5 6 7 |
# File 'app/services/activity/resource_list.rb', line 5 def query @query end |
Class Method Details
.build_formatter(r) ⇒ BaseFormatter
Picks the formatter for a resource, falling back to BaseFormatter.
60 61 62 63 64 65 66 67 68 |
# File 'app/services/activity/resource_list.rb', line 60 def self.build_formatter(r) formatter_class = begin "Activity::ResourceList::#{r.class.name}Formatter".constantize rescue StandardError nil end formatter = formatter_class.new(r) if formatter_class formatter || Activity::ResourceList::BaseFormatter.new(r) end |
Instance Method Details
#process(party, options = {}) ⇒ Hash
Returns grouped results with pagination info.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/services/activity/resource_list.rb', line 14 def process(party, = {}) @party = party @page = [:page].to_i @page = 1 if @page < 1 @per_page = ([:per_page] || 20).to_i @per_page = 20 if @per_page < 1 @filters = [:filters] @query = [:query] # supplier don't have the rest @filters = %w[purchase_orders] if party.is_a?(Supplier) resources = %i[opportunities orders quotes room_configurations support_cases invoices rmas credit_memos purchase_orders] resources &= @filters.map(&:to_sym) if @filters.present? results = [] total_count = 0 total_displayed = 0 resources.each do |resource_type| retriever = ResourceRetriever.build(resource_type, party: party, query: query, page: page, per_page: per_page) values = retriever.resources next if values.blank? option_group_text = resource_type.to_s.humanize date_range = values.filter_map(&:created_at) range_start = date_range.min&.to_fs(:crm_date_only) range_end = date_range.max&.to_fs(:crm_date_only) option_group_text << " from #{range_start} until #{range_end}" # Get the total count from the retriever before limit/offset was applied total_count += retriever.total_count total_displayed += values.size results << { text: option_group_text, children: values.map do |r| formatter = self.class.build_formatter(r) { text: formatter.display, id: formatter.identifier } end } end more = total_displayed < total_count { results: results, pagination: { more: more } } end |