diff --git a/app/assets/stylesheets/partials/activities/_form.sass b/app/assets/stylesheets/partials/activities/_form.sass index 5c39f7d7..3eed2741 100644 --- a/app/assets/stylesheets/partials/activities/_form.sass +++ b/app/assets/stylesheets/partials/activities/_form.sass @@ -88,3 +88,10 @@ form#new-activity display: none width: 80% padding: 3em 0 + +.delete-activity + margin-top: 4em + header + margin-bottom: 0.5em + button + background-color: $red diff --git a/app/controllers/activities_controller.rb b/app/controllers/activities_controller.rb index 90bd6232..2de96322 100644 --- a/app/controllers/activities_controller.rb +++ b/app/controllers/activities_controller.rb @@ -40,8 +40,12 @@ def update end def destroy - @activity.destroy if @activity - respond_with(@activity, location: activities_path) + if @activity && params[:confirm_delete] && @activity.destroy + redirect_to root_path, notice: I18n.t("destroy_activity.notice") + else + flash[:error] = I18n.t("destroy_activity.error") + render :edit + end end private diff --git a/app/views/activities/_delete_form.html.haml b/app/views/activities/_delete_form.html.haml new file mode 100644 index 00000000..efdb3d4e --- /dev/null +++ b/app/views/activities/_delete_form.html.haml @@ -0,0 +1,8 @@ += form_tag activity_path(@activity), method: :delete do + + %fieldset + %label.inline + = check_box_tag :confirm_delete + = t("delete_activity_form.confirmation", count: @activity.participants.count) + + = button_tag t("delete_activity_form.submit") diff --git a/app/views/activities/edit.html.haml b/app/views/activities/edit.html.haml index 594b9b9d..d099ccc0 100644 --- a/app/views/activities/edit.html.haml +++ b/app/views/activities/edit.html.haml @@ -10,3 +10,12 @@ = link_to t("activities.new.label").downcase, new_activity_path = render "form" + + +%article.delete-activity + %header + %h2= t("activities.danger_zone") + %p.meta + %strong= t("delete_activity_form.title") + + = render "delete_form" diff --git a/config/locales/en.yml b/config/locales/en.yml index cdd012c3..bb77c03e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -97,6 +97,7 @@ en: image_url: invalid: "not valid URL" not_supported_protocol: "protocol not supported ;)" + danger_zone: Danger Zone new_activity: title: Organize an activity @@ -108,6 +109,10 @@ en: notice: Your activity has been updated! error: Your changes could not be saved. Please check your input. + destroy_activity: + notice: Your activity has been deleted! + error: Your activity could not be deleted. Did you check the confirmation? + activity_form: markdown_hint: You can use Markdown here name: @@ -132,6 +137,15 @@ en: label: URL for an image hint: Upload an image to e.g. imgur.com and link it here + delete_activity_form: + title: + Delete activity + confirmation: + one: Really delete this activity (with currently one participant) + other: Really delete this activity (with currently %{count} participants) + submit: + Delete activity + footer_nav: about: label: About diff --git a/spec/controllers/activities_controller_spec.rb b/spec/controllers/activities_controller_spec.rb index 36c602e5..c353da9f 100644 --- a/spec/controllers/activities_controller_spec.rb +++ b/spec/controllers/activities_controller_spec.rb @@ -154,7 +154,7 @@ end describe "#destroy" do - subject { delete :destroy, {id: activity_id} } + subject { delete :destroy, {id: activity_id, confirm_delete: true} } before do sign_in(current_user) @@ -164,7 +164,7 @@ expect(activity).to receive(:destroy).and_return(true) end - it { is_expected.to redirect_to activities_path } + it { is_expected.to redirect_to root_path } end diff --git a/spec/features/activites_spec.rb b/spec/features/activites_spec.rb index 3167b23d..a5934017 100644 --- a/spec/features/activites_spec.rb +++ b/spec/features/activites_spec.rb @@ -52,4 +52,41 @@ expect(page).to have_xpath('//li[@title="Anonymous"]') end end + + context 'deleting an activity' do + it 'deletes activity when checkbox is checked' do + login_as(creator) + visit "/activities/#{activity.id}" + click_link 'Edit' + check 'Really delete this activity (with currently 2 participants)' + click_button 'Delete activity' + + get_redirected_to_homepage + activity_is_deleted + end + + it 'does not delete activity when checkbox is not checked' do + login_as(creator) + visit "/activities/#{activity.id}" + click_link 'Edit' + + click_button 'Delete activity' + + activity_is_not_deleted + end + end + + def activity_is_not_deleted + expect(page).to have_content('Your activity could not be deleted.') + expect(Activity.exists?(activity.id)).to be_truthy + end + + def get_redirected_to_homepage + expect(current_path).to eq(root_path) + end + + def activity_is_deleted + expect(page).to have_content('Your activity has been deleted!') + expect(Activity.exists?(activity.id)).to be_falsy + end end