Skip to content

Commit fd1cee2

Browse files
committed
Added Config#view_paths to be used in Rack middleware.
1 parent b0ca359 commit fd1cee2

File tree

6 files changed

+51
-9
lines changed

6 files changed

+51
-9
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)

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)