Sha256: dcded7250dcae74457b9ab006d52963462b36bbee581a7bf1e6b6c71a1d3ee0d
Contents?: true
Size: 1.54 KB
Versions: 5
Compression:
Stored size: 1.54 KB
Contents
# frozen_string_literal: true module SolidusPromotions module Conditions class LineItemTaxon < Condition include LineItemLevelCondition has_many :condition_taxons, class_name: "SolidusPromotions::ConditionTaxon", foreign_key: :condition_id, dependent: :destroy has_many :taxons, through: :condition_taxons, class_name: "Spree::Taxon" MATCH_POLICIES = %w[include exclude].freeze validates :preferred_match_policy, inclusion: { in: MATCH_POLICIES } preference :match_policy, :string, default: MATCH_POLICIES.first def preload_relations [:taxons] end def eligible?(line_item, _options = {}) found = Spree::Classification.where( product_id: line_item.variant.product_id, taxon_id: condition_taxon_ids_with_children ).exists? case preferred_match_policy when "include" found when "exclude" !found else raise "unexpected match policy: #{preferred_match_policy.inspect}" end end def taxon_ids_string taxons.pluck(:id).join(",") end def taxon_ids_string=(taxon_ids) taxon_ids = taxon_ids.to_s.split(",").map(&:strip) self.taxons = Spree::Taxon.find(taxon_ids) end def updateable? true end private # ids of taxons conditions and taxons conditions children def condition_taxon_ids_with_children taxons.flat_map { |taxon| taxon.self_and_descendants.ids }.uniq end end end end
Version data entries
5 entries across 5 versions & 1 rubygems