Class: Spree::Pricing::BatchResolver
- Inherits:
-
Object
- Object
- Spree::Pricing::BatchResolver
- Defined in:
- app/models/spree/pricing/batch_resolver.rb
Overview
1商品の全 variant について、適用される実価格をまとめて解決する。
本家の Resolver は variant ごとに price_list の適用判定とクエリ(または Rails.cache 往復)を 行うため、多 variant 商品ではフラグメントキャッシュが冷えた際の一覧描画が極端に遅くなる。 price_list の適用判定は variant に依存しない(active/scheduled + 期間 + rules のみ)ので、 商品ごとに1回だけ判定し、全 variant の価格をまとめて読んでから選び直す。
価格の選択規則は Resolver から複製しているため、Spree アップグレード時は各メソッドの @gem-override マーカーで本家の変更(rake gem_override_marker:diff / :check)を確認すること。
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#variants ⇒ Object
readonly
Returns the value of attribute variants.
Instance Method Summary collapse
-
#initialize(variants:, context:) ⇒ BatchResolver
constructor
A new instance of BatchResolver.
-
#resolve ⇒ Array<Spree::Price>
Amount が確定した価格のみ(価格未設定の variant は除外).
Constructor Details
#initialize(variants:, context:) ⇒ BatchResolver
Returns a new instance of BatchResolver.
18 19 20 21 |
# File 'app/models/spree/pricing/batch_resolver.rb', line 18 def initialize(variants:, context:) @variants = variants @context = context end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
14 15 16 |
# File 'app/models/spree/pricing/batch_resolver.rb', line 14 def context @context end |
#variants ⇒ Object (readonly)
Returns the value of attribute variants.
14 15 16 |
# File 'app/models/spree/pricing/batch_resolver.rb', line 14 def variants @variants end |
Instance Method Details
#resolve ⇒ Array<Spree::Price>
Returns amount が確定した価格のみ(価格未設定の variant は除外).
24 25 26 27 28 29 30 31 32 33 |
# File 'app/models/spree/pricing/batch_resolver.rb', line 24 def resolve return [] if variants.empty? price_list_ids = applicable_price_lists.map(&:id) prices_by_variant_id = prices_by_variant_id_for(currency) variants.filter_map do |variant| find_best_price(prices_by_variant_id[variant.id] || [], price_list_ids) end end |