Skip to content

Commit f600d16

Browse files
authored
Merge pull request #115 from dblock/view-paths
Added Config#view_paths to be used in Rack middleware.
2 parents b0ca359 + 96ef320 commit f600d16

File tree

9 files changed

+85
-40
lines changed

9 files changed

+85
-40
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2020-04-20 08:35:29 -0400 using RuboCop version 0.81.0.
3+
# on 2020-04-26 17:46:16 -0400 using RuboCop version 0.81.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
* [#113](https://github.com/slack-ruby/slack-ruby-bot-server/pull/113): Added support for intervals with `.every` - [@dblock](https://github.com/dblock).
77
* [#112](https://github.com/slack-ruby/slack-ruby-bot-server/pull/112): Added support for multiple `.on` events as an argument - [@dblock](https://github.com/dblock).
88
* [#111](https://github.com/slack-ruby/slack-ruby-bot-server/pull/111): Removed dependency on Virtus - [@dblock](https://github.com/dblock).
9-
* [#110](https://github.com/slack-ruby/slack-ruby-bot-server/pull/110): Fix ActiveRecord sample app - [@CeeBeeUK](https://github.com/CeeBeeUK).
10-
* [#114](https://github.com/slack-ruby/slack-ruby-bot-server/pull/114): Use `require_relative` to prevent local collisions - [@dblock](https://github.com/dblock).
9+
* [#110](https://github.com/slack-ruby/slack-ruby-bot-server/pull/110): Fixed ActiveRecord sample app - [@CeeBeeUK](https://github.com/CeeBeeUK).
10+
* [#114](https://github.com/slack-ruby/slack-ruby-bot-server/pull/114): Used `require_relative` to prevent local collisions - [@dblock](https://github.com/dblock).
11+
* [#115](https://github.com/slack-ruby/slack-ruby-bot-server/pull/115): Added `Config#view_paths` to be used by Rack middleware - [@dblock](https://github.com/dblock).
1112

1213
#### 0.11.1 (2019/5/17)
1314

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,15 @@ SlackRubyBotServer::Service.instance.url # https://www.example.com
266266

267267
### HTML Templates
268268

269-
This library provides a [default HTML template and JS scripts](public) that implement the "Add to Slack" button workflow. Customize your pages by adding a `public` directory in your application and starting with a [index.html.erb](public/index.html.erb) template. The `views` and `public` folders are [loaded by default](lib/slack-ruby-bot-server/api/middleware.rb#L32).
269+
This library provides a [default HTML template and JS scripts](public) that implement the "Add to Slack" button workflow. Customize your pages by adding a `public` directory in your application and starting with a [index.html.erb](public/index.html.erb) template. The application's `views` and `public` folders are [loaded by default](lib/slack-ruby-bot-server/api/middleware.rb#L32).
270+
271+
You can add to or override template paths as follows.
272+
273+
`ruby
274+
SlackRubyBotServer.configure do |config|
275+
config.view_paths << File.expand_path(File.join(__dir__, 'public'))
276+
end
277+
```
270278
271279
### Access Tokens
272280

lib/slack-ruby-bot-server/api/middleware.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ def self.instance
3030
end
3131

3232
use Rack::ServerPages do |config|
33-
config.view_path = [
34-
'views', # relative to Dir.pwd
35-
'public', # relative to Dir.pwd
36-
File.expand_path(File.join(__dir__, '../../../public')) # built-in fallback
37-
]
33+
config.view_path = SlackRubyBotServer::Config.view_paths
3834
end
3935

4036
run Middleware.new

lib/slack-ruby-bot-server/config.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ module Config
55
attr_accessor :server_class
66
attr_accessor :service_class
77
attr_accessor :database_adapter
8+
attr_accessor :view_paths
89

910
def reset!
1011
self.server_class = SlackRubyBotServer::Server
1112
self.service_class = SlackRubyBotServer::Service
13+
14+
self.view_paths = [
15+
'views',
16+
'public',
17+
File.expand_path(File.join(__dir__, '../../public'))
18+
]
19+
1220
self.database_adapter = if defined?(::Mongoid)
1321
:mongoid
1422
elsif defined?(::ActiveRecord)

public/index.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<title>Slack Ruby Bot Server</title>
44
<script src="/scripts/jquery-1.7.1.min.js"></script>
55
<script src="/scripts/url.min.js"></script>
6+
<script src="/scripts/messages.js"></script>
67
<script src="/scripts/register.js"></script>
78
<script src="/scripts/stats.js"></script>
89
</head>

public/scripts/messages.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var SlackRubyBotServer = {};
2+
3+
$(document).ready(function() {
4+
5+
SlackRubyBotServer.message = function(text) {
6+
$('#messages').fadeOut('slow', function() {
7+
$('#messages').fadeIn('slow').html(text)
8+
});
9+
};
10+
11+
SlackRubyBotServer.error = function(xhr) {
12+
try {
13+
var message;
14+
if (xhr.responseText) {
15+
var rc = JSON.parse(xhr.responseText);
16+
if (rc && rc.message) {
17+
message = rc.message;
18+
if (message == 'invalid_code') {
19+
message = 'The code returned from the OAuth workflow was invalid.'
20+
} else if (message == 'code_already_used') {
21+
message = 'The code returned from the OAuth workflow has already been used.'
22+
}
23+
}
24+
}
25+
26+
SlackRubyBotServer.message(message || xhr.statusText || xhr.responseText || 'Unexpected Error');
27+
28+
} catch(err) {
29+
SlackRubyBotServer.message(err.message);
30+
}
31+
};
32+
33+
});

public/scripts/register.js

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,4 @@
1-
var SlackRubyBotServer = {};
2-
31
$(document).ready(function() {
4-
5-
SlackRubyBotServer.message = function(text) {
6-
$('#messages').fadeOut('slow', function() {
7-
$('#messages').fadeIn('slow').html(text)
8-
});
9-
};
10-
11-
SlackRubyBotServer.error = function(xhr) {
12-
try {
13-
var message;
14-
if (xhr.responseText) {
15-
var rc = JSON.parse(xhr.responseText);
16-
if (rc && rc.message) {
17-
message = rc.message;
18-
if (message == 'invalid_code') {
19-
message = 'The code returned from the OAuth workflow was invalid.'
20-
} else if (message == 'code_already_used') {
21-
message = 'The code returned from the OAuth workflow has already been used.'
22-
}
23-
}
24-
}
25-
26-
SlackRubyBotServer.message(message || xhr.statusText || xhr.responseText || 'Unexpected Error');
27-
28-
} catch(err) {
29-
SlackRubyBotServer.message(err.message);
30-
}
31-
};
32-
332
// Slack OAuth
343
var code = $.url('?code')
354
var state = $.url('?state')

spec/api/middleware_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'spec_helper'
2+
3+
describe SlackRubyBotServer::Api::Middleware do
4+
def middleware_classes(app)
5+
r = [app]
6+
7+
while (next_app = r.last.instance_variable_get(:@app))
8+
r << next_app
9+
end
10+
11+
r
12+
end
13+
14+
context 'overriding view_paths' do
15+
after do
16+
SlackRubyBotServer.config.reset!
17+
end
18+
it 'uses custom view paths' do
19+
SlackRubyBotServer.configure do |config|
20+
config.view_paths << 'custom'
21+
end
22+
server_pages = middleware_classes(SlackRubyBotServer::Api::Middleware.instance)[-2]
23+
expect(server_pages).to be_a Rack::ServerPages
24+
config = server_pages.instance_variable_get(:@config)
25+
expect(config[:view_path].count).to eq 4
26+
expect(config[:view_path][-1]).to eq 'custom'
27+
end
28+
end
29+
end

0 commit comments

Comments
 (0)