Skip to content

Commit

Permalink
Dummy Space model. Set current space based on subdomain
Browse files Browse the repository at this point in the history
  • Loading branch information
fbacall committed Jan 20, 2025
1 parent ce59272 commit e7fce9f
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ See [here](docs/customization.md) for an overview of how you can customize your
## API

See [here](docs/api.md) for details on programmatic access to TeSS via its API.

## Multi-space TeSS

See [here](docs/mtessx.md) for details on configuring multi-space TeSS.
11 changes: 11 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ApplicationController < ActionController::Base
# User auth should be required in the web interface as well; it's here rather than in routes so that it
# doesn't override the token auth, above.
before_action :authenticate_user!, except: [:index, :show, :embed, :calendar, :check_exists, :handle_error, :count, :redirect]
before_action :set_space
before_action :set_current_user

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

def set_space
@current_space = Space.find(request.subdomain) if request.subdomain
end

def current_space
@current_space
end

helper_method :current_space

def set_current_user
User.current_user = current_user
if TeSS::Config.sentry_enabled?
Expand Down
12 changes: 12 additions & 0 deletions app/models/space.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Space
include ActiveModel::Model

attr_accessor :id, :name, :logo

def self.find(id)
return nil if id.nil?
if TeSS::Config.spaces&.key?(id)
self.new(TeSS::Config.spaces[id].merge(id: id))
end
end
end
14 changes: 12 additions & 2 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,20 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<% if defined? TeSS::Config.site['logo'] and !TeSS::Config.site['logo'].blank? %>
<% if current_space %>
<a class="navbar-brand" href="/">
<%= image_tag(TeSS::Config.site['logo'], alt: TeSS::Config.site['logo_alt']) %>
<% if current_space.logo %>
<%= image_tag(current_space.logo, alt: current_space.name) %>
<% else %>
<%= current_space.name %>
<% end %>
</a>
<% else %>
<% if defined? TeSS::Config.site['logo'] and !TeSS::Config.site['logo'].blank? %>
<a class="navbar-brand" href="/">
<%= image_tag(TeSS::Config.site['logo'], alt: TeSS::Config.site['logo_alt']) %>
</a>
<% end %>
<% end %>
</div>

Expand Down
1 change: 1 addition & 0 deletions config/initializers/hosts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Rails.application.config.hosts << /.*\.mytess\.training(\:\d+)?/
35 changes: 35 additions & 0 deletions docs/mtessx.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Development

## Simulating subdomains

A multi-space enabled TeSS will set the current space based on the subdomain of the incoming request.

To allow your local development server to respond to requests with a subdomain,
edit your hosts file (`/etc/hosts` on Linux) to include something like the following, e.g.:

```
127.0.0.1 plants.mytess.training
127.0.0.1 astro.mytess.training
127.0.0.1 whatever.mytess.training
```

This will ensure that if you visit e.g. plants.mytess.training, your browser will route it to your local development server
(you may need to restart your browser after changing the hosts file).

Edit `config/hosts.rb` to ensure your Rails application accepts requests to the hosts provided above.

## Configuring spaces

Edit `tess.yml` to define spaces, e.g.:

```
spaces:
plants:
name: TeSS Plants Community
astro:
name: TeSS Space Community
whatever:
name: The Whatever Space
```

(Your server will need to be restarted after changing this file)
7 changes: 7 additions & 0 deletions test/config/test_tess.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ default: &default
enabled: false
stale_threshold: 0.3
rejected_threshold: 0.3
spaces:
plants:
name: TeSS Plants Community
astro:
name: TeSS Space Community
other:
name: Other TeSS Community
development:
<<: *default

Expand Down
12 changes: 12 additions & 0 deletions test/controllers/static_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,16 @@ class StaticControllerTest < ActionController::TestCase
end
end
end

test 'sets current space based on subdomain' do
old_host = @request.host
get :home
assert_nil assigns(:current_space)

@request.host = 'plants.mytess.training'
get :home
assert 'plants', assigns(:current_space).id
ensure
@request.host = old_host
end
end

0 comments on commit e7fce9f

Please sign in to comment.