Skip to content

Commit ff0ba1b

Browse files
committed
Basic CRUD for Spaces
1 parent 6d1415e commit ff0ba1b

21 files changed

Lines changed: 441 additions & 4 deletions

app/controllers/activities_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class ActivitiesController < ApplicationController
44
before_action :set_resource, only: [:index]
55
before_action :set_breadcrumbs, only: [:index]
66

7-
MODELS = %w[content_provider material collection event node workflow source learning_path learning_path_topic].freeze
7+
MODELS = %w[content_provider material collection event node workflow source learning_path learning_path_topic space].freeze
88

99
def show
1010
raise ActionController::RoutingError.new("") unless current_user&.is_admin?

app/controllers/application_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def user_not_authorized(exception)
108108
end
109109

110110
def set_space
111-
Space.current_space = Space.find_by_host(request.host)
111+
Space.current_space = TeSS::Config.feature['spaces'] ? Space.find_by_host(request.host) : Space.default
112112
end
113113

114114
def current_space
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# The controller for actions related to the Spaces model
2+
class SpacesController < ApplicationController
3+
before_action :feature_enabled?
4+
before_action :set_space, only: [:show, :edit, :update, :destroy]
5+
before_action :set_breadcrumbs
6+
7+
# GET /spaces
8+
def index
9+
@spaces = Space.all
10+
respond_to do |format|
11+
format.html
12+
end
13+
end
14+
15+
# GET /spaces/1
16+
def show
17+
respond_to do |format|
18+
format.html
19+
end
20+
end
21+
22+
# GET /spaces/new
23+
def new
24+
authorize Space
25+
@space = Space.new
26+
end
27+
28+
# GET /spaces/1/edit
29+
def edit
30+
authorize @space
31+
end
32+
33+
# POST /spaces
34+
def create
35+
authorize Space
36+
@space = Space.new(space_params)
37+
@space.user = current_user
38+
39+
respond_to do |format|
40+
if @space.save
41+
@space.create_activity :create, owner: current_user
42+
format.html { redirect_to @space, notice: 'Space was successfully created.' }
43+
else
44+
format.html { render :new }
45+
end
46+
end
47+
end
48+
49+
# PATCH/PUT /spaces/1
50+
def update
51+
authorize @space
52+
respond_to do |format|
53+
if @space.update(space_params)
54+
@space.create_activity(:update, owner: current_user) if @space.log_update_activity?
55+
format.html { redirect_to @space, notice: 'Space was successfully updated.' }
56+
else
57+
format.html { render :edit }
58+
end
59+
end
60+
end
61+
62+
# DELETE /spaces/1
63+
def destroy
64+
authorize @space
65+
@space.create_activity :destroy, owner: current_user
66+
@space.destroy
67+
respond_to do |format|
68+
format.html { redirect_to spaces_url, notice: 'Space was successfully deleted.' }
69+
end
70+
end
71+
72+
private
73+
74+
def set_space
75+
@space = Space.find(params[:id])
76+
end
77+
78+
def space_params
79+
permitted = [:title, :description, :theme, :image_url]
80+
permitted += [:host] if current_user.is_admin?
81+
params.require(:space).permit(*permitted)
82+
end
83+
end

app/helpers/application_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ def uri_state(uri, options = {})
186186
'ContentProvider' => TeSS::Config.placeholder['content_provider'],
187187
'Collection' => TeSS::Config.placeholder['collection'],
188188
'Trainer' => TeSS::Config.placeholder['person'],
189-
'Node' => 'elixir/elixir.svg'
189+
'Node' => 'elixir/elixir.svg',
190+
'Space'=> TeSS::Config.placeholder['content_provider'] # FIXME: Make a logo for spaces
190191
}.freeze
191192

192193
def get_image_url_for(resource)

app/helpers/spaces_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module SpacesHelper
2+
SPACES_INFO = 'info on spaces here'
3+
end

app/models/space.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
class Space < ApplicationRecord
2+
include PublicActivity::Common
3+
include LogParameterChanges
4+
5+
belongs_to :user
6+
27
has_image(placeholder: TeSS::Config.placeholder['content_provider'])
38

49
def self.current_space=(space)
@@ -16,4 +21,8 @@ def self.default
1621
def logo_alt
1722
"#{title} logo"
1823
end
24+
25+
def url
26+
"https://#{host}"
27+
end
1928
end

app/policies/space_policy.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class SpacePolicy < ApplicationPolicy
2+
3+
def create?
4+
@user && @user.has_role?(:admin)
5+
end
6+
7+
def edit?
8+
@user && (@user.is_owner?(@record) || manage?)
9+
end
10+
11+
def update?
12+
edit?
13+
end
14+
15+
def manage?
16+
@user && @user.is_admin?
17+
end
18+
19+
end

app/views/layouts/_user_menu.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@
5252
<%= link_to rails_admin_path, title: t('menu.user.view_admin_console') do %>
5353
<i class="fa fa-cog"></i> <%= t('menu.user.admin_console') %>
5454
<% end %>
55+
<% if TeSS::Config.feature['spaces'] %>
56+
<%= link_to spaces_path, title: t('menu.user.spaces_admin') do %>
57+
<i class="fa fa-globe"></i><%= t('menu.user.spaces_admin') %>
58+
<% end %>
59+
<% end %>
5560
<% end %>
5661
</li>
5762
<% end %>

app/views/spaces/_form.html.erb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<%= simple_form_for(@space) do |f| %>
2+
<%= render partial: 'common/error_summary', locals: { resource: @space } %>
3+
4+
<%= f.input :title %>
5+
6+
<%= f.input :description, as: :markdown_area, input_html: { rows: '10' } %>
7+
8+
<div class="form-group">
9+
<%= render partial: 'common/image_form', locals: { form: f } %>
10+
</div>
11+
12+
<div class="form-group">
13+
<%= f.submit ( f.object.new_record? ? "Register" : "Update") + " space", class: 'btn btn-primary' %>
14+
<%= link_to t('.cancel', default: t("helpers.links.cancel")),
15+
@space.new_record? ? spaces_path : space_path(@space), class: 'btn btn-default' %>
16+
</div>
17+
<% end %>

app/views/spaces/_space.html.erb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<% cache(space, expires_in: 6.hours) do %>
2+
<li class="masonry-brick media-item long">
3+
<%= link_to space, class: 'link-overlay' do %>
4+
<%= image_tag get_image_url_for(space), class: "media-image listing_image pull-right" %>
5+
<h4 class="mb-2 mt-3">
6+
<%= space.title %>
7+
</h4>
8+
9+
<%# if space.materials.length > 0 %>
10+
<!-- <div><strong><%#= pluralize(space.materials.length, 'training material') %></strong></div>-->
11+
<%# end %>
12+
13+
<%# if space.events.length > 0 %>
14+
<!-- <div>-->
15+
<!-- <strong><%#= pluralize(space.events.not_finished.length, 'event') %></strong>-->
16+
<!-- <em>(<%#= pluralize(space.events.finished.length, 'past event') %>)</em>-->
17+
<!-- </div>-->
18+
<%# end %>
19+
<% end %>
20+
</li>
21+
<% end %>

0 commit comments

Comments
 (0)