Sha256: 90e302d79b3bba3c95c2c064805803f096a292053b9e2230e46c99956652c060

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

module Comable
  class CartItem < ActiveRecord::Base
    belongs_to :customer, class_name: Comable::Customer.name, foreign_key: Comable::Customer.table_name.singularize.foreign_key
    belongs_to :stock, class_name: Comable::Stock.name, foreign_key: Comable::Stock.table_name.singularize.foreign_key

    with_options if: :customer do |customer|
      customer.validates Comable::Customer.table_name.singularize.foreign_key, uniqueness: { scope: [Comable::Customer.table_name.singularize.foreign_key, Comable::Stock.table_name.singularize.foreign_key] }
      customer.validates Comable::Customer.table_name.singularize.foreign_key, presence: true
    end

    with_options if: :guest_token do |guest|
      guest.validates :guest_token, uniqueness: { scope: [:guest_token, Comable::Stock.table_name.singularize.foreign_key] }
      guest.validates :guest_token, presence: true
    end

    delegate :product, to: :stock

    before_create :generate_guest_token

    def price
      stock.price * quantity
    end

    private

    def generate_guest_token
      return if send(Comable::Customer.table_name.singularize.foreign_key)
      self.guest_token ||= loop do
        random_token = SecureRandom.urlsafe_base64(nil, false)
        break random_token unless self.class.exists?(guest_token: random_token)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
comable-0.0.2 app/models/comable/cart_item.rb