diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fa986d..4914e19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [0.6.1] - 2018-03-16 +### Changed +- Update default error message for `:fetch_model` step, at `sequel_models` plugin, to indicate the model's name +- Add `:error_message` option for `sequel_models` plugin initializer to set the default error message +- Add `:error_message` option for `:fetch_model` step to override the default error message + ## [0.6.0] - 2018-03-01 ### Changed - Replace unmaintained `inflecto` gem with `dry-inflector` diff --git a/lib/pathway/plugins/sequel_models.rb b/lib/pathway/plugins/sequel_models.rb index 031e72d..e064dad 100644 --- a/lib/pathway/plugins/sequel_models.rb +++ b/lib/pathway/plugins/sequel_models.rb @@ -22,12 +22,13 @@ def after_commit(&bl) end module ClassMethods - attr_accessor :model_class, :search_field + attr_accessor :model_class, :search_field, :model_not_found - def model(model_class, search_by: model_class.primary_key, set_result_key: true) - self.model_class = model_class - self.search_field = search_by - self.result_key = Inflector.underscore(Inflector.demodulize(model_class.name)).to_sym if set_result_key + def model(model_class, search_by: model_class.primary_key, set_result_key: true, error_message: nil) + self.model_class = model_class + self.search_field = search_by + self.result_key = Inflector.underscore(Inflector.demodulize(model_class.name)).to_sym if set_result_key + self.model_not_found = error_message || Inflector.humanize(Inflector.underscore(Inflector.demodulize(model_class.name))) + ' not found' end def inherited(subclass) @@ -39,21 +40,27 @@ def inherited(subclass) module InstanceMethods extend Forwardable - delegate %i[model_class search_field] => 'self.class' + delegate %i[model_class search_field model_not_found] => 'self.class' delegate :db => :model_class - def fetch_model(state, from: model_class, search_by: search_field, using: search_by, to: result_key, overwrite: false) + 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 + model_not_found + end + if state[to].nil? || overwrite - wrap_if_present(state[:input][using]) - .then { |key| find_model_with(key, from, search_by) } + wrap_if_present(state[:input][using], message: error_message) + .then { |key| find_model_with(key, from, search_by, error_message) } .then { |model| state.update(to => model) } else state end end - def find_model_with(key, dataset = model_class, column = search_field) - wrap_if_present(dataset.first(column => key)) + def find_model_with(key, dataset = model_class, column = search_field, error_message = nil) + wrap_if_present(dataset.first(column => key), message: error_message) end end diff --git a/lib/pathway/version.rb b/lib/pathway/version.rb index 879c086..92ee929 100644 --- a/lib/pathway/version.rb +++ b/lib/pathway/version.rb @@ -1,3 +1,3 @@ module Pathway - VERSION = '0.6.0' + VERSION = '0.6.1' end diff --git a/spec/plugins/sequel_models_spec.rb b/spec/plugins/sequel_models_spec.rb index b0f4dd8..5c03a5b 100644 --- a/spec/plugins/sequel_models_spec.rb +++ b/spec/plugins/sequel_models_spec.rb @@ -114,7 +114,7 @@ def send_emails(my_model:,**) end describe '#fetch_model' do - let(:repository) { double } + let(:from_model) { double(name: 'Model') } let(:object) { double } it "fetches an instance through 'model_class' into result key" do @@ -123,13 +123,13 @@ def send_emails(my_model:,**) 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 repository when provdided" do - expect(repository).to receive(:first).with(pk: 'foo').and_return(object) + 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) state = { input: { myid: 'foo' } } result = operation - .fetch_model(state, from: repository, using: :myid, search_by: :pk) + .fetch_model(state, from: from_model, using: :myid, search_by: :pk) .value[:my_model] expect(result).to eq(object) @@ -166,6 +166,7 @@ def send_emails(my_model:,**) expect(result).to be_an(Result::Failure) expect(result.error).to be_an(Pathway::Error) expect(result.error.type).to eq(:not_found) + expect(result.error.message).to eq('My model not found') end it 'returns an error without hitting the database when search key is nil', :aggregate_failures do @@ -176,6 +177,7 @@ def send_emails(my_model:,**) expect(result).to be_an(Result::Failure) expect(result.error).to be_an(Pathway::Error) expect(result.error.type).to eq(:not_found) + expect(result.error.message).to eq('My model not found') end end