Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/amazon_search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/wishlist_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/wishlists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions app/models/wishlist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,17 @@ class Wishlist < ApplicationRecord

validates :name, presence: true,
uniqueness: true

def to_param
slug
end

before_save :create_slug
before_update :create_slug

private

def create_slug
self.slug = name.parameterize
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when an admin tries to add/update a valid wishlist that results in a duplicate slug? For example, "Wishlist 1" and "wishlist-1" are unique names but would result in the same slug. Since we're using them as the param, slugs must be unique.

Validations are run before the before_save callback, so a simple validation won't work. Maybe a before_validation callback or some sort of slug incrementing would work? I bet someone's solved this and written a blog post.

end
end
6 changes: 6 additions & 0 deletions db/migrate/20171023124712_add_column_slug_to_wishlists.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddColumnSlugToWishlists < ActiveRecord::Migration[5.1]
def change
add_column :wishlists, :slug, :string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a unique index to this column since we search by it so often. We should probably disallow nulls too.

add_index :wishlists, :slug, unique: true
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -76,6 +76,8 @@
t.text "name"
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"
Expand Down
3 changes: 1 addition & 2 deletions spec/features/managing_items_and_wishlists_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,11 @@

scenario "I can update an existing wishlist" do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is fine, but let's really test this change in the unit tests (spec/models/wishlist_spec.rb).

For the model specs, you'll want to verify that the slug gets set correctly after a save or an update. We'll also need to test how wishlist items with duplicate slugs are handled (see above). We probably don't need to test input edge cases since String#parameterize/ActiveSupport are well-tested already.

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
Expand Down
1 change: 0 additions & 1 deletion spec/routing/wishlists_routing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@
it "routes to #destroy" do
expect(:delete => "/wishlists/1").to route_to("wishlists#destroy", :id => "1")
end

end
end