Skip to content

Commit 7b14f13

Browse files
committed
Dummy Space model. Set current space based on subdomain
1 parent b038ec1 commit 7b14f13

8 files changed

Lines changed: 94 additions & 2 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ See [here](docs/customization.md) for an overview of how you can customize your
5757
## API
5858

5959
See [here](docs/api.md) for details on programmatic access to TeSS via its API.
60+
61+
## Multi-space TeSS
62+
63+
See [here](docs/mtessx.md) for details on configuring multi-space TeSS.

app/controllers/application_controller.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ApplicationController < ActionController::Base
1919
# User auth should be required in the web interface as well; it's here rather than in routes so that it
2020
# doesn't override the token auth, above.
2121
before_action :authenticate_user!, except: [:index, :show, :embed, :calendar, :check_exists, :handle_error, :count, :redirect]
22+
before_action :set_space
2223
before_action :set_current_user
2324

2425
# Should prevent forgery errors for JSON posts.
@@ -106,6 +107,16 @@ def user_not_authorized(exception)
106107
handle_error(:forbidden, t("#{policy_name}.#{exception.query}", scope: 'pundit', default: :default))
107108
end
108109

110+
def set_space
111+
@current_space = Space.find(request.subdomain) if request.subdomain
112+
end
113+
114+
def current_space
115+
@current_space
116+
end
117+
118+
helper_method :current_space
119+
109120
def set_current_user
110121
User.current_user = current_user
111122
if TeSS::Config.sentry_enabled?

app/models/space.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Space
2+
include ActiveModel::Model
3+
4+
attr_accessor :id, :name, :logo
5+
6+
def self.find(id)
7+
return nil if id.nil?
8+
if TeSS::Config.spaces&.key?(id)
9+
self.new(TeSS::Config.spaces[id].merge(id: id))
10+
end
11+
end
12+
end

app/views/layouts/_header.html.erb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,20 @@
2020
<span class="icon-bar"></span>
2121
<span class="icon-bar"></span>
2222
</button>
23-
<% if defined? TeSS::Config.site['logo'] and !TeSS::Config.site['logo'].blank? %>
23+
<% if current_space %>
2424
<a class="navbar-brand" href="/">
25-
<%= image_tag(TeSS::Config.site['logo'], alt: TeSS::Config.site['logo_alt']) %>
25+
<% if current_space.logo %>
26+
<%= image_tag(current_space.logo, alt: current_space.name) %>
27+
<% else %>
28+
<%= current_space.name %>
29+
<% end %>
2630
</a>
31+
<% else %>
32+
<% if defined? TeSS::Config.site['logo'] and !TeSS::Config.site['logo'].blank? %>
33+
<a class="navbar-brand" href="/">
34+
<%= image_tag(TeSS::Config.site['logo'], alt: TeSS::Config.site['logo_alt']) %>
35+
</a>
36+
<% end %>
2737
<% end %>
2838
</div>
2939

config/initializers/hosts.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Rails.application.config.hosts << /.*\.mytess\.training(\:\d+)?/

docs/mtessx.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Development
2+
3+
## Simulating subdomains
4+
5+
A multi-space enabled TeSS will set the current space based on the subdomain of the incoming request.
6+
7+
To allow your local development server to respond to requests with a subdomain,
8+
edit your hosts file (`/etc/hosts` on Linux) to include something like the following, e.g.:
9+
10+
```
11+
127.0.0.1 plants.mytess.training
12+
127.0.0.1 astro.mytess.training
13+
127.0.0.1 whatever.mytess.training
14+
```
15+
16+
This will ensure that if you visit e.g. plants.mytess.training, your browser will route it to your local development server
17+
(you may need to restart your browser after changing the hosts file).
18+
19+
Edit `config/hosts.rb` to ensure your Rails application accepts requests to the hosts provided above.
20+
21+
## Configuring spaces
22+
23+
Edit `tess.yml` to define spaces, e.g.:
24+
25+
```
26+
spaces:
27+
plants:
28+
name: TeSS Plants Community
29+
astro:
30+
name: TeSS Space Community
31+
whatever:
32+
name: The Whatever Space
33+
```
34+
35+
(Your server will need to be restarted after changing this file)

test/config/test_tess.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ default: &default
9999
enabled: false
100100
stale_threshold: 0.3
101101
rejected_threshold: 0.3
102+
spaces:
103+
plants:
104+
name: TeSS Plants Community
105+
astro:
106+
name: TeSS Space Community
107+
other:
108+
name: Other TeSS Community
102109
development:
103110
<<: *default
104111

test/controllers/static_controller_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,16 @@ class StaticControllerTest < ActionController::TestCase
327327
end
328328
end
329329
end
330+
331+
test 'sets current space based on subdomain' do
332+
old_host = @request.host
333+
get :home
334+
assert_nil assigns(:current_space)
335+
336+
@request.host = 'plants.mytess.training'
337+
get :home
338+
assert 'plants', assigns(:current_space).id
339+
ensure
340+
@request.host = old_host
341+
end
330342
end

0 commit comments

Comments
 (0)