Module: Spree::Reporting

Defined in:
lib/spree/reporting.rb,
lib/spree/reporting/query.rb,
lib/spree/reporting/result.rb,
lib/spree/reporting/schema.rb,
lib/spree/reporting/counters.rb,
lib/spree/reporting/registry.rb,
lib/spree/reporting/hydration.rb,
lib/spree/reporting/adapters/base.rb,
lib/spree/reporting/adapters/live.rb,
lib/spree/reporting/counter_result.rb,
lib/spree/reporting/default_vocabulary.rb

Overview

Semantic reporting layer (see docs/plans/6.0-analytics-semantic-layer.md).

Developers extend the vocabulary through the registry:

Spree.reporting.metric :wholesale_margin, sql: '...', base: :line_items, format: :money
Spree.reporting.dimension :warehouse, base: :orders, column: :stock_location_id, lookup: 'Spree::StockLocation'
Spree.reporting.counter :orders_on_hold, count: ->(store, channel:) { store.orders.on_hold.count }

Consumers (Admin API, dashboard, saved reports, AI tools) compose queries against registered names only — see Spree::Reporting::Query.

Defined Under Namespace

Modules: Adapters, DefaultVocabulary Classes: Base, Counter, CounterResult, Counters, Dimension, Hydration, InvalidQuery, Metric, Query, Registry, Result, Schema, UnknownMember

Class Method Summary collapse

Class Method Details

.image_url(media) ⇒ String?

Public URL for a media row a hydrated dimension carries, or nil when the record has no file. Mirrors what the API serializers render.

Parameters:

Returns:

  • (String, nil)


29
30
31
32
33
34
# File 'lib/spree/reporting.rb', line 29

def self.image_url(media)
  attachment = media.respond_to?(:attachment) ? media.attachment : media
  return nil unless attachment.respond_to?(:attached?) && attachment.attached?

  Rails.application.routes.url_helpers.cdn_image_url(attachment)
end

.join_paths(joins, prefix = []) ⇒ Array<Array<Symbol>>

Every association path a joins spec reaches, in ActiveRecord's joins format: [{ variant: { product: :product_categories } }] reaches [:variant], [:variant, :product] and [:variant, :product, :product_categories]. Comparing paths rather than top-level names keeps a nested join from passing for its first hop alone.

Parameters:

  • joins (Symbol, String, Hash, Array, nil)

Returns:

  • (Array<Array<Symbol>>)


11
12
13
14
15
16
17
18
19
# File 'lib/spree/reporting/registry.rb', line 11

def self.join_paths(joins, prefix = [])
  case joins
  when Hash
    joins.flat_map { |name, nested| [prefix + [name.to_sym]] + join_paths(nested, prefix + [name.to_sym]) }
  when Array then joins.flat_map { |join| join_paths(join, prefix) }
  when Symbol, String then [prefix + [joins.to_sym]]
  else []
  end.uniq
end

.joins_cover?(carried, required) ⇒ Boolean

Whether the joins carried cover every association path the required joins reach.

Returns:

  • (Boolean)


23
24
25
# File 'lib/spree/reporting/registry.rb', line 23

def self.joins_cover?(carried, required)
  (join_paths(required) - join_paths(carried)).empty?
end