Skip to content

Commit 32fd480

Browse files
committed
Create real model for Space
1 parent 7b14f13 commit 32fd480

9 files changed

Lines changed: 89 additions & 26 deletions

File tree

app/controllers/application_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ def user_not_authorized(exception)
108108
end
109109

110110
def set_space
111-
@current_space = Space.find(request.subdomain) if request.subdomain
111+
Space.current_space = Space.find_by_host(request.host)
112112
end
113113

114114
def current_space
115-
@current_space
115+
Space.current_space
116116
end
117117

118118
helper_method :current_space

app/models/default_space.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class DefaultSpace
2+
class Image
3+
def url
4+
TeSS::Config.site['logo']
5+
end
6+
end
7+
8+
def title
9+
TeSS::Config.site['title_short']
10+
end
11+
12+
def logo_alt
13+
TeSS::Config.site['logo_alt']
14+
end
15+
16+
def image?
17+
true
18+
end
19+
20+
def image
21+
Image.new
22+
end
23+
end

app/models/space.rb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
class Space
2-
include ActiveModel::Model
1+
class Space < ApplicationRecord
2+
has_image(placeholder: TeSS::Config.placeholder['content_provider'])
33

4-
attr_accessor :id, :name, :logo
4+
def self.current_space=(space)
5+
Thread.current[:current_space] = space
6+
end
7+
8+
def self.current_space
9+
Thread.current[:current_space] || Space.default
10+
end
11+
12+
def self.default
13+
DefaultSpace.new
14+
end
515

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
16+
def logo_alt
17+
"#{title} logo"
1118
end
1219
end

app/views/layouts/_header.html.erb

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,13 @@
2020
<span class="icon-bar"></span>
2121
<span class="icon-bar"></span>
2222
</button>
23-
<% if current_space %>
24-
<a class="navbar-brand" href="/">
25-
<% if current_space.logo %>
26-
<%= image_tag(current_space.logo, alt: current_space.name) %>
27-
<% else %>
28-
<%= current_space.name %>
29-
<% end %>
30-
</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>
23+
<a class="navbar-brand" href="/">
24+
<% if current_space.image? %>
25+
<%= image_tag(current_space.image.url, alt: current_space.logo_alt) %>
26+
<% else %>
27+
<%= current_space.title %>
3628
<% end %>
37-
<% end %>
29+
</a>
3830
</div>
3931

4032
<div class="collapse navbar-collapse" id="navbar-collapse">

config/tess.example.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ default: &default
159159
bioschemas_testing: false
160160
learning_paths: false
161161
collection_curation: true
162+
spaces: false
162163
restrict_content_provider_selection: false
163164
user_ingestion_methods: ['bioschemas']
164165
placeholder:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class CreateSpaces < ActiveRecord::Migration[7.2]
2+
def change
3+
create_table :spaces do |t|
4+
t.string :title
5+
t.text :description
6+
t.string :host
7+
t.string :theme
8+
t.attachment :image
9+
t.references :user, foreign_key: true
10+
t.timestamps
11+
t.index ['host'], name: 'index_spaces_on_host', unique: true
12+
end
13+
end
14+
end

db/schema.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,22 @@
474474
t.index ["user_id"], name: "index_sources_on_user_id"
475475
end
476476

477+
create_table "spaces", force: :cascade do |t|
478+
t.string "title"
479+
t.text "description"
480+
t.string "host"
481+
t.string "theme"
482+
t.string "image_file_name"
483+
t.string "image_content_type"
484+
t.bigint "image_file_size"
485+
t.datetime "image_updated_at"
486+
t.bigint "user_id"
487+
t.datetime "created_at", null: false
488+
t.datetime "updated_at", null: false
489+
t.index ["host"], name: "index_spaces_on_host", unique: true
490+
t.index ["user_id"], name: "index_spaces_on_user_id"
491+
end
492+
477493
create_table "staff_members", force: :cascade do |t|
478494
t.string "name"
479495
t.string "role"
@@ -622,6 +638,7 @@
622638
add_foreign_key "nodes", "users"
623639
add_foreign_key "sources", "content_providers"
624640
add_foreign_key "sources", "users"
641+
add_foreign_key "spaces", "users"
625642
add_foreign_key "staff_members", "nodes"
626643
add_foreign_key "stars", "users"
627644
add_foreign_key "subscriptions", "users"

test/controllers/static_controller_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,11 @@ class StaticControllerTest < ActionController::TestCase
331331
test 'sets current space based on subdomain' do
332332
old_host = @request.host
333333
get :home
334-
assert_nil assigns(:current_space)
334+
assert_equal 'TTI', Space.current_space.title
335335

336336
@request.host = 'plants.mytess.training'
337337
get :home
338-
assert 'plants', assigns(:current_space).id
338+
assert_equal 'TeSS Plants Community', Space.current_space.title
339339
ensure
340340
@request.host = old_host
341341
end

test/fixtures/spaces.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
plants:
2+
title: TeSS Plants Community
3+
host: plants.mytess.training
4+
astro:
5+
title: TeSS Space Community
6+
host: space.mytess.training
7+
other:
8+
title: Other TeSS Community
9+
host: other.mytess.training

0 commit comments

Comments
 (0)