implement joining table

This commit is contained in:
2012-08-30 18:32:30 +02:00
parent 6696992320
commit aa0821d0ae
15 changed files with 228 additions and 35 deletions
+8
View File
@@ -5,6 +5,8 @@ class List
property :needs_help, type: :boolean, default: false
property :needs_payment, type: :boolean, default: false
property :closed_at, type: Time
property :join_requests, type: Array, default: []
has_many :orders, dependent: :destroy
belongs_to :table
belongs_to :supplier
@@ -16,6 +18,12 @@ class List
view :by_supplier_id_and_id, key: [:supplier_id, :_id]
view :active_by_table_id, type: :custom, map_function: %|function(doc){
if(doc.ruby_class == 'List' && doc.state == 'active'){
emit(doc.table_id, 1);
}
}|, reduce_function: '_sum'
def self.from_table table, user
return if user.has_active_list?
list = new table: table, supplier_id: table.supplier_id, section_id: table.section_id
+5 -7
View File
@@ -16,15 +16,13 @@ class Table
validates :number, numericality: {greater_than: 0}
validates_uniqueness_of :number
view :active_lists, type: :custom, map_function: %|function(doc){
if(doc.ruby_class == 'List' && doc.state == 'active'){
emit(doc.table_id, 1);
}
}|, reduce_function: '_sum'
def occupied?
return @is_occupied if instance_variable_defined?(:'@is_occupied')
@is_occupied = !self.class.database.view(self.class.active_lists(key: id, reduce: true)).zero?
@is_occupied = !self.class.database.view(List.active_by_table_id(key: id, reduce: true)).zero?
end
def active_list
@active_list ||= self.class.database.view(List.active_by_table_id(key: id, include_docs: true, reduce: false, limit: 1)).try(:first)
end
def reserved?