Files
mozo-backend/app/models/supplier.rb
T
2012-09-06 14:49:22 +02:00

74 lines
1.9 KiB
Ruby

class Supplier
include SimplyStored::Couch
include Devise::Orm::SimplyStored
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable
property :name
property :open, type: :boolean, default: false
#WIFI
property :offer_wifi
property :wifi_ssid
property :wifi_type
property :wifi_password
#has_many :orders, through: :lists
has_many :products, dependent: :destroy
has_many :product_categories, dependent: :destroy
has_many :tables, dependent: :destroy
#has_many :lists, through: :tables
has_many :orders
has_many :sections, dependent: :destroy
after_create :add_section_on_create
def active_orders(options = {})
return @active_orders if @active_orders && @active_orders_options == options
@active_orders_options = options
@active_orders = options[:section_id] ? Order.active_for_supplier_and_section(self, options[:section_id]) : Order.active_for_supplier(id)
@active_orders.include_relation(list: {table: :section}, product_orders: :order)
@active_orders
end
def active_lists(options = {})
return @active_lists if @active_lists
@tables ||= active_tables(options)
@tables.include_relation(:lists)
@active_lists = @tables.map(&:lists).flatten.select(&:active?)
@active_lists.include_relations(table: :section, orders: {product_orders: :product})
end
# Return the currently active tables for the supplier
def active_tables(options = {})
options[:section_id].present? ? tables.select{|t| t.section_id == options[:section_id]} : tables
end
def non_placed_tables
tables.reject{|t| t.section_id.present? }
end
def open?
open
end
def closed?
!open?
end
def mark_as_open!
self.open = true
save
end
def mark_as_closed!
self.open = false
save
end
private
def add_section_on_create
@section = Section.create supplier: self, title: I18n.t('section.first_section_title')
end
end