Skip to content

Commit

Permalink
Merge pull request #161 from til/delete-activity
Browse files Browse the repository at this point in the history
Add form to delete an activity
  • Loading branch information
moonglum committed Aug 10, 2015
2 parents b0b623f + f48c043 commit c6482e7
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 4 deletions.
7 changes: 7 additions & 0 deletions app/assets/stylesheets/partials/activities/_form.sass
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 6 additions & 2 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions app/views/activities/_delete_form.html.haml
Original file line number Diff line number Diff line change
@@ -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")
9 changes: 9 additions & 0 deletions app/views/activities/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
14 changes: 14 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/activities_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
37 changes: 37 additions & 0 deletions spec/features/activites_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c6482e7

Please sign in to comment.