Sha256: 613ab675c5285f2fdaa2ff28d1ebde7f410f8539565733231bfef91516b1c7df
Contents?: true
Size: 1.76 KB
Versions: 3
Compression:
Stored size: 1.76 KB
Contents
module ShoppyCartus class Order < ApplicationRecord include AASM belongs_to :user, optional: true, class_name: ShoppyCartus.user_class belongs_to :credit_card, optional: true, class_name: 'ShoppyCartus::CreditCard' belongs_to :delivery, optional: true, class_name: 'ShoppyCartus::Delivery' has_many :addresses, as: :addressable, dependent: :destroy has_many :order_items, dependent: :destroy, class_name: 'ShoppyCartus::OrderItem' has_one :coupon, dependent: :nullify, class_name: 'ShoppyCartus::Coupon' validates :state, presence: true scope :in_progress, -> { find_by(state: 'filling') } scope :executed, -> { where.not(state: 'filling') } scope :by_state, ->(state) { where(state: state) } accepts_nested_attributes_for :addresses aasm column: :state do state :filling, initial: true state :in_confirmation, after_enter: :send_confirmation state :in_processing state :in_delivery state :completed state :canceled event :confirm do transitions from: :filling, to: :in_confirmation end event :process do transitions from: :in_confirmation, to: :in_processing end event :deliver do transitions from: :in_processing, to: :in_delivery end event :complete do transitions from: :in_delivery, to: :completed end event :cancel do transitions from: %i[filling in_confirmation in_processing in_delivery completed], to: :canceled end end def send_confirmation ShoppyCartus::OrderMailer.confirm_order(user, self).deliver_now end def self.aasm_states aasm.states.map(&:name) end def self.active_order_for_user(user) find_by(user: user, state: 'filling') end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
shoppy_cartus-0.1.2 | app/models/shoppy_cartus/order.rb |
shoppy_cartus-0.1.1 | app/models/shoppy_cartus/order.rb |
shoppy_cartus-0.1.0 | app/models/shoppy_cartus/order.rb |