Class: Report::BaseCommand

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::API, ActiveModel::Attributes, ActiveModel::Validations, ActiveModel::Validations::Callbacks, Normalizr::Concern, StripAttributes
Defined in:
app/services/report/base_command.rb

Overview

Command pattern to encapsulate parameters necessary to run a presence report

Direct Known Subclasses

AccountingRecords::AccountingRecordsCommand, AccountsPayableCommand, AccountsReceivableCommand, ActivityPerformance::Command, Report::BalanceSheet::BalanceSheetCommand, Report::BookOfBusiness::BookOfBusinessCommand, CallBreakdown::Command, CallStatistics::Command, CampaignsReport::CampaignsReportCommand, CashFlow::CashFlowCommand, ChannelRevenue::Command, ConversionReport::ConversionReportCommand, CostCenter::CostCenterCommand, CouponSalesReport::CouponSalesReportCommand, CustomerPerformanceCommand, GrossSalesReport::GrossSalesReportCommand, InventoryPlanningCommand, ItemMovedToScrap::ItemMovedToScrapReportCommand, ItemSale::ItemSaleCommand, ItemsSpecMatrix::Command, KpiCall::KpiCallCommand, KpiTimeOnTask::KpiTimeOnTaskCommand, LeadReport::LeadReportCommand, MissedCalls::MissedCallsCommand, ObsoleteItemsReport::Command, OpportunitiesReport::OpportunitiesReportCommand, OpportunitiesVsLogistics::OpportunitiesVsLogisticsCommand, OrdersOriginReport::OrdersReportCommand, PhoneQueueReportCommand, PresenceReport::PresenceCommand, ProfileDiscount::ProfileDiscountCommand, ProfitLoss::ProfitLossCommand, Reconciliation::ReconciliationCommand, RmasReport::RmasReportCommand, SalesCommissions::SalesCommissionsCommand, SalesProfit::SalesProfitCommand, SalesRepRanking::SalesRepRankingCommand, ShippingRates::ShippingRatesCommand, SmartServices::SmartServicesCommand, SourcesReport::SourcesReportCommand, TechCallsReport::TechCallsReportCommand, TechProductivity::TechProductivityCommand, WebSitePerformance::WebSitePerformanceCommand

Constant Summary collapse

ARRAY_TYPE_NAMES =

Virtus supported default: :method_name to call an instance method as the
default value. ActiveModel::Attributes does not; it stores the symbol
literally. This initializer resolves symbol defaults that map to real
instance methods so the Virtus-style default: :default_date_start pattern
keeps working without touching every subclass.

It also normalizes compacted-array attributes that have no explicit default.
ActiveModel::Attribute::WithCastValue bypasses type.cast for nil defaults, so
attribute :x, :compacted_array (no default) returns nil instead of [].

%i[compacted_array compacted_int_array].to_set.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ BaseCommand

Returns a new instance of BaseCommand.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/services/report/base_command.rb', line 22

def initialize(attrs = {})
  super
  self.class._default_attributes.each_value do |attr_def|
    raw = attr_def.value_before_type_cast
    name = attr_def.name

    # Resolve symbol defaults that name instance methods (Virtus compat).
    # We compare value_before_type_cast rather than the cast value because
    # custom types (e.g. :compacted_array) transform the symbol and the cast
    # result no longer equals `raw`.
    if raw.is_a?(Symbol) && respond_to?(raw, true) &&
       @attributes[name]&.value_before_type_cast == raw
      send(:"#{name}=", send(raw))
      next
    end

    # Seed nil array-typed attributes to [] so callers never hit nil.
    if ARRAY_TYPE_NAMES.include?(attr_def.type.type) && send(name).nil?
      send(:"#{name}=", [])
    end
  end
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



52
53
54
# File 'app/services/report/base_command.rb', line 52

def results
  @results
end

Instance Method Details

#attributesObject

Report service classes pass attributes to query helpers which access keys
with symbols (legacy Virtus behaviour). ActiveModel::Attributes returns a
plain String-keyed hash, so we expose an indifferent version.



48
49
50
# File 'app/services/report/base_command.rb', line 48

def attributes
  super.with_indifferent_access
end

#email_parametersObject



62
63
64
65
66
67
68
# File 'app/services/report/base_command.rb', line 62

def email_parameters
  {
    from: 'heatwaveteam@warmlyyours.com',
    to: 'cbillen@warmlyyours.com',
    subject: 'Presence Report'
  }
end

#email_partialObject



58
59
60
# File 'app/services/report/base_command.rb', line 58

def email_partial
  '/crm/reports/presence/results'
end

#executeObject



54
55
56
# File 'app/services/report/base_command.rb', line 54

def execute
  raise "Must implement execute method"
end