2014-12-04 18:50:02 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: orders
|
|
|
|
#
|
2015-09-14 09:20:52 +00:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer
|
|
|
|
# price_cents :integer
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# transaction_id :integer
|
2014-12-04 18:50:02 +00:00
|
|
|
#
|
|
|
|
|
2014-11-09 21:53:39 +00:00
|
|
|
class Order < ActiveRecord::Base
|
2015-11-30 17:20:54 +00:00
|
|
|
include ActionView::Helpers::TextHelper, ApplicationHelper
|
2015-01-14 23:39:34 +00:00
|
|
|
|
2014-12-10 12:21:06 +00:00
|
|
|
belongs_to :user, counter_cache: true
|
2015-02-09 16:06:24 +00:00
|
|
|
has_many :order_items, dependent: :destroy
|
|
|
|
has_many :products, through: :order_items
|
2014-11-25 13:27:27 +00:00
|
|
|
|
2015-09-14 09:20:52 +00:00
|
|
|
before_validation :calculate_price
|
2015-09-14 15:29:00 +00:00
|
|
|
before_save { |o| o.order_items = o.order_items.reject{ |oi| oi.count == 0 } }
|
2015-12-01 20:19:35 +00:00
|
|
|
after_create :create_api_job, unless: -> { user.guest? }
|
2015-09-12 09:17:58 +00:00
|
|
|
|
2016-02-16 14:00:43 +00:00
|
|
|
after_create :update_user_frecency
|
|
|
|
after_destroy :update_user_frecency
|
|
|
|
|
2014-12-04 18:50:02 +00:00
|
|
|
validates :user, presence: true
|
2015-09-14 18:26:16 +00:00
|
|
|
validates_associated :order_items
|
2015-09-17 12:05:33 +00:00
|
|
|
validate :product_presence
|
2014-12-04 18:50:02 +00:00
|
|
|
|
2015-09-14 15:29:00 +00:00
|
|
|
accepts_nested_attributes_for :order_items
|
2015-03-10 10:37:48 +00:00
|
|
|
|
2015-01-14 23:39:34 +00:00
|
|
|
def to_sentence
|
2015-02-09 16:06:24 +00:00
|
|
|
self.order_items.map {
|
2015-04-04 01:38:29 +00:00
|
|
|
|oi| pluralize(oi.count, oi.product.name)
|
2015-01-14 23:39:34 +00:00
|
|
|
}.to_sentence
|
|
|
|
end
|
2015-02-12 13:39:58 +00:00
|
|
|
|
2015-11-30 17:20:54 +00:00
|
|
|
def flash_success
|
|
|
|
f = "#{to_sentence} ordered."
|
2015-12-01 20:19:35 +00:00
|
|
|
f << " Please put #{euro_from_cents(price_cents)} in our pot!" if user.guest?
|
2015-11-30 17:20:54 +00:00
|
|
|
f << " Enjoy it!"
|
|
|
|
end
|
|
|
|
|
2015-10-26 17:57:59 +00:00
|
|
|
def deletable
|
|
|
|
self.created_at > Rails.application.config.call_api_after.ago
|
|
|
|
end
|
|
|
|
|
|
|
|
def sec_until_remove
|
|
|
|
self.created_at.to_i - (Time.now - Rails.application.config.call_api_after).to_i
|
|
|
|
end
|
|
|
|
|
2015-09-14 18:26:16 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def calculate_price
|
2015-09-21 15:38:22 +00:00
|
|
|
self.price_cents = self.order_items.map{ |oi| oi.count * (oi.product.try(:price_cents) || 0) }.sum
|
2015-02-12 13:39:58 +00:00
|
|
|
end
|
2015-09-12 09:47:00 +00:00
|
|
|
|
2015-09-14 18:26:16 +00:00
|
|
|
def create_api_job
|
2015-09-14 18:55:49 +00:00
|
|
|
return if Rails.env.test?
|
|
|
|
|
2015-09-14 18:26:16 +00:00
|
|
|
priority = 0
|
|
|
|
run_at = Rails.application.config.call_api_after.from_now
|
|
|
|
job = TabApiJob.new(id)
|
2015-09-12 09:47:00 +00:00
|
|
|
|
2015-09-14 18:55:49 +00:00
|
|
|
Delayed::Job.enqueue job, priority: priority, run_at: run_at
|
2015-09-14 18:26:16 +00:00
|
|
|
end
|
2015-09-17 12:05:33 +00:00
|
|
|
|
|
|
|
def product_presence
|
|
|
|
errors.add(:base, "You have to order at least one product.") if order_items.map(&:count).sum.zero?
|
|
|
|
end
|
2016-02-16 14:00:43 +00:00
|
|
|
|
|
|
|
def update_user_frecency
|
|
|
|
self.user.calculate_frecency
|
|
|
|
end
|
2014-11-09 21:53:39 +00:00
|
|
|
end
|