Make section table view work request and event based

This commit is contained in:
2013-03-10 12:48:03 +01:00
parent cc13797f5a
commit 5eac918255
11 changed files with 164 additions and 38 deletions
+10 -3
View File
@@ -70,6 +70,7 @@ class List
list.save
user.active_list_id = list.id
user.save
# list_added is depricated, now handled by list_update
#list.broadcast_supplier list.supplier_id, 'list_added', list.with_info_as_json
list
end
@@ -96,7 +97,12 @@ class List
end
def self.active_for_table(table_id, options = {})
database.view(active_by_table_id_view(options.reverse_merge(key: table_id, reduce: false, include_docs: true)))
if table_id.is_a?(Array)
database.view(active_by_table_id_view(options.reverse_merge(keys: table_id, reduce: false, include_docs: true)))
else
table_id = table_id.id if table_id.is_a?(SimplyStored::Couch)
database.view(active_by_table_id_view(options.reverse_merge(key: table_id, reduce: false, include_docs: true)))
end
end
def self.for_user(user, options = {})
@@ -158,13 +164,14 @@ class List
def move_to_table! to_table
UserTableMove.create list: self, from_table_id: table_id, to_table: to_table
from_table = self.table_id.try(:dup)
self.table = to_table
self.section_id = to_table.section_id
if save
for user_id in user_ids
broadcast_user user_id, 'list_changed_table', list_id: id, table: to_table, section_title: to_table.section.try(:title)
broadcast_user user_id, 'list_changed_table', list_id: id, table: to_table, section_title: to_table.section.try(:title), from_table_id: from_table
end
broadcast_supplier supplier_id, 'list_changed_table', list_id: id, table: to_table, section_title: to_table.section.try(:title)
broadcast_supplier supplier_id, 'list_changed_table', list_id: id, table: to_table, section_title: to_table.section.try(:title), from_table_id: from_table
end
end
+6 -5
View File
@@ -33,7 +33,8 @@ class Order
# Return all currently active orders for a given section
def self.active_for_supplier_and_section(supplier, section_id)
database.view(active_for_supplier_and_section_view(key: [supplier.id, section_id], reduce: false, include_docs: true))
supplier_id = supplier.is_a?(SimplyStored::Couch) ? supplier.id : supplier
database.view(active_for_supplier_and_section_view(key: [supplier_id, section_id], reduce: false, include_docs: true))
end
def table_number
@@ -53,9 +54,9 @@ class Order
self.state = 'active'
if save
for user_id in list.user_ids
broadcast_user user_id, 'order_being_processed', id: id
broadcast_user user_id, 'order_being_processed', id: id, list_id: list_id
end
broadcast_supplier supplier_id, 'order_being_processed', id: id
broadcast_supplier supplier_id, 'order_being_processed', id: id, list_id: list_id
end
end
@@ -63,9 +64,9 @@ class Order
self.state = 'delivered'
if save
for user_id in list.user_ids
broadcast_user user_id, 'order_being_delivered', id: id
broadcast_user user_id, 'order_being_delivered', id: id, list_id: list_id
end
broadcast_supplier supplier_id, 'order_being_delivered', id: id
broadcast_supplier supplier_id, 'order_being_delivered', id: id, list_id: list_id
end
end
+8 -2
View File
@@ -29,6 +29,10 @@ class Section
@active_lists = List.active_for_section(self.id)
end
def active_orders
@active_orders ||= Order.active_for_supplier_and_section(supplier_id, id)
end
def width
self.path.last.try(:first).to_f - self.path.first.try(:first).to_f
@@ -66,9 +70,11 @@ class Section
h[:tables] = []
for table in tables
ht = table.as_json
table_list = active_lists.find{|l| l.table_id == table.id}
ht[:occupied] = occupied_tables.include?(table)
ht[:needs_help] = ht[:occupied] ? active_lists.find{|l| l.table_id == table.id}.try(:needs_help).present? : false
ht[:needs_payment] = ht[:occupied] ? active_lists.find{|l| l.table_id == table.id}.try(:needs_payment).present? : false
ht[:needs_help] = ht[:occupied] && table_list ? table_list.needs_help? : false
ht[:needs_payment] = ht[:occupied] && table_list ? table_list.needs_payment? : false
ht[:active_order] = ht[:occupied] && table_list ? active_orders.any?{|o| o.list_id == table_list.id } : false
h[:tables] << ht
end
@for_tables_as_json = h
+16
View File
@@ -11,6 +11,7 @@ class Table
has_many :lists
attr_protected :supplier_id
attr_accessor :active_list_id
validates :supplier_id, presence: true
#validates :list_id, presence: true
@@ -45,6 +46,21 @@ class Table
@is_occupied = !self.class.database.view(List.active_by_table_id_view(key: id, reduce: true)).zero?
end
def self.enrich_active_list_id(tables)
if tables.is_a?(Array)
lists = List.active_for_table(tables.map(&:id))
for table in tables
table.active_list_id = lists.find{|l| l.table_id == table.id}.try(:id)
end
tables
else
table = tables
list = List.active_for_table(table).first
table.active_list_id = list.id if list.present?
table
end
end
def active_list
@active_list ||= self.class.database.view(List.active_by_table_id_view(key: id, include_docs: true, reduce: false, limit: 1)).try(:first)
end