Implement spring and reset counters functionality
This commit is contained in:
+2
-1
@@ -26,7 +26,8 @@ class Order
|
||||
}
|
||||
}], reduce_function: '_sum'
|
||||
|
||||
view :by_supplier_id_and_id, key: [:supplier_id, :_id] # Do not comment me out
|
||||
view :by_supplier_id_and_id, key: [:supplier_id, :_id] # Do not comment me out this is for security, TODO: make the security server side for speed and space optimization
|
||||
view :by_supplier_id_and_state, key: [:supplier_id, :state], reduce_function: '_sum'
|
||||
|
||||
def self.for_supplier(supplier, options = {})
|
||||
total_entries = database.view(by_supplier_id_and_id({startkey: ["#{supplier.id}\u9999"], endkey: [supplier.id], include_docs: false, reduce: true, descending: true}))
|
||||
|
||||
@@ -115,6 +115,35 @@ class Supplier
|
||||
confirmable
|
||||
end
|
||||
|
||||
def self.reset_counters!
|
||||
spec = Order.by_supplier_id_and_state(reduce: true, group_level: 2)
|
||||
reset_order_counters_with_spec spec
|
||||
end
|
||||
|
||||
def reset_counters!
|
||||
spec = Order.by_supplier_id_and_state(startkey: [id], endkey: [id, {}], reduce: true, group_level: 2)
|
||||
self.class.reset_order_counters_with_spec spec
|
||||
end
|
||||
|
||||
def self.reset_order_counters_with_spec(spec)
|
||||
# taken from the couch_potato source since we want a model/custom mix here (hmmmm, something that should be fixed in the couchbase version)
|
||||
results = CouchPotato::View::ViewQuery.new(
|
||||
database.couchrest_database,
|
||||
spec.design_document,
|
||||
{spec.view_name => { map: spec.map_function, reduce: spec.reduce_function} },
|
||||
({spec.list_name => spec.list_function} unless spec.list_name.nil?),
|
||||
spec.language
|
||||
).query_view!(spec.view_parameters)
|
||||
Array.wrap(results['rows']).each do |result|
|
||||
supplier_id, state = result['key']
|
||||
case state
|
||||
when 'placed' then Qwaiter::Counter.set "supplier:#{supplier_id}:orders_in_process", result['value']
|
||||
when 'active' then Qwaiter::Counter.set "supplier:#{supplier_id}:orders_delivered", result['value']
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# Send confirmation instructions by email
|
||||
def send_confirmation_instructions(*args)
|
||||
self.confirmation_token = nil if reconfirmation_required?
|
||||
|
||||
Reference in New Issue
Block a user