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