Sha256: cb1f1481ca6bca077bfef01e9cfbbeda5c8247ac28b136a6504d76490f5ec520

Contents?: true

Size: 1.53 KB

Versions: 23

Compression:

Stored size: 1.53 KB

Contents

module Workarea
  module Admin
    module Insights
      class ProductViewModel < ApplicationViewModel
        include InsightsDetails

        insights_scope -> { Metrics::ProductByDay.by_product(model.id) }

        add_sparkline :orders
        add_summaries :views, :orders, :revenue, :units_sold, :merchandise, :discounts
        add_graph_data :views, :orders, :revenue, :units_sold

        def feed
          @feed ||= InsightViewModel.wrap(
            Workarea::Insights::Base.by_product(model.id).to_a
          )
        end

        def top?
          Workarea::Insights::TopProducts.current.include?(product_id: model.id)
        end

        def trending?
          Workarea::Insights::TrendingProducts.current.include?(product_id: model.id)
        end

        def skus
          @skus ||= skus_report.results.map { |r| OpenStruct.new(r) }
        end

        def skus_report
          @skus_report ||= Workarea::Reports::SalesBySku.new(
            skus: model.skus,
            starts_at: starts_at,
            ends_at: ends_at
          )
        end

        def average_price
          return nil if orders.zero?
          (merchandise - discounts) / orders
        end

        def previous_average_price
          return nil if previous_orders.zero?
          (previous_merchandise - previous_discounts) / previous_orders
        end

        def average_price_percent_change
          return nil if previous_average_price.blank?
          calculate_percent_change(previous_average_price, average_price)
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
workarea-admin-3.4.14 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.13 app/view_models/workarea/admin/insights/product_view_model.rb
workarea-admin-3.4.12 app/view_models/workarea/admin/insights/product_view_model.rb