Skip to content

Commit

Permalink
Merge pull request #54 from alexagranov/no_mongoid_exception_on_activ…
Browse files Browse the repository at this point in the history
…erecord

remove one more Mongoid dependency when rescuing from _invoke
  • Loading branch information
dblock authored Mar 29, 2017
2 parents e0c867e + 656e53c commit a7a90c4
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### 0.6.1 (Next)

* Your contribution here.
* [#54](https://github.com/slack-ruby/slack-ruby-bot-server/pull/54): Removing one more Mongoid dependency when rescuing from _invoke - [@alexagranov](https://github.com/alexagranov).
* [#53](https://github.com/slack-ruby/slack-ruby-bot-server/pull/53): No need for `otr-activerecord` if using activerecord under Rails - [@alexagranov](https://github.com/alexagranov).

#### 0.6.0 (3/12/2017)
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'rspec/core'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList['spec/**/*_spec.rb']
spec.pattern = FileList['spec/**/*_spec.rb'].exclude(%r{ext\/(?!#{ENV['DATABASE_ADAPTER']})})
end

require 'rubocop/rake_task'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module SlackRubyBot
module Commands
class Base
class << self
alias _invoke invoke

def invoke(client, data)
_invoke client, data
rescue StandardError => e
logger.info "#{name.demodulize.upcase}: #{client.owner}, #{e.class}: #{e}"
client.say(channel: data.channel, text: e.message, gif: 'error')
true
end
end
end
end
end
5 changes: 2 additions & 3 deletions lib/slack-ruby-bot-server/ext/slack-ruby-bot.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
%w(client commands/base).each do |ext|
require_relative "slack-ruby-bot/#{ext}"
end
require_relative 'slack-ruby-bot/client'
require_relative "#{SlackRubyBotServer::Config.database_adapter}/slack-ruby-bot/commands/base"
19 changes: 19 additions & 0 deletions spec/ext/activerecord/commands_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'spec_helper'

describe SlackRubyBot::Commands::Base do
let(:client) { SlackRubyBot::Client.new }
let(:message_hook) { SlackRubyBot::Hooks::Message.new }

context 'exception handling' do
context 'StandardError' do
before do
allow(SlackRubyBot::Commands::Base).to receive(:_invoke).and_raise('mock error')
end

it 'responds to channel with exception message' do
expect(client).to receive(:say).with(channel: 'channel', text: 'mock error', gif: 'error')
message_hook.call(client, Hashie::Mash.new(text: 'raising exception', channel: 'channel', user: 'user'))
end
end
end
end
36 changes: 36 additions & 0 deletions spec/ext/mongoid/commands_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'spec_helper'

describe SlackRubyBot::Commands::Base do
let(:client) { SlackRubyBot::Client.new }
let(:message_hook) { SlackRubyBot::Hooks::Message.new }

context 'exception handling' do
context 'StandardError' do
before do
allow(SlackRubyBot::Commands::Base).to receive(:_invoke).and_raise('mock error')
end

it 'responds to channel with exception message' do
expect(client).to receive(:say).with(channel: 'channel', text: 'mock error', gif: 'error')
message_hook.call(client, Hashie::Mash.new(text: 'raising exception', channel: 'channel', user: 'user'))
end
end

context 'Mongoid::Errors::Validations' do
class MockDocument
include Mongoid::Document
validates_presence_of :mock_id
end
let(:mock_document) { MockDocument.new }

before do
allow(SlackRubyBot::Commands::Base).to receive(:_invoke) { mock_document.save! }
end

it 'responds to channel with exception message' do
expect(client).to receive(:say).with(channel: 'channel', text: "undefined method `mock_id' for #{mock_document.inspect}", gif: 'error')
message_hook.call(client, Hashie::Mash.new(text: 'raising exception', channel: 'channel', user: 'user'))
end
end
end
end

0 comments on commit a7a90c4

Please sign in to comment.