Sha256: 0d0553dee48331e628aa82fb7cd103c76e40e725316fb735c05c3c628a750fd5

Contents?: true

Size: 1.12 KB

Versions: 6

Compression:

Stored size: 1.12 KB

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 :discount, presence: true, numericality: {greater_than_or_equal_to: 0, less_than_or_equal_to: 100}
      validates :product_id, uniqueness: {scope: [:client_id, :previous_price_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", "price", "status", "product_id", "client_id", "previous_price_id"]
      end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
comee_core-0.1.23 app/models/comee/core/client_price.rb
comee_core-0.1.22 app/models/comee/core/client_price.rb
comee_core-0.1.21 app/models/comee/core/client_price.rb
comee_core-0.1.20 app/models/comee/core/client_price.rb
comee_core-0.1.19 app/models/comee/core/client_price.rb
comee_core-0.1.18 app/models/comee/core/client_price.rb