Skip to content

Commit 3ee741b

Browse files
Merge pull request #1 from mud-turtles-2014/wishlists
Wishlists
2 parents 9788dd3 + 94059de commit 3ee741b

File tree

8 files changed

+126
-0
lines changed

8 files changed

+126
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
class WishlistsController < ApplicationController
2+
before_action :set_wishlist, only: [:show, :edit, :update, :destroy]
3+
# GET /wishlists
4+
# GET /wishlists.json
5+
def index
6+
@current_user = current_user
7+
@wishlists = Wishlist.all
8+
end
9+
10+
# GET /wishlists/1
11+
# GET /wishlists/1.json
12+
def show
13+
end
14+
15+
# GET /wishlists/new
16+
def new
17+
@current_user = current_user
18+
@wishlist = @current_user.wishlists.new
19+
# @trip.expenses << Expense.new
20+
end
21+
22+
# GET /wishlists/1/edit
23+
def edit
24+
25+
end
26+
27+
# POST /wishlists
28+
# POST /wishlists.json
29+
def create
30+
@current_user = current_user
31+
@wishlist = @current_user.wishlists.new(wishlist_params)
32+
33+
respond_to do |format|
34+
if @wishlist.save
35+
format.html { redirect_to wishlist_path(@wishlist), notice: 'Wishlist was successfully created.' }
36+
#format.html { redirect_to @trip, notice: 'Trip was successfully created.' }
37+
format.json { render :show, status: :created, location: @wishlist }
38+
else
39+
format.html { render :new }
40+
format.json { render json: @wishlist.errors, status: :unprocessable_entity }
41+
end
42+
end
43+
end
44+
45+
# PATCH/PUT /wishlists/1
46+
# PATCH/PUT /wishlists/1.json
47+
def update
48+
respond_to do |format|
49+
if @wishlist.update(wishlist_params)
50+
format.html { redirect_to @wishlist, notice: 'Wishlist was successfully updated.' }
51+
format.json { render :show, status: :ok, location: @wishlist }
52+
else
53+
format.html { render :edit }
54+
format.json { render json: @wishlist.errors, status: :unprocessable_entity }
55+
end
56+
end
57+
end
58+
59+
# DELETE /wishlists/1
60+
# DELETE /wishlists/1.json
61+
def destroy
62+
@wishlist.destroy
63+
respond_to do |format|
64+
format.html { redirect_to wishlists_url, notice: 'Wishlist was successfully destroyed.' }
65+
format.json { head :no_content }
66+
end
67+
end
68+
69+
private
70+
# Use callbacks to share common setup or constraints between actions.
71+
def set_wishlist
72+
@wishlist = Wishlist.find(params[:id])
73+
end
74+
75+
# Never trust parameters from the scary internet, only allow the white list through.
76+
def wishlist_params
77+
params.require(:wishlist).permit(:name, :budget)
78+
end
79+
end

GoBug/app/models/user.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ class User < ActiveRecord::Base
22
has_secure_password
33

44
has_many :trips
5+
has_many :wishlists
56
has_many :expenses, through: :trips
67
end

GoBug/app/models/wishlist.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Wishlist < ActiveRecord::Base
2+
has_many :wishlist_items
3+
has_many :items, through: :wishlist_items
4+
belongs_to :user
5+
end

GoBug/app/models/wishlist_item.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class WishlistItem< ActiveRecord::Base
2+
belongs_to :wishlist
3+
belongs_to :item
4+
end

GoBug/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
resources :expenses, shallow: true
2020
end
2121

22+
resources :wishlists
2223
# The priority is based upon order of creation: first created -> highest priority.
2324
# See how all your routes lay out with "rake routes".
2425

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CreateWishlists < ActiveRecord::Migration
2+
def change
3+
create_table :wishlists do |t|
4+
t.references :user
5+
t.string :name
6+
t.float :budget
7+
8+
t.timestamps
9+
end
10+
end
11+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateWishlistItems < ActiveRecord::Migration
2+
def change
3+
create_table :wishlist_items do |t|
4+
t.references :item
5+
t.references :wishlist
6+
7+
t.timestamps
8+
end
9+
end
10+
end

GoBug/db/schema.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,19 @@
7575
t.datetime "updated_at"
7676
end
7777

78+
create_table "wishlist_items", force: true do |t|
79+
t.integer "item_id"
80+
t.integer "wishlist_id"
81+
t.datetime "created_at"
82+
t.datetime "updated_at"
83+
end
84+
85+
create_table "wishlists", force: true do |t|
86+
t.integer "user_id"
87+
t.string "name"
88+
t.float "budget"
89+
t.datetime "created_at"
90+
t.datetime "updated_at"
91+
end
92+
7893
end

0 commit comments

Comments
 (0)