diff --git a/CHANGELOG.md b/CHANGELOG.md index 849544a..a64bfb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [0.11.2] - 2020-07-22 +### Changed +- Improve `from:` option for `:fetch_model` step, at `:sequel_models` plugin to also accept a Sequel Dataset + ## [0.11.1] - 2020-01-09 ### Changed - Improve custom `rspec` matchers for testing field presence on schemas diff --git a/lib/pathway/plugins/sequel_models.rb b/lib/pathway/plugins/sequel_models.rb index 8287fcc..046549e 100644 --- a/lib/pathway/plugins/sequel_models.rb +++ b/lib/pathway/plugins/sequel_models.rb @@ -63,10 +63,13 @@ module InstanceMethods delegate :db => :model_class def fetch_model(state, from: model_class, search_by: search_field, using: search_by, to: result_key, overwrite: false, error_message: nil) - error_message ||= if from != model_class - Inflector.humanize(Inflector.underscore(Inflector.demodulize(from.name))) + ' not found' - else + error_message ||= if (from == model_class) model_not_found + elsif from.respond_to?(:name) || from.respond_to?(:model) + from_name = (from.respond_to?(:name) ? from : from.model).name + Inflector.humanize(Inflector.underscore(Inflector.demodulize(from_name))) + ' not found' + else + 'Register not found' end if state[to].nil? || overwrite diff --git a/lib/pathway/version.rb b/lib/pathway/version.rb index fa2e292..8cd1e58 100644 --- a/lib/pathway/version.rb +++ b/lib/pathway/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Pathway - VERSION = '0.11.1' + VERSION = '0.11.2' end diff --git a/spec/plugins/sequel_models_spec.rb b/spec/plugins/sequel_models_spec.rb index 1514e67..180efdd 100644 --- a/spec/plugins/sequel_models_spec.rb +++ b/spec/plugins/sequel_models_spec.rb @@ -303,7 +303,8 @@ class InvalidOperation < MyOperation end describe '#fetch_model' do - let(:from_model) { double(name: 'Model') } + let(:other_model) { double(name: 'OtherModel') } + let(:dataset) { double(model: other_model) } let(:object) { double } it "fetches an instance through 'model_class' into result key" do @@ -312,18 +313,31 @@ class InvalidOperation < MyOperation expect(operation.fetch_model(input: {email: key}).value[:my_model]).to eq(object) end - it "fetches an instance through 'model_class' and sets result key using an overrided search column, input key and 'from' model when provided" do - expect(from_model).to receive(:first).with(pk: 'foo').and_return(object) - expect(MyModel).to_not receive(:first) + context "when proving and external repository through 'from:'" do + it "fetches an instance through 'model_class' and sets result key using an overrided search column, input key and 'from' model class" do + expect(other_model).to receive(:first).with(pk: 'foo').and_return(object) + expect(MyModel).to_not receive(:first) - state = { input: { myid: 'foo' } } - result = operation - .fetch_model(state, from: from_model, using: :myid, search_by: :pk) - .value[:my_model] + state = { input: { myid: 'foo' } } + result = operation + .fetch_model(state, from: other_model, using: :myid, search_by: :pk) + .value[:my_model] - expect(result).to eq(object) - end + expect(result).to eq(object) + end + + it "fetches an instance through 'model_class' and sets result key using an overrided search column, input key and 'from' dataset" do + expect(dataset).to receive(:first).with(pk: 'foo').and_return(object) + expect(MyModel).to_not receive(:first) + state = { input: { myid: 'foo' } } + result = operation + .fetch_model(state, from: dataset, using: :myid, search_by: :pk) + .value[:my_model] + + expect(result).to eq(object) + end + end it "fetches an instance through 'model_class' and sets result key using an overrided search column and input key with only :search_by is provided" do expect(MyModel).to receive(:first).with(name: 'foobar').and_return(object)