shopping cart
This commit is contained in:
parent
733870ce96
commit
df7585c5e1
11 changed files with 79 additions and 11 deletions
|
@ -1,5 +1,5 @@
|
|||
class BooksController < ApplicationController
|
||||
before_action :set_book, only: [:show, :edit, :update]
|
||||
before_action :set_book, only: [:show, :edit, :update, :add_to_cart]
|
||||
before_action :ensure_admin, only: [:edit, :update]
|
||||
|
||||
def index
|
||||
|
@ -23,6 +23,17 @@ class BooksController < ApplicationController
|
|||
end
|
||||
end
|
||||
|
||||
def add_to_cart
|
||||
@book = Book.find(params[:id])
|
||||
current_user.books << @book
|
||||
@book.decrement!(:quantity)
|
||||
redirect_to '/books', notice: 'Book added to your cart'
|
||||
end
|
||||
|
||||
def shopping_cart
|
||||
@books = current_user.books.map { |book| BooksPresenter.new(book) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_book
|
||||
|
|
|
@ -15,4 +15,9 @@ class ApplicationRecord < ActiveRecord::Base
|
|||
AuditRecord.create(model: self.class, action: 'create', params: result.to_json)
|
||||
result
|
||||
end
|
||||
def decrement!(*args)
|
||||
result = super(*args)
|
||||
AuditRecord.create(model: self.class, action: 'decrement!', params: self.to_json)
|
||||
result
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
class User < ApplicationRecord
|
||||
has_and_belongs_to_many :books
|
||||
|
||||
has_secure_password
|
||||
has_secure_password :recovery_password, validations: false
|
||||
enum role: [:customer, :admin], _default: :customer
|
||||
|
|
|
@ -24,8 +24,8 @@
|
|||
<%= book.quantity %>
|
||||
</div>
|
||||
<div class='col s2'>
|
||||
<% if logged_in? && book.quantity.positive? %>
|
||||
<%= link_to 'Add', edit_book_path(book), class: "btn" %>
|
||||
<% if logged_in? && book.quantity.positive? && !current_user.books.exists?(book.id) %>
|
||||
<%= link_to 'Add', "book/#{book.id}/add_to_cart", method: :post, class: "btn" %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
|
|
22
app/views/books/shopping_cart.erb
Normal file
22
app/views/books/shopping_cart.erb
Normal file
|
@ -0,0 +1,22 @@
|
|||
<div class='container'>
|
||||
<div class='row'>
|
||||
<div class='col s2'>Title</div>
|
||||
<div class='col s3'>Authors</div>
|
||||
<div class='col s1'>Price</div>
|
||||
</div>
|
||||
<% @books.each do |book| %>
|
||||
<div class='row'>
|
||||
<div class='col s2'>
|
||||
<%= link_to book.title, book %>
|
||||
</div>
|
||||
|
||||
<div class='col s3'>
|
||||
<%= book.authors %>
|
||||
</div>
|
||||
|
||||
<div class='col s1'>
|
||||
<%= book.price_with_currency %>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
|
@ -32,7 +32,7 @@
|
|||
<% end %>
|
||||
<% if logged_in? %>
|
||||
<div class='col s2'>
|
||||
<%= link_to 'Shopping cart', '/authors', method: :get%>
|
||||
<%= link_to 'Shopping cart', '/shopping_cart', method: :get%>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
|
|
@ -9,6 +9,9 @@ Rails.application.routes.draw do
|
|||
get 'recover_password/:id/:recovery_password', to: 'users#recover_password_form'
|
||||
post 'recover_password', to: 'users#recover_password'
|
||||
post 'user/:id/block', to: 'users#block'
|
||||
post 'book/:id/add_to_cart', to: 'books#add_to_cart'
|
||||
get 'shopping_cart', to: 'books#shopping_cart'
|
||||
|
||||
resources :books
|
||||
resources :authors
|
||||
end
|
||||
|
|
10
db/migrate/20210322002803_create_books_users.rb
Normal file
10
db/migrate/20210322002803_create_books_users.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
class CreateBooksUsers < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
create_table :books_users do |t|
|
||||
t.belongs_to :book
|
||||
t.belongs_to :user
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
11
db/schema.rb
generated
11
db/schema.rb
generated
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2021_03_21_235625) do
|
||||
ActiveRecord::Schema.define(version: 2021_03_22_002803) do
|
||||
|
||||
create_table "audit_records", force: :cascade do |t|
|
||||
t.string "model"
|
||||
|
@ -47,6 +47,15 @@ ActiveRecord::Schema.define(version: 2021_03_21_235625) do
|
|||
t.index ["published"], name: "index_books_on_published"
|
||||
end
|
||||
|
||||
create_table "books_users", force: :cascade do |t|
|
||||
t.integer "book_id"
|
||||
t.integer "user_id"
|
||||
t.datetime "created_at", precision: 6, null: false
|
||||
t.datetime "updated_at", precision: 6, null: false
|
||||
t.index ["book_id"], name: "index_books_users_on_book_id"
|
||||
t.index ["user_id"], name: "index_books_users_on_user_id"
|
||||
end
|
||||
|
||||
create_table "users", force: :cascade do |t|
|
||||
t.string "email"
|
||||
t.string "password_digest"
|
||||
|
|
13
db/seeds.rb
13
db/seeds.rb
|
@ -15,7 +15,13 @@ books = Book.create([
|
|||
title: 'Imaginary trip',
|
||||
price: 3600,
|
||||
published: true,
|
||||
quantity: 0
|
||||
quantity: 1
|
||||
},
|
||||
{
|
||||
title: 'Winnie the Pooh',
|
||||
price: 3700,
|
||||
published: true,
|
||||
quantity: 5
|
||||
},
|
||||
])
|
||||
|
||||
|
@ -32,12 +38,17 @@ authors = Author.create([
|
|||
first_name: 'Rick',
|
||||
last_name: 'Pickle'
|
||||
},
|
||||
{
|
||||
first_name: 'Alan',
|
||||
last_name: 'Milne'
|
||||
},
|
||||
])
|
||||
|
||||
books.first.authors << authors.first
|
||||
books.second.authors << authors.first
|
||||
books.third.authors << authors.second
|
||||
books.third.authors << authors.third
|
||||
books.fourth.authors << authors.fourth
|
||||
|
||||
User.create([
|
||||
{
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe AuditRecord, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue