Sha256: 4f3e9a1eb82acd44702fa41c230cf12ddc3686507e4558cb2cd95b6be095c388

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

module Comee
  module Core
    class ClientPrice < Price
      enum :margin_type, {increase: 0, discount: 1}
      belongs_to :client
      belongs_to :previous_price, class_name: "Comee::Core::ClientPrice", optional: true
      belongs_to :next_price, class_name: "Comee::Core::ClientPrice", optional: true
      belongs_to :product_lookup, optional: true

      validates :price, :margin_type, presence: true
      validates :price, numericality: {greater_than: 0}
      validates :margin, presence: true, numericality: {greater_than_or_equal_to: 0, less_than_or_equal_to: 100}
      validates :product_id, uniqueness: {scope: %i[client_id previous_price_id next_price_id status]}
      validate :validate_product_lookup

      def validate_product_lookup
        return unless product_lookup && product && client

        if product_lookup.itemable_id != client_id ||
           product_lookup.itemable_type != "Comee::Core::Client" ||
           product_lookup.product_id != product_id
          errors.add(:product_lookup, "contains wrong client or product.")
        end
      end

      def self.ransackable_attributes(_auth_object = nil)
        %w[
          valid_from
          valid_to
          price
          status
          product_id
          client_id
          previous_price_id
          next_price_id
        ]
      end

      def self.ransackable_associations(_auth_object = nil)
        %w[product client]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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