Sha256: 13d57cd56cf699bace094d5eb0a12a58e614dcc58fd87b1f5acf43387048a066

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

class Spree::VolumePrice < ApplicationRecord
  belongs_to :variant, touch: true, optional: true
  belongs_to :volume_price_model, touch: true, optional: true
  belongs_to :spree_role, class_name: 'Spree::Role', foreign_key: 'role_id', optional: true
  acts_as_list scope: [:variant_id, :volume_price_model_id]

  validates :amount, presence: true
  validates :discount_type,
    presence: true,
    inclusion: {
      in: %w(price dollar percent)
    }

  validate :range_format

  def self.for_variant(variant, user: nil)
    roles = [nil]
    if user
      roles << user.resolve_role&.id
    end

    where(
      arel_table[:variant_id].eq(variant.id).
      or(
        arel_table[:volume_price_model_id].in(variant.volume_price_model_ids)
      )
    ).
      where(role_id: roles).
      order(position: :asc, amount: :asc)
  end

  delegate :include?, to: :range_from_string

  def display_range
    range.gsub(/\.+/, "-").gsub(/\(|\)/, '')
  end

  private

  def range_format
    if !(SolidusVolumePricing::RangeFromString::RANGE_FORMAT =~ range ||
         SolidusVolumePricing::RangeFromString::OPEN_ENDED =~ range)
      errors.add(:range, :must_be_in_format)
    end
  end

  def range_from_string
    SolidusVolumePricing::RangeFromString.new(range).to_range
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solidus_volume_pricing-1.0.0 app/models/spree/volume_price.rb