diff --git a/app/controllers/locker_applications_controller.rb b/app/controllers/locker_applications_controller.rb index de2f882..d3c42b8 100644 --- a/app/controllers/locker_applications_controller.rb +++ b/app/controllers/locker_applications_controller.rb @@ -19,7 +19,7 @@ def show # GET /locker_applications/new def new - @locker_application = LockerApplication.new(user: current_user) + @locker_application = LockerApplication.new(user: current_user, building_id: current_user.building_id) end # GET /locker_applications/1/edit @@ -43,7 +43,7 @@ def toggle_archived redirect_back(fallback_location: awaiting_assignment_locker_applications_path) end - # rubocop:disable Metrics/AbcSize + # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity # POST /locker_applications or /locker_applications.json def create @locker_application = LockerApplication.new(locker_application_params) @@ -52,10 +52,10 @@ def create @locker_application.preferred_size = 2 if @locker_application.building&.name == 'Lewis Library' - @locker_application.complete = true unless Flipflop.lewis_patrons? + @locker_application.complete = true if current_user.admin? || !Flipflop.lewis_patrons? update_or_create(@locker_application.save, message: 'Locker application was successfully created.', method: :new) end - # rubocop:enable Metrics/AbcSize + # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity # PATCH/PUT /locker_applications/1 or /locker_applications/1.json def update @@ -131,7 +131,9 @@ def update_or_create(valid, message: 'Locker application was successfully update respond_to do |format| if valid if method == :new && Flipflop.lewis_patrons? - format.html { redirect_to edit_locker_application_url(@locker_application) } + format.html do + redirect_to edit_locker_application_url(@locker_application), notice: { message: 'Application successfully created', type: 'success' } + end else format.html { redirect_to @locker_application, notice: { message:, type: 'success' } } end diff --git a/app/views/locker_applications/edit.html.erb b/app/views/locker_applications/edit.html.erb index 39dea4f..db0fa7a 100644 --- a/app/views/locker_applications/edit.html.erb +++ b/app/views/locker_applications/edit.html.erb @@ -1,6 +1,9 @@ <% content_for :title do %> Edit application <% end %> +<% if notice.present? %> + <%= notice['message'] %> +<% end %> <%= render 'form', locker_application: @locker_application %> <%= link_to 'Show', @locker_application %> | diff --git a/app/views/locker_applications/new.html.erb b/app/views/locker_applications/new.html.erb index b212ab9..08ec89e 100644 --- a/app/views/locker_applications/new.html.erb +++ b/app/views/locker_applications/new.html.erb @@ -1,8 +1,8 @@ <% content_for :title do %> Locker application <% end %> -<% if Flipflop.lewis_patrons? %> - <%= render 'building_select_form', locker_application: @locker_application %> -<% else %> +<% if current_user.admin? || !Flipflop.lewis_patrons? %> <%= render 'form', locker_application: @locker_application %> +<% else %> + <%= render 'building_select_form', locker_application: @locker_application %> <% end %> diff --git a/spec/features/locker_application_new_spec.rb b/spec/features/locker_application_new_spec.rb index 04d699f..7c20919 100644 --- a/spec/features/locker_application_new_spec.rb +++ b/spec/features/locker_application_new_spec.rb @@ -162,18 +162,15 @@ it 'can assign the application to an existing user' do visit root_path - select('Firestone Library', from: :locker_application_building_id) - click_button('Next') - new_application = LockerApplication.last - expect(new_application.user).to eq(admin) expect(page).to have_content('Firestone Library Locker Application') expect(page).to have_field('Applicant Netid', with: admin.uid) check('Keyed entry (rather than combination)') fill_in('Applicant Netid', with: user.uid, fill_options: { clear: :backspace }) expect(page).to have_field('Applicant Netid', with: user.uid) click_button('Submit Locker Application') + new_application = LockerApplication.last expect(page).not_to have_content('User must exist') - expect(page).to have_current_path(locker_application_path(new_application)) + expect(page).to have_current_path(edit_locker_application_path(new_application)) expect(new_application.reload.complete).to be true expect(new_application.reload.user).to eq(user) end @@ -182,18 +179,15 @@ context 'with a valid user that does not exist yet' do it 'can create and assign an application to a new user' do visit root_path - select('Firestone Library', from: :locker_application_building_id) - click_button('Next') - new_application = LockerApplication.last - expect(new_application.user).to eq(admin) expect(page).to have_content('Firestone Library Locker Application') expect(page).to have_field('Applicant Netid', with: admin.uid) fill_in('Applicant Netid', with: 'arbitrary netid', fill_options: { clear: :backspace }) fill_in('Additional accessibility needs', with: 'Lower row') expect(page).to have_field('Applicant Netid', with: 'arbitrary netid') click_button('Submit Locker Application') + new_application = LockerApplication.last expect(page).not_to have_content('User must exist') - expect(page).to have_current_path(locker_application_path(new_application)) + expect(page).to have_current_path(edit_locker_application_path(new_application)) end end diff --git a/spec/views/locker_applications/new.html.erb_spec.rb b/spec/views/locker_applications/new.html.erb_spec.rb index bcfdedb..a2bce97 100644 --- a/spec/views/locker_applications/new.html.erb_spec.rb +++ b/spec/views/locker_applications/new.html.erb_spec.rb @@ -40,9 +40,7 @@ it 'renders new locker_application form and allows the user netid to be edited' do render - assert_select 'form[action=?][method=?]', locker_applications_path, 'post' do - assert_select 'input[type=hidden][name=?]', 'locker_application[user_uid]', count: 1 - end + assert_select 'input-text[name=?]', 'locker_application[user_uid]', count: 1 end end end