From 98d77b74b5c88c9e7075da823aa5a8ee592d0d9c Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Mon, 23 Oct 2017 21:05:22 +0800 Subject: [PATCH 01/20] create migration for adding slug column in wishlists table --- db/migrate/20171023124712_add_column_slug_to_wishlists.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 db/migrate/20171023124712_add_column_slug_to_wishlists.rb diff --git a/db/migrate/20171023124712_add_column_slug_to_wishlists.rb b/db/migrate/20171023124712_add_column_slug_to_wishlists.rb new file mode 100644 index 0000000..52f1add --- /dev/null +++ b/db/migrate/20171023124712_add_column_slug_to_wishlists.rb @@ -0,0 +1,5 @@ +class AddColumnSlugToWishlists < ActiveRecord::Migration[5.1] + def change + add_column :wishlists, :slug, :string + end +end From 9178c164dad943f87e0fee1428549293af659789 Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Mon, 23 Oct 2017 21:05:49 +0800 Subject: [PATCH 02/20] change parameter to slug --- spec/routing/wishlists_routing_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/routing/wishlists_routing_spec.rb b/spec/routing/wishlists_routing_spec.rb index aeb6aa4..92a06c4 100644 --- a/spec/routing/wishlists_routing_spec.rb +++ b/spec/routing/wishlists_routing_spec.rb @@ -8,7 +8,7 @@ end it "routes to #show" do - expect(:get => "/wishlists/1").to route_to("wishlists#show", :id => "1") + expect(:get => "/wishlists/dc-general").to route_to("wishlists#show", :slug => "dc-general") end it "routes to #edit" do @@ -30,6 +30,5 @@ it "routes to #destroy" do expect(:delete => "/wishlists/1").to route_to("wishlists#destroy", :id => "1") end - end end From 64ea806b9ab5c04140e71d7c3b0927c81f1f9c57 Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Mon, 23 Oct 2017 21:06:34 +0800 Subject: [PATCH 03/20] excepty show action in wishlists and create get route to wishlist passing parameter slug --- config/routes.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 516cace..3c2f57e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,12 +5,14 @@ end # Wishlists & Items - resources :wishlists, except: [:index] do + resources :wishlists, except: [:index, :show] do resources :wishlist_items, shallow: true, only: [:create, :edit, :update, :destroy] resource :amazon_search, controller: :amazon_search, only: [:new, :show] end + + get 'wishlists/:slug', to: 'wishlists#show', as: :wishlist_by_slug # Users resources :users From 5280fb061329028dd00155b646d2e6e3b3a748c1 Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Mon, 23 Oct 2017 21:07:15 +0800 Subject: [PATCH 04/20] change route to wishlist_by_slug_path passing wishlist slug as parameter --- app/views/partials/_primary_nav.html.erb | 2 +- app/views/wishlists/edit.html.erb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/partials/_primary_nav.html.erb b/app/views/partials/_primary_nav.html.erb index c56ff39..a96fabe 100644 --- a/app/views/partials/_primary_nav.html.erb +++ b/app/views/partials/_primary_nav.html.erb @@ -24,7 +24,7 @@ diff --git a/app/views/wishlists/edit.html.erb b/app/views/wishlists/edit.html.erb index 400c61d..2e8a3d9 100644 --- a/app/views/wishlists/edit.html.erb +++ b/app/views/wishlists/edit.html.erb @@ -3,5 +3,5 @@ <%= render 'form', wishlist: @wishlist %> - <%= link_to 'Back to Wishlist', @wishlist %> + <%= link_to 'Back to Wishlist', wishlist_by_slug_path(@wishlist.slug) %> From 87412ccc9206b68a4c33edbab89d77c508a8918f Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Mon, 23 Oct 2017 21:08:02 +0800 Subject: [PATCH 05/20] in show action find wishlist by slug and after successfully create redirect to wishlist_by_slug_path --- app/controllers/wishlists_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/wishlists_controller.rb b/app/controllers/wishlists_controller.rb index 57b95a1..99937bb 100644 --- a/app/controllers/wishlists_controller.rb +++ b/app/controllers/wishlists_controller.rb @@ -3,7 +3,7 @@ class WishlistsController < ApplicationController def show skip_authorization - @wishlist = Wishlist.includes(wishlist_items: :item).find(params[:id]) + @wishlist = Wishlist.includes(wishlist_items: :item).find_by_slug(params[:slug]) @site_managers = @wishlist.users @wishlist_items = @wishlist.wishlist_items.priority_order end @@ -25,7 +25,7 @@ def create attach_site_managers if @wishlist.save - redirect_to @wishlist, notice: 'Wishlist was successfully created.' + redirect_to wishlist_by_slug_path(@wishlist.slug), notice: 'Wishlist was successfully created.' else render :new end From 0e498726f165b0e566b39680bcc378bc0c33ca8c Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Mon, 23 Oct 2017 21:10:40 +0800 Subject: [PATCH 06/20] create slug before save and update --- app/models/wishlist.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/models/wishlist.rb b/app/models/wishlist.rb index 98f45b1..0816475 100644 --- a/app/models/wishlist.rb +++ b/app/models/wishlist.rb @@ -17,4 +17,13 @@ class Wishlist < ApplicationRecord validates :name, presence: true, uniqueness: true + + before_save :create_slug + before_update :create_slug + + private + + def create_slug + self.slug = name.parameterize + end end From e45e1fe2e526190b7d0dc5539af0e17542361a74 Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Mon, 23 Oct 2017 21:11:04 +0800 Subject: [PATCH 07/20] after successfully updated redirect to wishlist_by_slug_path --- app/controllers/wishlists_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/wishlists_controller.rb b/app/controllers/wishlists_controller.rb index 99937bb..0e57145 100644 --- a/app/controllers/wishlists_controller.rb +++ b/app/controllers/wishlists_controller.rb @@ -35,7 +35,7 @@ def update authorize @wishlist attach_site_managers if @wishlist.update(wishlist_params) - redirect_to @wishlist, notice: 'Wishlist was successfully updated.' + redirect_to wishlist_by_slug_path(@wishlist.slug), notice: 'Wishlist was successfully updated.' else render :edit end From 807f0569d8461b1d706f77904648a3b955430e08 Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Mon, 23 Oct 2017 21:11:20 +0800 Subject: [PATCH 08/20] add column slug --- db/schema.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/db/schema.rb b/db/schema.rb index d6a3f92..59e4b3d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170908012526) do +ActiveRecord::Schema.define(version: 20171023124712) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -76,6 +76,7 @@ t.text "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "slug" end add_foreign_key "pledges", "users" From ad7db1452cea853c2b0879af9f910a977f6361fd Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Mon, 23 Oct 2017 21:39:20 +0800 Subject: [PATCH 09/20] change route path --- app/controllers/wishlist_items_controller.rb | 6 +++--- app/views/pledges/_form.html.erb | 2 +- app/views/pledges/_pledge.html.erb | 2 +- app/views/pledges/show.html.erb | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/wishlist_items_controller.rb b/app/controllers/wishlist_items_controller.rb index e88b1aa..8db40d8 100644 --- a/app/controllers/wishlist_items_controller.rb +++ b/app/controllers/wishlist_items_controller.rb @@ -11,7 +11,7 @@ def create authorize wishlist.wishlist_items.build @wishlist_item = wishlist.wishlist_items.create!(wishlist_item_create_params) - redirect_to wishlist_path(@wishlist_item.wishlist), notice: "Added #{@wishlist_item.name}." + redirect_to wishlist_by_slug_path(@wishlist_item.wishlist.slug), notice: "Added #{@wishlist_item.name}." end def edit @@ -22,7 +22,7 @@ def update authorize @wishlist_item if @wishlist_item.update(wishlist_item_params) - redirect_to @wishlist_item.wishlist, notice: 'Wishlist item was successfully updated.' + redirect_to wishlist_by_slug_path(@wishlist_item.wishlist.slug), notice: 'Wishlist item was successfully updated.' else render :edit end @@ -34,7 +34,7 @@ def destroy wishlist = @wishlist_item.wishlist @wishlist_item.destroy - redirect_to wishlist, notice: 'Item was successfully removed from wishlist.' + redirect_to wishlist_by_slug_path(wishlist.slug), notice: 'Item was successfully removed from wishlist.' end private diff --git a/app/views/pledges/_form.html.erb b/app/views/pledges/_form.html.erb index 7110305..b259ff0 100644 --- a/app/views/pledges/_form.html.erb +++ b/app/views/pledges/_form.html.erb @@ -16,7 +16,7 @@
<%= pledge.wishlist_item.name %>
Wishlist
-
<%= link_to pledge.wishlist.name, pledge.wishlist %>
+
<%= link_to pledge.wishlist.name, wishlist_by_slug_path(pledge.wishlist.slug) %>
Pledging user
diff --git a/app/views/pledges/_pledge.html.erb b/app/views/pledges/_pledge.html.erb index 3c9faff..4158861 100644 --- a/app/views/pledges/_pledge.html.erb +++ b/app/views/pledges/_pledge.html.erb @@ -25,7 +25,7 @@
Wishlist
- <%= link_to pledge.wishlist_name, pledge.wishlist %> + <%= link_to pledge.wishlist_name, wishlist_by_slug_path(pledge.wishlist.slug) %>
Number pledged
diff --git a/app/views/pledges/show.html.erb b/app/views/pledges/show.html.erb index fb0f3b9..a78c778 100644 --- a/app/views/pledges/show.html.erb +++ b/app/views/pledges/show.html.erb @@ -49,6 +49,6 @@

<% end %> - <%= link_to "Back to #{@pledge.wishlist.name}", @pledge.wishlist %> | + <%= link_to "Back to #{@pledge.wishlist.name}", wishlist_by_slug_path(@pledge.wishlist.slug) %> | <%= link_to "Back to user page", @pledge.user %> From a5f47cc5126b374a157ec50e86d26fe639a9487d Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Mon, 23 Oct 2017 21:39:54 +0800 Subject: [PATCH 10/20] change expected path --- spec/features/browsing_spec.rb | 2 +- spec/features/managing_items_and_wishlists_spec.rb | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/spec/features/browsing_spec.rb b/spec/features/browsing_spec.rb index 24cd9a5..0285782 100644 --- a/spec/features/browsing_spec.rb +++ b/spec/features/browsing_spec.rb @@ -19,7 +19,7 @@ visit "/" click_link "St. Joseph's" - expect(current_path).to eq wishlist_path(st_josephs) + expect(current_path).to eq wishlist_by_slug_path(st_josephs.slug) end context "when there are wishlist items" do diff --git a/spec/features/managing_items_and_wishlists_spec.rb b/spec/features/managing_items_and_wishlists_spec.rb index df3a7e8..6a5afb4 100644 --- a/spec/features/managing_items_and_wishlists_spec.rb +++ b/spec/features/managing_items_and_wishlists_spec.rb @@ -104,12 +104,11 @@ scenario "I can update an existing wishlist" do click_link "DC General" - dc_general_path = current_path click_link "Edit Wishlist" fill_in("wishlist_name", with: "VA General") click_button "Update Wishlist" - expect(current_path).to eq dc_general_path + expect(current_path).to eq wishlist_by_slug_path('va-general') expect(page).to have_text "Wishlist was successfully updated." expect(page).to have_text "VA General" end From f9be02ee70f24219c6e9114f19b3698f6ae95a59 Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Mon, 23 Oct 2017 21:46:56 +0800 Subject: [PATCH 11/20] change wishlist path name and parameter to be pass --- spec/features/managing_items_and_wishlists_spec.rb | 14 +++++++------- spec/features/pledging_an_item_spec.rb | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/spec/features/managing_items_and_wishlists_spec.rb b/spec/features/managing_items_and_wishlists_spec.rb index 6a5afb4..8a93d62 100644 --- a/spec/features/managing_items_and_wishlists_spec.rb +++ b/spec/features/managing_items_and_wishlists_spec.rb @@ -15,7 +15,7 @@ let(:wishlist) { site_manager.wishlists.first } scenario "I can add a new item to my wishlist", :external do - visit wishlist_path(wishlist) + visit wishlist_by_slug_path(wishlist.slug) click_link "Add to Wishlist" fill_in "search_field", with: "corgi" click_button "Search Amazon" @@ -34,7 +34,7 @@ end scenario "My search can't be blank", :external do - visit wishlist_path(wishlist) + visit wishlist_by_slug_path(wishlist.slug) click_link "Add to Wishlist" click_button "Search Amazon" @@ -44,7 +44,7 @@ scenario "I can edit a wishlist item on my wishlist" do create(:wishlist_item, wishlist: wishlist) - visit wishlist_path(wishlist) + visit wishlist_by_slug_path(wishlist.slug) within ".wishlist-item-tools" do click_link "Edit" end @@ -58,7 +58,7 @@ scenario "I can remove an item from my wishlist" do create(:wishlist_item, wishlist: wishlist, staff_message: "Another item.") - visit wishlist_path(wishlist) + visit wishlist_by_slug_path(wishlist.slug) within ".wishlist-item-tools" do click_link "Remove" @@ -69,16 +69,16 @@ end scenario "I cannot delete my wishlist" do - visit wishlist_path(wishlist) + visit wishlist_by_slug_path(wishlist.slug) - visit wishlist_path(wishlist) + visit wishlist_by_slug_path(wishlist.slug) within "#wishlist-actions" do expect(page).not_to have_link "Destroy" end end scenario "I cannot edit my wishlist" do - visit wishlist_path(wishlist) + visit wishlist_by_slug_path(wishlist.slug) expect(page).not_to have_link "Edit Wishlist" visit edit_wishlist_path(wishlist) diff --git a/spec/features/pledging_an_item_spec.rb b/spec/features/pledging_an_item_spec.rb index 49a1b01..8cfdeeb 100644 --- a/spec/features/pledging_an_item_spec.rb +++ b/spec/features/pledging_an_item_spec.rb @@ -94,7 +94,7 @@ end scenario "I can re-pledge an item to increment its quantity" do - visit wishlist_path(pledge.wishlist) + visit wishlist_by_slug_path(pledge.wishlist.slug) click_button "Pledge to Donate" expect(page).to have_text "Number pledged 2" end From f8eebdaad1aeb033cd587961e4d6ed953d4397b6 Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Mon, 23 Oct 2017 21:54:37 +0800 Subject: [PATCH 12/20] change expected response url --- spec/controllers/wishlists_controller_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/controllers/wishlists_controller_spec.rb b/spec/controllers/wishlists_controller_spec.rb index 273fe29..f7dfcdd 100644 --- a/spec/controllers/wishlists_controller_spec.rb +++ b/spec/controllers/wishlists_controller_spec.rb @@ -26,7 +26,7 @@ describe "GET #show" do it "returns a success response" do wishlist = create(:wishlist, valid_attributes) - get :show, params: {id: wishlist.to_param}, session: {} + get :show, params: {slug: wishlist.slug}, session: {} expect(response).to be_success end end @@ -182,7 +182,7 @@ it "redirects to the created wishlist" do post :create, params: {wishlist: valid_attributes}, session: admin_session - expect(response).to redirect_to(Wishlist.last) + expect(response).to redirect_to wishlist_by_slug_path(Wishlist.last.slug) end end @@ -284,7 +284,7 @@ put :update, params: {id: wishlist.to_param, wishlist: valid_attributes}, session: admin_session - expect(response).to redirect_to(wishlist) + expect(response).to redirect_to wishlist_by_slug_path(wishlist.slug) end end From 0fe1182d67d9b8decb676dcd37d38866de3d7eed Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Wed, 25 Oct 2017 07:46:55 +0800 Subject: [PATCH 13/20] change to wishlist_path --- app/controllers/wishlist_items_controller.rb | 6 +++--- app/controllers/wishlists_controller.rb | 6 +++--- app/views/partials/_primary_nav.html.erb | 2 +- app/views/pledges/_form.html.erb | 2 +- app/views/pledges/_pledge.html.erb | 2 +- app/views/pledges/show.html.erb | 2 +- app/views/wishlists/edit.html.erb | 2 +- spec/controllers/wishlists_controller_spec.rb | 6 +++--- spec/features/browsing_spec.rb | 2 +- .../managing_items_and_wishlists_spec.rb | 17 +++++++++-------- spec/features/pledging_an_item_spec.rb | 2 +- spec/routing/wishlists_routing_spec.rb | 2 +- 12 files changed, 26 insertions(+), 25 deletions(-) diff --git a/app/controllers/wishlist_items_controller.rb b/app/controllers/wishlist_items_controller.rb index 8db40d8..e88b1aa 100644 --- a/app/controllers/wishlist_items_controller.rb +++ b/app/controllers/wishlist_items_controller.rb @@ -11,7 +11,7 @@ def create authorize wishlist.wishlist_items.build @wishlist_item = wishlist.wishlist_items.create!(wishlist_item_create_params) - redirect_to wishlist_by_slug_path(@wishlist_item.wishlist.slug), notice: "Added #{@wishlist_item.name}." + redirect_to wishlist_path(@wishlist_item.wishlist), notice: "Added #{@wishlist_item.name}." end def edit @@ -22,7 +22,7 @@ def update authorize @wishlist_item if @wishlist_item.update(wishlist_item_params) - redirect_to wishlist_by_slug_path(@wishlist_item.wishlist.slug), notice: 'Wishlist item was successfully updated.' + redirect_to @wishlist_item.wishlist, notice: 'Wishlist item was successfully updated.' else render :edit end @@ -34,7 +34,7 @@ def destroy wishlist = @wishlist_item.wishlist @wishlist_item.destroy - redirect_to wishlist_by_slug_path(wishlist.slug), notice: 'Item was successfully removed from wishlist.' + redirect_to wishlist, notice: 'Item was successfully removed from wishlist.' end private diff --git a/app/controllers/wishlists_controller.rb b/app/controllers/wishlists_controller.rb index 0e57145..57b95a1 100644 --- a/app/controllers/wishlists_controller.rb +++ b/app/controllers/wishlists_controller.rb @@ -3,7 +3,7 @@ class WishlistsController < ApplicationController def show skip_authorization - @wishlist = Wishlist.includes(wishlist_items: :item).find_by_slug(params[:slug]) + @wishlist = Wishlist.includes(wishlist_items: :item).find(params[:id]) @site_managers = @wishlist.users @wishlist_items = @wishlist.wishlist_items.priority_order end @@ -25,7 +25,7 @@ def create attach_site_managers if @wishlist.save - redirect_to wishlist_by_slug_path(@wishlist.slug), notice: 'Wishlist was successfully created.' + redirect_to @wishlist, notice: 'Wishlist was successfully created.' else render :new end @@ -35,7 +35,7 @@ def update authorize @wishlist attach_site_managers if @wishlist.update(wishlist_params) - redirect_to wishlist_by_slug_path(@wishlist.slug), notice: 'Wishlist was successfully updated.' + redirect_to @wishlist, notice: 'Wishlist was successfully updated.' else render :edit end diff --git a/app/views/partials/_primary_nav.html.erb b/app/views/partials/_primary_nav.html.erb index a96fabe..c56ff39 100644 --- a/app/views/partials/_primary_nav.html.erb +++ b/app/views/partials/_primary_nav.html.erb @@ -24,7 +24,7 @@ diff --git a/app/views/pledges/_form.html.erb b/app/views/pledges/_form.html.erb index b259ff0..7110305 100644 --- a/app/views/pledges/_form.html.erb +++ b/app/views/pledges/_form.html.erb @@ -16,7 +16,7 @@
<%= pledge.wishlist_item.name %>
Wishlist
-
<%= link_to pledge.wishlist.name, wishlist_by_slug_path(pledge.wishlist.slug) %>
+
<%= link_to pledge.wishlist.name, pledge.wishlist %>
Pledging user
diff --git a/app/views/pledges/_pledge.html.erb b/app/views/pledges/_pledge.html.erb index 4158861..3c9faff 100644 --- a/app/views/pledges/_pledge.html.erb +++ b/app/views/pledges/_pledge.html.erb @@ -25,7 +25,7 @@
Wishlist
- <%= link_to pledge.wishlist_name, wishlist_by_slug_path(pledge.wishlist.slug) %> + <%= link_to pledge.wishlist_name, pledge.wishlist %>
Number pledged
diff --git a/app/views/pledges/show.html.erb b/app/views/pledges/show.html.erb index a78c778..fb0f3b9 100644 --- a/app/views/pledges/show.html.erb +++ b/app/views/pledges/show.html.erb @@ -49,6 +49,6 @@

<% end %> - <%= link_to "Back to #{@pledge.wishlist.name}", wishlist_by_slug_path(@pledge.wishlist.slug) %> | + <%= link_to "Back to #{@pledge.wishlist.name}", @pledge.wishlist %> | <%= link_to "Back to user page", @pledge.user %> diff --git a/app/views/wishlists/edit.html.erb b/app/views/wishlists/edit.html.erb index 2e8a3d9..400c61d 100644 --- a/app/views/wishlists/edit.html.erb +++ b/app/views/wishlists/edit.html.erb @@ -3,5 +3,5 @@ <%= render 'form', wishlist: @wishlist %> - <%= link_to 'Back to Wishlist', wishlist_by_slug_path(@wishlist.slug) %> + <%= link_to 'Back to Wishlist', @wishlist %> diff --git a/spec/controllers/wishlists_controller_spec.rb b/spec/controllers/wishlists_controller_spec.rb index f7dfcdd..273fe29 100644 --- a/spec/controllers/wishlists_controller_spec.rb +++ b/spec/controllers/wishlists_controller_spec.rb @@ -26,7 +26,7 @@ describe "GET #show" do it "returns a success response" do wishlist = create(:wishlist, valid_attributes) - get :show, params: {slug: wishlist.slug}, session: {} + get :show, params: {id: wishlist.to_param}, session: {} expect(response).to be_success end end @@ -182,7 +182,7 @@ it "redirects to the created wishlist" do post :create, params: {wishlist: valid_attributes}, session: admin_session - expect(response).to redirect_to wishlist_by_slug_path(Wishlist.last.slug) + expect(response).to redirect_to(Wishlist.last) end end @@ -284,7 +284,7 @@ put :update, params: {id: wishlist.to_param, wishlist: valid_attributes}, session: admin_session - expect(response).to redirect_to wishlist_by_slug_path(wishlist.slug) + expect(response).to redirect_to(wishlist) end end diff --git a/spec/features/browsing_spec.rb b/spec/features/browsing_spec.rb index 0285782..24cd9a5 100644 --- a/spec/features/browsing_spec.rb +++ b/spec/features/browsing_spec.rb @@ -19,7 +19,7 @@ visit "/" click_link "St. Joseph's" - expect(current_path).to eq wishlist_by_slug_path(st_josephs.slug) + expect(current_path).to eq wishlist_path(st_josephs) end context "when there are wishlist items" do diff --git a/spec/features/managing_items_and_wishlists_spec.rb b/spec/features/managing_items_and_wishlists_spec.rb index 8a93d62..df3a7e8 100644 --- a/spec/features/managing_items_and_wishlists_spec.rb +++ b/spec/features/managing_items_and_wishlists_spec.rb @@ -15,7 +15,7 @@ let(:wishlist) { site_manager.wishlists.first } scenario "I can add a new item to my wishlist", :external do - visit wishlist_by_slug_path(wishlist.slug) + visit wishlist_path(wishlist) click_link "Add to Wishlist" fill_in "search_field", with: "corgi" click_button "Search Amazon" @@ -34,7 +34,7 @@ end scenario "My search can't be blank", :external do - visit wishlist_by_slug_path(wishlist.slug) + visit wishlist_path(wishlist) click_link "Add to Wishlist" click_button "Search Amazon" @@ -44,7 +44,7 @@ scenario "I can edit a wishlist item on my wishlist" do create(:wishlist_item, wishlist: wishlist) - visit wishlist_by_slug_path(wishlist.slug) + visit wishlist_path(wishlist) within ".wishlist-item-tools" do click_link "Edit" end @@ -58,7 +58,7 @@ scenario "I can remove an item from my wishlist" do create(:wishlist_item, wishlist: wishlist, staff_message: "Another item.") - visit wishlist_by_slug_path(wishlist.slug) + visit wishlist_path(wishlist) within ".wishlist-item-tools" do click_link "Remove" @@ -69,16 +69,16 @@ end scenario "I cannot delete my wishlist" do - visit wishlist_by_slug_path(wishlist.slug) + visit wishlist_path(wishlist) - visit wishlist_by_slug_path(wishlist.slug) + visit wishlist_path(wishlist) within "#wishlist-actions" do expect(page).not_to have_link "Destroy" end end scenario "I cannot edit my wishlist" do - visit wishlist_by_slug_path(wishlist.slug) + visit wishlist_path(wishlist) expect(page).not_to have_link "Edit Wishlist" visit edit_wishlist_path(wishlist) @@ -104,11 +104,12 @@ scenario "I can update an existing wishlist" do click_link "DC General" + dc_general_path = current_path click_link "Edit Wishlist" fill_in("wishlist_name", with: "VA General") click_button "Update Wishlist" - expect(current_path).to eq wishlist_by_slug_path('va-general') + expect(current_path).to eq dc_general_path expect(page).to have_text "Wishlist was successfully updated." expect(page).to have_text "VA General" end diff --git a/spec/features/pledging_an_item_spec.rb b/spec/features/pledging_an_item_spec.rb index 8cfdeeb..49a1b01 100644 --- a/spec/features/pledging_an_item_spec.rb +++ b/spec/features/pledging_an_item_spec.rb @@ -94,7 +94,7 @@ end scenario "I can re-pledge an item to increment its quantity" do - visit wishlist_by_slug_path(pledge.wishlist.slug) + visit wishlist_path(pledge.wishlist) click_button "Pledge to Donate" expect(page).to have_text "Number pledged 2" end diff --git a/spec/routing/wishlists_routing_spec.rb b/spec/routing/wishlists_routing_spec.rb index 92a06c4..f7191df 100644 --- a/spec/routing/wishlists_routing_spec.rb +++ b/spec/routing/wishlists_routing_spec.rb @@ -8,7 +8,7 @@ end it "routes to #show" do - expect(:get => "/wishlists/dc-general").to route_to("wishlists#show", :slug => "dc-general") + expect(:get => "/wishlists/1").to route_to("wishlists#show", :id => "1") end it "routes to #edit" do From b2d8dec163109d9c7ae0a3f53a869cde03b08b55 Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Wed, 25 Oct 2017 07:47:22 +0800 Subject: [PATCH 14/20] remove route wishlist_by_slug --- config/routes.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 3c2f57e..01d3584 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,15 +5,13 @@ end # Wishlists & Items - resources :wishlists, except: [:index, :show] do + resources :wishlists, except: [:index] do resources :wishlist_items, shallow: true, only: [:create, :edit, :update, :destroy] resource :amazon_search, controller: :amazon_search, only: [:new, :show] end - get 'wishlists/:slug', to: 'wishlists#show', as: :wishlist_by_slug - # Users resources :users From 2fa27d28ecc86bc8c6f8cb6a8bd02156c2ecb09f Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Wed, 25 Oct 2017 07:58:37 +0800 Subject: [PATCH 15/20] use find_by_slug --- app/controllers/amazon_search_controller.rb | 2 +- app/controllers/wishlist_items_controller.rb | 2 +- app/controllers/wishlists_controller.rb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/amazon_search_controller.rb b/app/controllers/amazon_search_controller.rb index 0c09fc6..5e9b2e0 100644 --- a/app/controllers/amazon_search_controller.rb +++ b/app/controllers/amazon_search_controller.rb @@ -21,7 +21,7 @@ def amazon_client end def set_wishlist - @wishlist = Wishlist.find(params[:wishlist_id]) + @wishlist = Wishlist.find_by_slug(params[:wishlist_id]) end def pundit_user diff --git a/app/controllers/wishlist_items_controller.rb b/app/controllers/wishlist_items_controller.rb index e88b1aa..30e3007 100644 --- a/app/controllers/wishlist_items_controller.rb +++ b/app/controllers/wishlist_items_controller.rb @@ -7,7 +7,7 @@ def index end def create - wishlist = Wishlist.find(params[:wishlist_id]) + wishlist = Wishlist.find_by_slug(params[:wishlist_id]) authorize wishlist.wishlist_items.build @wishlist_item = wishlist.wishlist_items.create!(wishlist_item_create_params) diff --git a/app/controllers/wishlists_controller.rb b/app/controllers/wishlists_controller.rb index 57b95a1..10b1895 100644 --- a/app/controllers/wishlists_controller.rb +++ b/app/controllers/wishlists_controller.rb @@ -3,7 +3,7 @@ class WishlistsController < ApplicationController def show skip_authorization - @wishlist = Wishlist.includes(wishlist_items: :item).find(params[:id]) + @wishlist = Wishlist.includes(wishlist_items: :item).find_by_slug(params[:id]) @site_managers = @wishlist.users @wishlist_items = @wishlist.wishlist_items.priority_order end @@ -50,7 +50,7 @@ def destroy private # Use callbacks to share common setup or constraints between actions. def set_wishlist - @wishlist = Wishlist.find(params[:id]) + @wishlist = Wishlist.find_by_slug(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. From d0b4ea312e25ecdf0bb1fb9a8856d83328ee0624 Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Wed, 25 Oct 2017 07:58:51 +0800 Subject: [PATCH 16/20] set slug as param --- app/models/wishlist.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/wishlist.rb b/app/models/wishlist.rb index 0816475..72fcfd3 100644 --- a/app/models/wishlist.rb +++ b/app/models/wishlist.rb @@ -18,6 +18,10 @@ class Wishlist < ApplicationRecord validates :name, presence: true, uniqueness: true + def to_param + slug + end + before_save :create_slug before_update :create_slug From 75e2744debf5c4e6ff12218b16e8cefdae24b567 Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Wed, 25 Oct 2017 07:59:26 +0800 Subject: [PATCH 17/20] expect path equal to link with parameter va-general --- spec/features/managing_items_and_wishlists_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/features/managing_items_and_wishlists_spec.rb b/spec/features/managing_items_and_wishlists_spec.rb index df3a7e8..131a6e7 100644 --- a/spec/features/managing_items_and_wishlists_spec.rb +++ b/spec/features/managing_items_and_wishlists_spec.rb @@ -104,12 +104,11 @@ scenario "I can update an existing wishlist" do click_link "DC General" - dc_general_path = current_path click_link "Edit Wishlist" fill_in("wishlist_name", with: "VA General") click_button "Update Wishlist" - expect(current_path).to eq dc_general_path + expect(current_path).to eq wishlist_path('va-general') expect(page).to have_text "Wishlist was successfully updated." expect(page).to have_text "VA General" end From 9283eba73bf1bfb1383a9cbd8bfb97a69a15d334 Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Thu, 26 Oct 2017 10:21:56 +0800 Subject: [PATCH 18/20] remove whitespace --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 01d3584..516cace 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,7 +11,7 @@ resource :amazon_search, controller: :amazon_search, only: [:new, :show] end - + # Users resources :users From d63b040bff696a31bb46ecf2ef7dec6c47e22c55 Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Thu, 26 Oct 2017 10:22:09 +0800 Subject: [PATCH 19/20] set slug column to unique --- db/migrate/20171023124712_add_column_slug_to_wishlists.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/db/migrate/20171023124712_add_column_slug_to_wishlists.rb b/db/migrate/20171023124712_add_column_slug_to_wishlists.rb index 52f1add..475afd8 100644 --- a/db/migrate/20171023124712_add_column_slug_to_wishlists.rb +++ b/db/migrate/20171023124712_add_column_slug_to_wishlists.rb @@ -1,5 +1,6 @@ class AddColumnSlugToWishlists < ActiveRecord::Migration[5.1] def change add_column :wishlists, :slug, :string + add_index :wishlists, :slug, unique: true end end From 205553ecf670fc489da3567cc7d2fa07ec89a582 Mon Sep 17 00:00:00 2001 From: Ryan Angelo Date: Thu, 26 Oct 2017 10:22:25 +0800 Subject: [PATCH 20/20] slug column is unique --- db/schema.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/db/schema.rb b/db/schema.rb index 59e4b3d..3b38844 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -77,6 +77,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "slug" + t.index ["slug"], name: "index_wishlists_on_slug", unique: true end add_foreign_key "pledges", "users"