Class: SolidusFriendlyPromotions::EligibilityResults

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
app/models/solidus_friendly_promotions/eligibility_results.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(promotion) ⇒ EligibilityResults

Returns a new instance of EligibilityResults.



7
8
9
10
# File 'app/models/solidus_friendly_promotions/eligibility_results.rb', line 7

def initialize(promotion)
  @promotion = promotion
  @results = []
end

Instance Attribute Details

#promotionObject (readonly)

Returns the value of attribute promotion.



6
7
8
# File 'app/models/solidus_friendly_promotions/eligibility_results.rb', line 6

def promotion
  @promotion
end

#resultsObject (readonly)

Returns the value of attribute results.



6
7
8
# File 'app/models/solidus_friendly_promotions/eligibility_results.rb', line 6

def results
  @results
end

Instance Method Details

#add(item:, condition:, success:, code:, message:) ⇒ Object



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

def add(item:, condition:, success:, code:, message:)
  results << EligibilityResult.new(
    item: item,
    condition: condition,
    success: success,
    code: code,
    message: message
  )
end

#each(&block) ⇒ Object



40
41
42
# File 'app/models/solidus_friendly_promotions/eligibility_results.rb', line 40

def each(&block)
  results.each(&block)
end

#error_messagesObject



32
33
34
35
36
37
38
# File 'app/models/solidus_friendly_promotions/eligibility_results.rb', line 32

def error_messages
  return [] if results.empty?
  results.group_by(&:condition).map do |condition, results|
    next if results.any?(&:success)
    results.detect { |r| !r.success }&.message
  end.compact
end

#lastObject



44
45
46
# File 'app/models/solidus_friendly_promotions/eligibility_results.rb', line 44

def last
  results.last
end

#success?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
# File 'app/models/solidus_friendly_promotions/eligibility_results.rb', line 22

def success?
  return true if results.empty?
  promotion.benefits.any? do |benefit|
    benefit.conditions.all? do |condition|
      results_for_condition = results.select { |result| result.condition == condition }
      results_for_condition.any?(&:success)
    end
  end
end