Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions apps/dashboard/app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,14 @@ def destroy
return
end

if @project.destroy!
redirect_to projects_path, notice: I18n.t('dashboard.jobs_project_deleted')
else
redirect_to projects_path, alert: I18n.t('dashboard.jobs_project_generic_error', error: @project.collect_errors)
# We call deletable? here because it is always false after destroy!
deletable = @project.deletable?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This resolves an error I realized in testing, where as soon as Project#destroy! is called on a deletable project, it turns deletable? == false and then serves the wrong notice. This is fixed by making sure all calls to deletable? happen before the destroy action.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like you need to leave a comment for the same in case we forget this detail.

begin
@project.destroy!
message_key = "dashboard.jobs_project_#{deletable ? 'deleted' : 'removed'}"
redirect_to projects_path, notice: I18n.t(message_key)
rescue StandardError => e
redirect_to projects_path, alert: I18n.t('dashboard.jobs_project_generic_error', error: "#{e.class}: #{e.message}")
end
end

Expand Down
20 changes: 13 additions & 7 deletions apps/dashboard/app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,27 +194,33 @@ def add_to_lookup(operation)
false
end

def remove_from_lookup
def remove_from_lookup!
new_table = Project.lookup_table.except(id)
File.write(Project.lookup_file, new_table.to_yaml)
true
rescue StandardError => e
errors.add(:update, "Cannot update lookup file with error #{e.class}:#{e.message}")
false
end

def editable?
File.writable?(manifest_path)
end

def deletable?
begin
File.stat(configuration_directory).uid == CurrentUser.uid
rescue
false
end
end

def icon_class
# rails will prepopulate the tag with fa- so just the name is needed
icon.sub('fas://', '')
end

def destroy!
remove_from_lookup
FileUtils.remove_dir(configuration_directory, true)
remove_from_lookup!
return unless deletable?

FileUtils.remove_dir(configuration_directory)
end

def configuration_directory
Expand Down
14 changes: 10 additions & 4 deletions apps/dashboard/app/views/projects/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@
</div>
<div class="row">
<div class="col">
<%= button_to 'Delete', project_path(project.id), class: 'btn btn-danger w-100 mb-1', method: 'delete',
form: { class: 'button_to d-flex justify-content-center' },
data: { confirm: I18n.t('dashboard.jobs_project_delete_project_confirmation') }
%>
<% if project.deletable? %>
<%= button_to 'Delete', project_path(project.id), class: 'btn btn-danger w-100 mb-1', method: 'delete',
form: { class: 'button_to d-flex justify-content-center' },
data: { confirm: I18n.t('dashboard.jobs_project_delete_project_confirmation') }
%>
<% else %>
<%= button_to 'Remove', project_path(project.id), class: 'btn btn-warning w-100 mb-1', method: 'delete',
form: { class: 'button_to d-flex justify-content-center' }
%>
<% end %>
</div>
<% if project.editable? %>
<div class="col">
Expand Down
1 change: 1 addition & 0 deletions apps/dashboard/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ en:
jobs_project_imported: Project successfully imported!
jobs_project_delete_project_confirmation: Delete all contents of project directory?
jobs_project_deleted: Project successfully deleted!
jobs_project_removed: Project removed from lookup!
jobs_project_description_placeholder: Project description
jobs_project_directory_help_html: 'Leave empty and a new directory will be created. <br />Default location: %{root_directory}'
jobs_project_directory_error: Project directory path is not set for this workflow
Expand Down