diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 92874eb0..afe3a086 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -2,7 +2,7 @@ class ProjectsController < ApplicationController before_action :authenticate_employee, only: %i[new create] - before_action :authenticate_responsible_user, only: %i[edit update] + before_action :authenticate_responsible_user, only: %i[edit update destroy] def index @projects = Project.all @@ -49,6 +49,11 @@ def update end end + def destroy + @project.destroy + redirect_back fallback_location: projects_url, notice: 'Project was successfully deleted.' + end + private def authenticate_responsible_user diff --git a/app/controllers/servers_controller.rb b/app/controllers/servers_controller.rb index 4cfd4bd5..2d51e09d 100644 --- a/app/controllers/servers_controller.rb +++ b/app/controllers/servers_controller.rb @@ -117,7 +117,7 @@ def update def destroy @server.destroy respond_to do |format| - format.html { redirect_to servers_url, notice: 'Server was successfully destroyed.' } + format.html { redirect_to servers_path, notice: 'Server was successfully deleted.' } format.json { head :no_content } end end diff --git a/app/models/project.rb b/app/models/project.rb index b64d515d..86c7e991 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -6,6 +6,7 @@ class Project < ApplicationRecord join_table: :responsible_users, foreign_key: :user_id, association_foreign_key: :project_id + has_many :virtual_machine_configs validates :name, presence: true validates :description, presence: true diff --git a/app/views/projects/_table.html.erb b/app/views/projects/_table.html.erb index d886e3fd..fba5d37b 100644 --- a/app/views/projects/_table.html.erb +++ b/app/views/projects/_table.html.erb @@ -15,7 +15,7 @@ <%# https://getbootstrap.com/docs/4.0/content/typography/#inline %> diff --git a/app/views/projects/show.html.erb b/app/views/projects/show.html.erb index b57eee30..416bc612 100644 --- a/app/views/projects/show.html.erb +++ b/app/views/projects/show.html.erb @@ -1,38 +1,38 @@

<%= I18n.t('project.title') %>

- - - - - - - - - - - - - - - -
<%= Project.human_attribute_name('name') %><%= @project.name %>
<%= Project.human_attribute_name('description') %><%= @project.description %>
<%= Project.human_attribute_name('responsible_users') %> - <%# https://getbootstrap.com/docs/4.0/content/typography/#inline %> -
    - <% @project.responsible_users.each do |user| %> -
  • <%= user.name %>
  • - <% end %> -
-
+
+
<%= Project.human_attribute_name('name') %>
+
<%= @project.name %>
+
<%= Project.human_attribute_name('description') %>
+
<%= @project.description %>
+
VMs
+
+ <% if @project.virtual_machine_configs.each do |config| %> + <%= link_to config.name, controller: "vms", action: "show", id: config.name %> + <% end.empty? %> + --- + <% end %> +
+
<%= Project.human_attribute_name('responsible_users') %>
+
+ <%# https://getbootstrap.com/docs/4.0/content/typography/#inline %> + +
+
- <%= link_to 'All Projects', - projects_path, - class: 'btn btn-primary' - %> - <% if @project.responsible_users.include? current_user %> - <%= link_to 'Edit', - edit_project_path(@project), - class: 'btn btn-primary' - %> + <% if @project.responsible_users.include? current_user %> + <%= link_to fa_icon('pencil', text: 'Edit'), edit_project_path(@project), + class: 'btn btn-primary' %> + <% vm_names = @project.virtual_machine_configs.map(&:name).join(", ") %> + <% warning = "The following VMs will loose their project:\n" + vm_names %> + <%= link_to fa_icon('trash', text: 'Delete'), @project, method: :delete, + data: {confirm: ["Are you sure you want to delete this project?\n", + "#{warning if vm_names.present?}"].join('')}, + class: 'btn btn-danger' %> <% end %>
diff --git a/config/routes.rb b/config/routes.rb index da44e792..2f80f016 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,7 +11,7 @@ # '/app_settings/...' resources :app_settings, only: %i[update edit] # '/projects/...' - resources :projects, except: :destroy + resources :projects # '/servers/...' resources :servers # '/hosts/...' diff --git a/spec/views/projects/show.html.erb_spec.rb b/spec/views/projects/show.html.erb_spec.rb index 0c530662..067035ac 100644 --- a/spec/views/projects/show.html.erb_spec.rb +++ b/spec/views/projects/show.html.erb_spec.rb @@ -28,12 +28,8 @@ expect(rendered).to include project.responsible_users[1].name end - it 'has a link to go to the all projects page' do - expect(rendered).to have_link('All Projects', href: projects_path) - end - it 'shows a link to edit the project' do - expect(rendered).to have_link('Edit', href: edit_project_path(project)) + expect(rendered).to have_link(href: edit_project_path(project)) end end @@ -41,7 +37,16 @@ let(:current_user) { FactoryBot.create :user } it 'does not show a link to edit the project' do - expect(rendered).not_to have_link('Edit', href: edit_project_path(project)) + expect(rendered).not_to have_link(href: edit_project_path(project)) + end + end + + context 'with a project that is connected to VM configs' do + it 'shows names of VMs with links' do + config = FactoryBot.create :virtual_machine_config, responsible_users: [current_user] + project.virtual_machine_configs << config + render + expect(rendered).to have_link(config.name, href: vm_path(config.name)) end end end