Skip to content

Commit

Permalink
Use currently_valid_prices method for detecting default price
Browse files Browse the repository at this point in the history
This uses the redefined public method from the previous commit to detect
the default price. Note that through using `reverse_each.detect` rather
than `min` with a block we should be able to save some iterations.
  • Loading branch information
mamhoff committed Sep 23, 2022
1 parent 5b9d4f7 commit f556d91
Showing 1 changed file with 3 additions and 25 deletions.
28 changes: 3 additions & 25 deletions core/app/models/concerns/spree/default_price.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,15 @@ def default_price_or_build
# @return [Spree::Price, nil]
# @see Spree::Variant.default_price_attributes
def default_price
prioritized_default(
prices_meeting_criteria_to_be_default(
prices
)
)
end

def has_default_price?
default_price.present? && !default_price.discarded?
end

private

def prices_meeting_criteria_to_be_default(prices)
criteria = self.class.default_price_attributes.transform_keys(&:to_s)
prices.select do |price|
currently_valid_prices.detect do |price|
contender = price.attributes.slice(*criteria.keys)
criteria == contender
end
end

def prioritized_default(prices)
prices.min do |prev, succ|
contender_one, contender_two = [succ, prev].map do |item|
[
item.updated_at || Time.zone.now,
item.id || Float::INFINITY
]
end
contender_one <=> contender_two
end
def has_default_price?
default_price.present? && !default_price.discarded?
end
end
end

0 comments on commit f556d91

Please sign in to comment.