Implement spring and reset counters functionality

This commit is contained in:
2014-03-07 13:37:01 +01:00
parent 775e7965a2
commit 95e268adea
14 changed files with 104 additions and 84 deletions
+29
View File
@@ -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?