Sha256: 28408f5456ee56135aa94e4cc1e14b0a8e740cdb4453a0b63c2db69d3627813b

Contents?: true

Size: 991 Bytes

Versions: 1

Compression:

Stored size: 991 Bytes

Contents

module Comee
  module Core
    class ClientPrice < ApplicationRecord
      enum :status, {old: 0, current: 1, future: 2}
      belongs_to :product
      belongs_to :client
      belongs_to :previous_price, class_name: "Comee::Core::ClientPrice", optional: true

      validates :valid_from, :valid_to, :price, :status, presence: true
      validates :price, numericality: {greater_than: 0}
      validates :product_id, uniqueness: {scope: :client_id}
      validate :validate_price_validity_dates

      def validate_price_validity_dates
        return unless valid_from && valid_to

        period = Period.new(valid_from, valid_to)
        errors.add(:base, "Price validity date range is not correct.") unless period.valid?
      end

      def self.ransackable_attributes(auth_object = nil)
        ["valid_from", "valid_to", "new_price", "old_price", "product_id", "client_id"]
      end

      def self.ransackable_associations(auth_object = nil)
        []
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
comee_core-0.1.14 app/models/comee/core/client_price.rb