Sha256: 0cfdf95e7f68f360723fad923f21849f3190b23b718cf02e1b4d197da1b2cc73

Contents?: true

Size: 845 Bytes

Versions: 1

Compression:

Stored size: 845 Bytes

Contents

module Unidom::Shopping::Concerns::AsItemShopper

  extend ActiveSupport::Concern

  included do |includer|

    has_many :shopping_items, class_name: 'Unidom::Shopping::ShoppingItem', as: :shopper

    def add!(it, into: nil, at: Time.now, unit_price: 0, quantity: 1)
      query = shopping_items.shopping_cart_is(into).shopped_is(it).valid_at(now: at).alive
      item  = query.first
      if item.present?
        item.unit_price = unit_price
        item.increment! quantity: quantity
      else
        item = shopping_items.create! shopping_cart: into, shopped: it, unit_price: unit_price, quantity: quantity, opened_at: at
      end
      item
    end

    #def add?(it, into: nil, at: Time.now)
    #  shopping_items.shopping_cart_is(into).shopped_is(it).valid_at(now: at).alive.exists?
    #end

  end

  module ClassMethods
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unidom-shopping-1.2 app/models/unidom/shopping/concerns/as_item_shopper.rb