Class: Report::BaseCommand

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::API, ActiveModel::Attributes, ActiveModel::Validations, ActiveModel::Validations::Callbacks, Heatwave::AttributeNormalizing, 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

Methods included from Heatwave::AttributeNormalizing

normalize

Constructor Details

#initialize(attrs = {}) ⇒ BaseCommand

Returns a new instance of BaseCommand.



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 24

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.
    send(:"#{name}=", []) if ARRAY_TYPE_NAMES.include?(attr_def.type.type) && send(name).nil?
  end
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



64
65
66
# File 'app/services/report/base_command.rb', line 64

def results
  @results
end

Instance Method Details

#[](key) ⇒ Object?

Hash-style attribute access (Virtus carryover). The 7 report templates
under app/views/crm/reports/* read @report_command[:company_ids] etc.
from when these were Virtus models; ActiveModel::Attributes models don't
expose [], which made every report 500 (see AppSignal #4880, #4878).

Parameters:

  • key (Symbol, String)

    Attribute name (symbol/string both accepted
    thanks to the indifferent-access #attributes override above).

Returns:

  • (Object, nil)

    The attribute value, or nil if the key is unknown.



60
61
62
# File 'app/services/report/base_command.rb', line 60

def [](key)
  attributes[key]
end

#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



74
75
76
77
78
79
80
# File 'app/services/report/base_command.rb', line 74

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

#email_partialObject



70
71
72
# File 'app/services/report/base_command.rb', line 70

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

#executeObject



66
67
68
# File 'app/services/report/base_command.rb', line 66

def execute
  raise "Must implement execute method"
end