From 34b0379203af063ef3822da3bfbf893aba8f56cd Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Wed, 9 Oct 2024 09:23:06 +0200 Subject: [PATCH] procedure.find_column raise NotFound to fit AR interface --- app/models/concerns/columns_concern.rb | 8 ++++++-- spec/models/concerns/columns_concern_spec.rb | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app/models/concerns/columns_concern.rb b/app/models/concerns/columns_concern.rb index a3c62fe50..d21cb1cba 100644 --- a/app/models/concerns/columns_concern.rb +++ b/app/models/concerns/columns_concern.rb @@ -9,8 +9,12 @@ module ColumnsConcern # instead, we are using h_id == { procedure_id:, column_id: } # another way to find a column is to look for its label def find_column(h_id: nil, label: nil) - return columns.find { _1.h_id == h_id } if h_id.present? - return columns.find { _1.label == label } if label.present? + column = columns.find { _1.h_id == h_id } if h_id.present? + column = columns.find { _1.label == label } if label.present? + + raise ActiveRecord::RecordNotFound if column.nil? + + column end def columns diff --git a/spec/models/concerns/columns_concern_spec.rb b/spec/models/concerns/columns_concern_spec.rb index 63ac65622..e7f167865 100644 --- a/spec/models/concerns/columns_concern_spec.rb +++ b/spec/models/concerns/columns_concern_spec.rb @@ -1,6 +1,22 @@ # frozen_string_literal: true describe ColumnsConcern do + describe '#find_column' do + let(:procedure) { build(:procedure) } + let(:notifications_column) { procedure.notifications_column } + + it do + label = notifications_column.label + expect(procedure.find_column(label:)).to eq(notifications_column) + + h_id = notifications_column.h_id + expect(procedure.find_column(h_id:)).to eq(notifications_column) + + unknwon = 'unknown' + expect { procedure.find_column(h_id: unknwon) }.to raise_error(ActiveRecord::RecordNotFound) + end + end + describe "#columns" do subject { procedure.columns }