Sha256: 8b4ea232a9a79477f7394d9218be22e093cda6abf56d4e7d423caf5ed6f585d1

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

module Comee
  module Core
    class Price < ApplicationRecord
      self.abstract_class = true
      before_save :compute_status

      enum :status, {past: 0, current: 1, future: 2}

      scope :current, -> { where(status: Price.statuses[:current]) }
      scope :future, -> { where(status: Price.statuses[:future]) }

      belongs_to :product
      belongs_to :unit

      validates :valid_from, :valid_to, presence: true
      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 compute_status
        period = Period.new(valid_from, valid_to)
        if period.past?
          self.status = Price.statuses[:past]
        elsif period.current?
          self.status = Price.statuses[:current]
        elsif period.future?
          self.status = Price.statuses[:future]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
comee_core-0.1.33 app/models/comee/core/price.rb