Skip to content

Commit

Permalink
Merge pull request #25 from pabloh/update_fetch_model_error_message
Browse files Browse the repository at this point in the history
Change default error message when :fetch_model fails
  • Loading branch information
pabloh committed Mar 16, 2018
2 parents 2bc362e + 3f967f2 commit c99d127
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
29 changes: 18 additions & 11 deletions lib/pathway/plugins/sequel_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/pathway/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Pathway
VERSION = '0.6.0'
VERSION = '0.6.1'
end
10 changes: 6 additions & 4 deletions spec/plugins/sequel_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit c99d127

Please sign in to comment.