Files
mozo-backend/app/models/supplier.rb
T
2013-10-07 23:00:01 +02:00

136 lines
3.8 KiB
Ruby

class Supplier
include SimplyStored::Couch
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable, :confirmable
property :unconfirmed_email
property :name
property :open, type: :boolean, default: false
property :time_zone, default: 'UTC'
property :night_offset, type: Fixnum, default: 0 # Minutes
#LOCATION
property :lat, type: Float, default: 52.08062426379751
property :lng, type: Float, default: 4.312562942504883
#WIFI
property :offer_wifi
property :wifi_ssid
property :wifi_type
property :wifi_password
#REVIEWS
property :iens_profile
#has_many :orders, through: :lists
has_many :products, dependent: :destroy
has_many :product_categories, dependent: :destroy
has_many :tables, dependent: :destroy
has_many :lists, dependent: :destroy
#has_many :lists, through: :tables
has_many :orders, dependent: :destroy
has_many :sections, dependent: :destroy
after_create :add_section_on_create
view :by_email, key: :email
validates :name, presence: true
validates :iens_profile, numericality: {allow_blank: true}
def location=(val)
lat, lng = val.is_a?(Array) ? val : val.strip.split(/[ ,]+/).map(&:to_f)
self.lat = lat
self.lng = lng
end
def location
[lat, lng].join(',')
end
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
@active_lists = options[:section_id].present? ? List.active_for_supplier_and_section(self, options[:section_id]) : List.active_for_supplier(id)
@active_lists.include_relations(table: :section, orders: {product_orders: :product})
@active_lists
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
# Find a user by its confirmation token and try to confirm it.
# If no user is found, returns a new user with an error.
# If the user is already confirmed, create an error for the user
# Options must have the confirmation_token
#
# Overwrite devise method for mail sending
def self.confirm_by_token(confirmation_token)
confirmable = find_or_initialize_with_error_by(:confirmation_token, confirmation_token)
send_creation_mail = confirmable.errors.empty? && !confirmable.confirmed?
confirmable.confirm! if confirmable.persisted?
confirmable.send_creation_notifications if send_creation_mail
confirmable
end
# Send confirmation instructions by email
def send_confirmation_instructions
self.confirmation_token = nil if reconfirmation_required?
@reconfirmation_required = false
@bypass_postpone = true and generate_confirmation_token! if self.confirmation_token.blank?
self.devise_mailer.confirmation_instructions(self).deliver
end
def find_order(id)
order = Order.find(id)
raise SimplyStored::RecordNotFound unless order.supplier_id == self.id
order
end
def send_creation_notifications
SupplierMailer.creation(self).deliver
end
def week_starts_on_monday?
true
end
private
def add_section_on_create
@section = Section.create supplier: self, title: I18n.t('supplier.section.first_section_title')
end
end