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)
This commit is contained in:
BenClaw
2026-05-17 22:26:34 +02:00
parent eef3d17431
commit bf3f8764e1
+12 -15
View File
@@ -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}"