From bf3f8764e1ac3fcfd3ab4b980ffd92931f071f11 Mon Sep 17 00:00:00 2001 From: BenClaw Date: Sun, 17 May 2026 22:26:34 +0200 Subject: [PATCH] fix(counter): use couchrest_database, proper begin/rescue instead of inline rescue nil - Order.database returns CouchPotato::Database, not CouchRest::Database - Fix: use .couchrest_database for raw CouchRest access - Replace fragile 'rescue nil' inline with explicit begin/rescue - Simplify YAML.safe_load (remove unnecessary permitted_classes) --- lib/mozo/counter.rb | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/mozo/counter.rb b/lib/mozo/counter.rb index d5c0ac15..23f0cbba 100644 --- a/lib/mozo/counter.rb +++ b/lib/mozo/counter.rb @@ -33,17 +33,17 @@ module Mozo def self.provision! require 'couchrest' - db = Order.database + # CouchPotato::Database wraps CouchRest — get the raw DB for direct view queries + db = Order.database.couchrest_database design = '_design/order_counter' view = 'by_supplier_id_and_state' - # Ensure the design doc exists - unless db.get(design) rescue nil + # Ensure the design doc exists (auto-create if missing) + begin + db.get(design) + rescue RestClient::ResourceNotFound, CouchRest::NotFound puts "[Counter.provision!] Creating design doc #{design}" - doc = YAML.safe_load( - File.read(Rails.root.join('drb_counter/couchdb_design.yml')), - permitted_classes: [Symbol] - ) + doc = YAML.safe_load(File.read(Rails.root.join('drb_counter/couchdb_design.yml'))) doc['_id'] = design doc['language'] ||= 'javascript' db.save_doc(doc) @@ -59,14 +59,11 @@ module Mozo supplier_id, state = row['key'] count = row['value'] - case state - when 'placed' - key = "supplier_counter:#{supplier_id}:orders_placed" - when 'active' - key = "supplier_counter:#{supplier_id}:orders_in_process" - else - next - end + key = case state + when 'placed' then "supplier_counter:#{supplier_id}:orders_placed" + when 'active' then "supplier_counter:#{supplier_id}:orders_in_process" + end + next unless key set(key, count) puts " #{key} = #{count}"