зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-08-28 15:26:18 +03:00
Halt processing in UpdatesController when callback returns false
Этот коммит содержится в:
@@ -1,5 +1,6 @@
|
||||
require 'abstract_controller'
|
||||
require 'active_support/callbacks'
|
||||
require 'active_support/version'
|
||||
|
||||
module Telegram
|
||||
module Bot
|
||||
@@ -7,6 +8,16 @@ module Telegram
|
||||
abstract!
|
||||
|
||||
include AbstractController::Callbacks
|
||||
# Redefine callbacks with default terminator.
|
||||
if ActiveSupport.gem_version >= Gem::Version.new('5')
|
||||
define_callbacks :process_action,
|
||||
skip_after_callbacks_if_terminated: true
|
||||
else
|
||||
define_callbacks :process_action,
|
||||
terminator: ->(_, result) { result == false },
|
||||
skip_after_callbacks_if_terminated: true
|
||||
end
|
||||
|
||||
include AbstractController::Translation
|
||||
|
||||
require 'telegram/bot/updates_controller/log_subscriber'
|
||||
|
||||
@@ -143,4 +143,45 @@ RSpec.describe Telegram::Bot::UpdatesController do
|
||||
it { should eq bot.username }
|
||||
end
|
||||
end
|
||||
|
||||
describe '#process_action' do
|
||||
subject { -> { instance.process_action(:action) } }
|
||||
|
||||
context 'when callbacks are defined' do
|
||||
let(:instance) { controller_class.new }
|
||||
let(:controller_class) do
|
||||
Class.new(described_class) do
|
||||
before_action :hook
|
||||
attr_reader :acted, :hooked
|
||||
|
||||
def action
|
||||
@acted = true
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def hook
|
||||
@hooked = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it { should change(instance, :hooked).to true }
|
||||
it { should change(instance, :acted).to true }
|
||||
|
||||
context 'when callback returns false' do
|
||||
before do
|
||||
controller_class.prepend(Module.new do
|
||||
def hook
|
||||
super
|
||||
false
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
it { should change(instance, :hooked).to true }
|
||||
it { should_not change(instance, :acted).from nil }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Ссылка в новой задаче
Block a user