From bb197921e0ef950ecedf9844918adb1a512c3db0 Mon Sep 17 00:00:00 2001 From: Nikita Kholin Date: Sat, 29 Jul 2017 19:45:31 +0300 Subject: [PATCH] Add undo to activity in history --- app/assets/stylesheets/history.sass | 6 ++++++ app/controllers/activities_controller.rb | 9 +++++++++ app/views/dashboard/_history.html.slim | 15 +++++++++------ spec/features/dashboard_spec.rb | 11 +++++++++++ 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/app/assets/stylesheets/history.sass b/app/assets/stylesheets/history.sass index 6dada18..ebbb860 100644 --- a/app/assets/stylesheets/history.sass +++ b/app/assets/stylesheets/history.sass @@ -9,3 +9,9 @@ .no-activities @extend .text-center + +.activity-card + display: flex + +.activity-text + flex: 1 diff --git a/app/controllers/activities_controller.rb b/app/controllers/activities_controller.rb index ca2bd1f..6370d05 100644 --- a/app/controllers/activities_controller.rb +++ b/app/controllers/activities_controller.rb @@ -11,6 +11,15 @@ def create end end + def destroy + activity = Activity.find(params[:id]) + if activity.destroy + redirect_to dashboard_path, notice: "Undone #{activity.name} successfully" + else + redirect_to dashboard_path, alert: activity.errors.full_messages.join(', ') + end + end + private def activity_params diff --git a/app/views/dashboard/_history.html.slim b/app/views/dashboard/_history.html.slim index 1b2e8b7..6d60d42 100644 --- a/app/views/dashboard/_history.html.slim +++ b/app/views/dashboard/_history.html.slim @@ -2,9 +2,12 @@ - if @activities.empty? .no-activities There is nothing done yet. Try sumbitting done activity. - @activities.each do |activity| - .activity.card.card-inverse.card-info - .card-block - .card-text - = "#{activity.user.name} has done " - em = "\"#{activity.name}\" " - = activity_time_tag(activity.created_at) + .activity.card.card-inverse.card-info class="activity_#{activity.id}" + .card-block + .activity-card.card-text + .activity-text + = "#{activity.user.name} has done " + em = "\"#{activity.name}\" " + = activity_time_tag(activity.created_at) + .activity-undo + = button_to 'Undo', activity_path(id: activity.id), method: :delete, class: 'btn btn-warning btn-sm form-inline' diff --git a/spec/features/dashboard_spec.rb b/spec/features/dashboard_spec.rb index 80c92f9..e889743 100644 --- a/spec/features/dashboard_spec.rb +++ b/spec/features/dashboard_spec.rb @@ -3,6 +3,7 @@ let(:user) { room.users.first } let!(:room_activity) { create :room_activity, room: room } let(:room_activity_name) { room_activity.name } + let(:undone_activity) { room.activities.last } background do sign_in user @@ -23,4 +24,14 @@ expect(page).to have_content "#{user.name} has done \"#{room_activity.name}\"" end end + + scenario 'User undoes activity' do + click_on 'History' + + expect { + within ".activity_#{undone_activity.id}" do + click_button 'Undo' + end + }.to change { Activity.count }.by(-1) + end end