mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 21:11:10 +08:00
DEV: Make the model
step compatible with collections
This commit is contained in:

committed by
Loïc Guitaut

parent
a509441148
commit
bc4e016b49
@ -219,7 +219,7 @@ module Chat
|
|||||||
class ModelStep < Step
|
class ModelStep < Step
|
||||||
def call(instance, context)
|
def call(instance, context)
|
||||||
context[name] = super
|
context[name] = super
|
||||||
raise ArgumentError, "Model not found" if !context[name]
|
raise ArgumentError, "Model not found" if context[name].blank?
|
||||||
rescue ArgumentError => exception
|
rescue ArgumentError => exception
|
||||||
context[result_key].fail(exception: exception)
|
context[result_key].fail(exception: exception)
|
||||||
context.fail!
|
context.fail!
|
||||||
|
@ -76,6 +76,30 @@ RSpec.describe Chat::ServiceRunner do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class FailureWithCollectionModelService
|
||||||
|
include Chat::Service::Base
|
||||||
|
|
||||||
|
model :fake_model, :fetch_fake_model
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def fetch_fake_model
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class SuccessWithCollectionModelService
|
||||||
|
include Chat::Service::Base
|
||||||
|
|
||||||
|
model :fake_model, :fetch_fake_model
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def fetch_fake_model
|
||||||
|
[:models_found]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe ".call(service, &block)" do
|
describe ".call(service, &block)" do
|
||||||
subject(:runner) { described_class.call(service, object, &actions_block) }
|
subject(:runner) { described_class.call(service, object, &actions_block) }
|
||||||
|
|
||||||
@ -207,7 +231,8 @@ RSpec.describe Chat::ServiceRunner do
|
|||||||
end
|
end
|
||||||
BLOCK
|
BLOCK
|
||||||
|
|
||||||
context "when the service failed without a model" do
|
context "when fetching a single model" do
|
||||||
|
context "when the service fails without a model" do
|
||||||
let(:service) { FailureWithModelService }
|
let(:service) { FailureWithModelService }
|
||||||
|
|
||||||
it "runs the provided block" do
|
it "runs the provided block" do
|
||||||
@ -224,6 +249,25 @@ RSpec.describe Chat::ServiceRunner do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "when fetching a collection" do
|
||||||
|
context "when the service fails without a model" do
|
||||||
|
let(:service) { FailureWithCollectionModelService }
|
||||||
|
|
||||||
|
it "runs the provided block" do
|
||||||
|
expect(runner).to eq :no_model
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when the service does not fail with a model" do
|
||||||
|
let(:service) { SuccessWithCollectionModelService }
|
||||||
|
|
||||||
|
it "does not run the provided block" do
|
||||||
|
expect(runner).not_to eq :no_model
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "when using several actions together" do
|
context "when using several actions together" do
|
||||||
let(:service) { FailureService }
|
let(:service) { FailureService }
|
||||||
let(:actions) { <<-BLOCK }
|
let(:actions) { <<-BLOCK }
|
||||||
|
Reference in New Issue
Block a user