Skip to content

Commit

Permalink
Allow from: option on 'fetch_model' step to take datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloh committed Jul 22, 2020
1 parent e31d6f3 commit d98cbb9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 6 additions & 3 deletions lib/pathway/plugins/sequel_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/pathway/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Pathway
VERSION = '0.11.1'
VERSION = '0.11.2'
end
34 changes: 24 additions & 10 deletions spec/plugins/sequel_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit d98cbb9

Please sign in to comment.