Class: SolidusFriendlyPromotions::FriendlyPromotionAdjuster::LoadPromotions

Inherits:
Object
  • Object
show all
Defined in:
app/models/solidus_friendly_promotions/friendly_promotion_adjuster/load_promotions.rb

Instance Method Summary collapse

Constructor Details

#initialize(order:, dry_run_promotion: nil) ⇒ LoadPromotions

Returns a new instance of LoadPromotions.



6
7
8
9
# File 'app/models/solidus_friendly_promotions/friendly_promotion_adjuster/load_promotions.rb', line 6

def initialize(order:, dry_run_promotion: nil)
  @order = order
  @dry_run_promotion = dry_run_promotion
end

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/solidus_friendly_promotions/friendly_promotion_adjuster/load_promotions.rb', line 11

def call
  promos = connected_order_promotions | sale_promotions
  promos << dry_run_promotion if dry_run_promotion
  promos.flat_map(&:benefits).group_by(&:preload_relations).each do |preload_relations, benefits|
    preload(records: benefits, associations: preload_relations)
    benefits.flat_map(&:conditions).group_by(&:preload_relations).each do |preload_relations, conditions|
      preload(records: conditions, associations: preload_relations)
    end
  end
  promos.reject { |promotion| promotion.usage_limit_exceeded?(excluded_orders: [order]) }
end