Sha256: 5f57ac23e06f72477dc71ac9f734ee7f5cb3b84bd31405bd40a1c6b9cd6efec7

Contents?: true

Size: 1.38 KB

Versions: 6

Compression:

Stored size: 1.38 KB

Contents

module Comee
  module Core
    class CustomerOrderItem < ApplicationRecord
      default_scope { order(serial_no: :asc) }

      before_save :calculate_total_price, :fetch_customer_item_alias

      belongs_to :customer_order
      belongs_to :product
      belongs_to :unit

      validates :serial_no, :customer_item_no, :customer_item_description, :quantity, :price, :delivery_date, presence: true
      validates :quantity, :price, numericality: {greater_than_or_equal_to: 0}
      validates :total_price, numericality: {greater_than_or_equal_to: 0}, allow_nil: true
      validates :serial_no, numericality: {greater_than: 0, only_integer: true}

      def calculate_total_price
        self.total_price = (price * quantity).round(2)
      end

      def fetch_customer_item_alias
        self.customer_item_alias = "" unless customer_order && product_id
        lookup = ProductLookup.find_by(
          itemable_id: customer_order.client_id,
          itemable_type: "Comee::Core::Client",
          product_id: product_id
        )
        self.customer_item_alias = lookup&.aliases
      end

      def self.ransackable_attributes(_auth_object = nil)
        %w[id customer_order_id product_id unit_id customer_item_no customer_item_description delivery_date]
      end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
comee_core-0.3.37 app/models/comee/core/customer_order_item.rb
comee_core-0.3.36 app/models/comee/core/customer_order_item.rb
comee_core-0.3.35 app/models/comee/core/customer_order_item.rb
comee_core-0.3.34 app/models/comee/core/customer_order_item.rb
comee_core-0.3.33 app/models/comee/core/customer_order_item.rb
comee_core-0.3.32 app/models/comee/core/customer_order_item.rb