From 3fe38f7df13f52bd40f37e73db656034828c98da Mon Sep 17 00:00:00 2001 From: Nikita Kholin Date: Sun, 23 Jul 2017 17:55:07 +0300 Subject: [PATCH] Make dashboard work again --- app/controllers/activities_controller.rb | 21 +++++++++++++++++ app/models/activity.rb | 2 ++ .../_submit_done_action_form.html.slim | 13 +++++------ app/views/dashboard/index.html.slim | 6 ++--- config/routes.rb | 2 +- spec/features/dashboard_spec.rb | 23 +++++++++---------- 6 files changed, 43 insertions(+), 24 deletions(-) create mode 100644 app/controllers/activities_controller.rb diff --git a/app/controllers/activities_controller.rb b/app/controllers/activities_controller.rb new file mode 100644 index 0000000..ca2bd1f --- /dev/null +++ b/app/controllers/activities_controller.rb @@ -0,0 +1,21 @@ +class ActivitiesController < ApplicationController + before_action :authenticate_user! + before_action :require_room_presence + + def create + activity = Activity.new(activity_params) + if activity.save + redirect_to dashboard_path, notice: "Good job, #{current_user.name}, for \"#{activity.name}\"!" + else + redirect_to dashboard_path, alert: activity.errors.full_messages.join(', ') + end + end + + private + + def activity_params + params.require(:activity) + .permit(:room_activity_id) + .merge(user_id: current_user.id) + end +end diff --git a/app/models/activity.rb b/app/models/activity.rb index 6fc739d..fa1162a 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -2,4 +2,6 @@ class Activity < ApplicationRecord belongs_to :user belongs_to :room_activity + + delegate :name, to: :room_activity end diff --git a/app/views/dashboard/_submit_done_action_form.html.slim b/app/views/dashboard/_submit_done_action_form.html.slim index 135bbf1..603f44d 100644 --- a/app/views/dashboard/_submit_done_action_form.html.slim +++ b/app/views/dashboard/_submit_done_action_form.html.slim @@ -1,7 +1,6 @@ -= form_tag(submit_done_action_path, method: :post, class: 'text-center') do - .form-group - = collection_select :user_action, :id, - current_user.user_actions, :id, :name, - { prompt: 'What have you done today?', disabled: false }, - { class: 'custom-select', required: true } - = submit_tag 'Submit', class: 'btn btn-primary' += simple_form_for Activity.new do |f| + .card.text-center + .card-block + h5.text-center What have you done today? + = f.input :room_activity_id, collection: @room.room_activities, prompt: '-', label: false + = f.submit 'Submit', class: 'btn btn-primary' diff --git a/app/views/dashboard/index.html.slim b/app/views/dashboard/index.html.slim index 770c902..82583ab 100644 --- a/app/views/dashboard/index.html.slim +++ b/app/views/dashboard/index.html.slim @@ -11,10 +11,8 @@ ul.nav.nav-tabs.justify-content-center.m-3 role="tablist" .tab-content #status.tab-pane.active role="tabpanel" = render 'status' - - .card.col-sm-4.offset-sm-4.text-center - .card-block - = render 'submit_done_action_form' + + = render 'submit_done_action_form' #progress.tab-pane role="tabpanel" = render 'progress' diff --git a/config/routes.rb b/config/routes.rb index 658f43e..fa35be5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,7 +13,6 @@ scope 'dashboard', controller: :dashboard do get '/', action: :index, as: 'dashboard' - post 'submit-done-action' post 'punish', as: 'punish' # TODO: switch to punishments#create end @@ -22,6 +21,7 @@ end resources :room_activities, only: [:new, :update, :create, :destroy] + resources :activities, only: [:create, :destroy] resources :punishments, only: [:index] end diff --git a/spec/features/dashboard_spec.rb b/spec/features/dashboard_spec.rb index 3b6246a..0199ac1 100644 --- a/spec/features/dashboard_spec.rb +++ b/spec/features/dashboard_spec.rb @@ -1,26 +1,25 @@ RSpec.feature 'Dashboard' do - let(:user) { create(:user) } let(:room) { create(:room, :with_activities) } + let(:user) { room.users.first } + let!(:room_activity) { create :room_activity, room: room } + let(:room_activity_name) { room_activity.name } background do - user.join_room(room) sign_in user visit dashboard_path end - scenario 'User submits done action' do - # room_action = room.room_actions.first + scenario 'User submits done activity' do + expect { + select room_activity_name, from: 'activity_room_activity_id' + click_button 'Submit' + }.to change { Activity.count }.by(1) - # expect { - # select room_action.name, from: 'user_action_id' - # click_button 'Submit' - # }.to change { user.user_actions.find_by(room_action_id: room_action.id).value }.by(1) + expect(page).to have_content 'Good job' + expect(user.next_on_room_activities.pluck(:name)).not_to include(room_activity_name) - # expect(page).to have_content 'Good job' - # expect(room_action.next_on_user).not_to eq user - - # click_on 'History' # TODO: turn on this test, it is temporary disabled + # click_on 'History' # within '#history' do # expect(page).to have_content "#{user.name} has done \"#{room_action.name}\"" # end