Skip to content

Commit

Permalink
488/DeleteProjects (#502)
Browse files Browse the repository at this point in the history
* Add initial deletion for projects

* Refactor the projects index page design

* More refactoring

* Even more refactoring

* Add a prompt if a VM will loose its project

* Remove json return in projects controller

* Add fa for deletion, remove unnecessary _list file

* Revert schema.rb

* Revert schema.rb

* Fix(Project table): Truncate description

* Fix(Project show): Display VM names, refactor warning

* Chore(Project table): Indent

* Fix(Project show spec): Adapt to changed HTML

* Test(Projects show): Display of VM names + links

* Fix(Projects show): No table

* Fix(Project table): Valid HTML markup for thead

* Chore(Whitespace): Tiny change for diff
  • Loading branch information
bdaase authored and chrisma committed Mar 27, 2019
1 parent 0d77a3d commit 516ba12
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 42 deletions.
7 changes: 6 additions & 1 deletion app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/servers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/projects/_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<%# https://getbootstrap.com/docs/4.0/content/typography/#inline %>
<ul class="list-inline">
<% project.responsible_users.each do |user| %>
<li class="list-inline-item"><%= user.name %></li>
<li class="list-inline-item"><%= link_to user.name, user %></li>
<% end %>
</ul>
</td>
Expand Down
64 changes: 32 additions & 32 deletions app/views/projects/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
<h1><%= I18n.t('project.title') %></h1>

<table class="table">
<tbody>
<tr>
<th><%= Project.human_attribute_name('name') %></th>
<td><%= @project.name %></td>
</tr>
<tr>
<th><%= Project.human_attribute_name('description') %></th>
<td><%= @project.description %></td>
</tr>
<tr>
<th><%= Project.human_attribute_name('responsible_users') %></th>
<td>
<%# https://getbootstrap.com/docs/4.0/content/typography/#inline %>
<ul class="list-inline">
<% @project.responsible_users.each do |user| %>
<li class="list-inline-item"><%= user.name %></li>
<% end %>
</ul>
</td>
</tr>
</tbody>
</table>
<dl>
<dt><strong><%= Project.human_attribute_name('name') %></strong></dt>
<dd><%= @project.name %></dd>
<dt><strong><%= Project.human_attribute_name('description') %></strong></dt>
<dd><%= @project.description %></dd>
<dt><strong>VMs</strong></dt>
<dd>
<% if @project.virtual_machine_configs.each do |config| %>
<%= link_to config.name, controller: "vms", action: "show", id: config.name %>
<% end.empty? %>
---
<% end %>
</dd>
<dt><strong><%= Project.human_attribute_name('responsible_users') %></strong></dt>
<dd>
<%# https://getbootstrap.com/docs/4.0/content/typography/#inline %>
<ul class="list-inline mb-0">
<% @project.responsible_users.each do |user| %>
<li class="list-inline-item"><%= link_to user.name, user %></li>
<% end %>
</ul>
</dd>
</dl>

<div class="float-right">
<%= 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 %>
</div>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# '/app_settings/...'
resources :app_settings, only: %i[update edit]
# '/projects/...'
resources :projects, except: :destroy
resources :projects
# '/servers/...'
resources :servers
# '/hosts/...'
Expand Down
17 changes: 11 additions & 6 deletions spec/views/projects/show.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,25 @@
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

context 'if the current user is not a responsible user for the project' do
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

0 comments on commit 516ba12

Please sign in to comment.