From a1406493ea177ab00be593af78ca2541696ae4fa Mon Sep 17 00:00:00 2001 From: Lisa Taylor Date: Sun, 20 Feb 2022 10:42:32 -0700 Subject: [PATCH] Feat: user story #15 complete --- app/controllers/admin/merchants_controller.rb | 17 +++++++++++++++++ app/views/admin/merchants/edit.html.erb | 5 +++++ app/views/admin/merchants/show.html.erb | 11 ++++++++--- config/routes.rb | 5 +++-- spec/features/admin/merchants/show_spec.rb | 5 +++-- spec/features/admin/merchants/update_spec.rb | 17 +++++++---------- 6 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 app/views/admin/merchants/edit.html.erb diff --git a/app/controllers/admin/merchants_controller.rb b/app/controllers/admin/merchants_controller.rb index 80f17768e8..8a0609b06d 100644 --- a/app/controllers/admin/merchants_controller.rb +++ b/app/controllers/admin/merchants_controller.rb @@ -1,4 +1,5 @@ class Admin::MerchantsController < ApplicationController + #reason to have 2 merchant controllers -- encapsulates methods within the Admin realm in a separate controller def index @merchants = Merchant.all end @@ -6,4 +7,20 @@ def index def show @merchant = Merchant.find(params[:id]) end + + def edit + @merchant = Merchant.find(params[:id]) + end + + def update + merchant = Merchant.find(params[:id]) + merchant.update(merchant_params) + redirect_to "/admin/merchants/#{merchant.id}" + flash[:notice] = "Successfully Updated Merchant Information" + end + + private + def merchant_params + params.require(:merchant).permit(:name, :status) + end end diff --git a/app/views/admin/merchants/edit.html.erb b/app/views/admin/merchants/edit.html.erb new file mode 100644 index 0000000000..3240b6eb1b --- /dev/null +++ b/app/views/admin/merchants/edit.html.erb @@ -0,0 +1,5 @@ +<%= form_with model: [:admin, @merchant], local: true do |form| %> + <%= form.label :name %> + <%= form.text_field :name, required: :true %> + <%= form.submit "Update Merchant" %> +<% end %> diff --git a/app/views/admin/merchants/show.html.erb b/app/views/admin/merchants/show.html.erb index 67592f67d1..70bb337438 100644 --- a/app/views/admin/merchants/show.html.erb +++ b/app/views/admin/merchants/show.html.erb @@ -1,3 +1,8 @@ -
-

<%= @merchant.name %> -

+
+

<%= @merchant.name %>

+

<%= link_to "Update Merchant", "/admin/merchants/#{@merchant.id}/edit" %>

+
+ +<% if flash.any? %> + <%= flash[:notice] %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index cb3e04edad..4ede161c6c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,6 +7,7 @@ get '/merchants/:id/dashboard', to: 'merchants#dashboard' get '/merchants/:id/invoices', to: 'merchant_invoices#index' - get 'admin/merchants', to: 'admin/merchants#index' - get 'admin/merchants/:id', to: 'admin/merchants#show' + namespace :admin do + resources :merchants + end end diff --git a/spec/features/admin/merchants/show_spec.rb b/spec/features/admin/merchants/show_spec.rb index 194a96a3c0..0cc0a92e0b 100644 --- a/spec/features/admin/merchants/show_spec.rb +++ b/spec/features/admin/merchants/show_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe 'Admin Merchants Show' do +RSpec.describe 'Admin Merchants Show Page' do describe 'user story #16' do it "when clicking merchant name I am directed to that merchants show page" do merchant_1 = Merchant.create!(name: "LT's Tee Shirts LLC") @@ -40,7 +40,8 @@ it "updates are submitted - redirected back to admin/merchant show page w/ updated info and flash message" do merchant_1 = Merchant.create!(name: "LT's Tee Shirts LLC") visit "/admin/merchants/#{merchant_1.id}/edit" - fill_in 'Name', with: "LT's T-shirts and Hoodies LLC" + + fill_in "Name", with: "LT's T-shirts and Hoodies LLC" click_button("Update Merchant") expect(current_path).to eq("/admin/merchants/#{merchant_1.id}") diff --git a/spec/features/admin/merchants/update_spec.rb b/spec/features/admin/merchants/update_spec.rb index 091838968b..a195ebe72b 100644 --- a/spec/features/admin/merchants/update_spec.rb +++ b/spec/features/admin/merchants/update_spec.rb @@ -2,27 +2,24 @@ RSpec.describe 'Admin Merchants Edit' do describe 'user story #15 - continued' do + before :each do + @merchant_1 = Merchant.create!(name: "LT's Tee Shirts LLC") + visit "/admin/merchants/#{@merchant_1.id}/edit" + end it "submits the edit form and udpates merchant" do - merchant_1 = Merchant.create!(name: "LT's Tee Shirts LLC") - - visit "/admin/merchants/#{merchant_1.id}" - fill_in 'Name', with: "LT's T-shirts and Hoodies LLC" click_button 'Update Merchant' - expect(current_path).to eq("/admin/merchants/#{merchant_1.id}") + expect(current_path).to eq("/admin/merchants/#{@merchant_1.id}") expect(page).to have_content("LT's T-shirts and Hoodies LLC") expect(page).to have_content("Successfully Updated Merchant Information") end it 'all info not updated - yeilds an error' do - merchant_1 = Merchant.create!(name: "LT's Tee Shirts LLC") - visit edit_admin_merchant_path(merchant_1.id) click_button 'Update Merchant' - expect(current_path).to eq("/admin/merchants/#{merchant_1.id}") - expect(page).to have_content("Error:") - expect(page).to have_content("LT's T-shirts and Hoodies LLC") + expect(current_path).to eq("/admin/merchants/#{@merchant_1.id}") + expect(page).to have_content("LT's Tee Shirts LLC") end end end