books quantity
This commit is contained in:
parent
c2dfc1f04a
commit
733870ce96
7 changed files with 58 additions and 27 deletions
|
@ -2,5 +2,5 @@
|
||||||
@import "https://fonts.googleapis.com/icon?family=Material+Icons";
|
@import "https://fonts.googleapis.com/icon?family=Material+Icons";
|
||||||
|
|
||||||
body {
|
body {
|
||||||
margin: 100px;
|
margin: 10px;
|
||||||
}
|
}
|
|
@ -3,7 +3,7 @@ class BooksController < ApplicationController
|
||||||
before_action :ensure_admin, only: [:edit, :update]
|
before_action :ensure_admin, only: [:edit, :update]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
if current_user.admin?
|
if current_user&.admin?
|
||||||
books = Book.all
|
books = Book.all
|
||||||
else
|
else
|
||||||
books = Book.published
|
books = Book.published
|
||||||
|
|
|
@ -1,20 +1,35 @@
|
||||||
<div class='container'>
|
<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 class='col s3'>Quantity</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
<% @books.each do |book| %>
|
<% @books.each do |book| %>
|
||||||
<div class='row'>
|
<div class='row'>
|
||||||
<div class='col s3'>
|
<div class='col s2'>
|
||||||
Title: <%= link_to book.title, book %>
|
<%= link_to book.title, book %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class='col s3'>
|
<div class='col s3'>
|
||||||
Price: <%= book.price_with_currency %>
|
<%= book.authors %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class='col s3'>
|
<div class='col s1'>
|
||||||
Authors: <%= book.authors %>
|
<%= book.price_with_currency %>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<% if current_user.admin? %>
|
<div class='col s1'>
|
||||||
|
<%= book.quantity %>
|
||||||
|
</div>
|
||||||
|
<div class='col s2'>
|
||||||
|
<% if logged_in? && book.quantity.positive? %>
|
||||||
|
<%= link_to 'Add', edit_book_path(book), class: "btn" %>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<% if current_user&.admin? %>
|
||||||
<div class='col s2'>
|
<div class='col s2'>
|
||||||
<%= book.published ? 'published' : 'unpublished' %>
|
<%= book.published ? 'published' : 'unpublished' %>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -13,22 +13,29 @@
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div class='container'>
|
<div class='container'>
|
||||||
<div class='row'>
|
<div class='card-panel'>
|
||||||
<div class='col s1'>
|
<div class='row'>
|
||||||
<%= link_to 'Home', '/welcome', method: :get%>
|
<div class='col s2'>
|
||||||
</div>
|
<%= link_to 'Home', '/welcome', method: :get%>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class='col s1'>
|
<div class='col s2'>
|
||||||
<%= link_to 'Books', '/books', method: :get%>
|
<%= link_to 'Books', '/books', method: :get%>
|
||||||
|
</div>
|
||||||
|
<% if current_user&.admin? %>
|
||||||
|
<div class='col s2'>
|
||||||
|
<%= link_to 'Authors', '/authors', method: :get%>
|
||||||
|
</div>
|
||||||
|
<div class='col s2'>
|
||||||
|
<%= link_to 'Users', '/users', method: :get%>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
<% if logged_in? %>
|
||||||
|
<div class='col s2'>
|
||||||
|
<%= link_to 'Shopping cart', '/authors', method: :get%>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
<% if current_user&.admin? %>
|
|
||||||
<div class='col s1'>
|
|
||||||
<%= link_to 'Authors', '/authors', method: :get%>
|
|
||||||
</div>
|
|
||||||
<div class='col s1'>
|
|
||||||
<%= link_to 'Users', '/users', method: :get%>
|
|
||||||
</div>
|
|
||||||
<% end %>
|
|
||||||
</div>
|
</div>
|
||||||
<% flash.each do |type, notice| %>
|
<% flash.each do |type, notice| %>
|
||||||
<div class='card-panel teal lighten-5'>
|
<div class='card-panel teal lighten-5'>
|
||||||
|
|
5
db/migrate/20210321235625_add_quantity_to_books.rb
Normal file
5
db/migrate/20210321235625_add_quantity_to_books.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class AddQuantityToBooks < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
add_column :books, :quantity, :integer
|
||||||
|
end
|
||||||
|
end
|
3
db/schema.rb
generated
3
db/schema.rb
generated
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2021_03_21_225317) do
|
ActiveRecord::Schema.define(version: 2021_03_21_235625) do
|
||||||
|
|
||||||
create_table "audit_records", force: :cascade do |t|
|
create_table "audit_records", force: :cascade do |t|
|
||||||
t.string "model"
|
t.string "model"
|
||||||
|
@ -43,6 +43,7 @@ ActiveRecord::Schema.define(version: 2021_03_21_225317) do
|
||||||
t.boolean "published"
|
t.boolean "published"
|
||||||
t.datetime "created_at", precision: 6, null: false
|
t.datetime "created_at", precision: 6, null: false
|
||||||
t.datetime "updated_at", precision: 6, null: false
|
t.datetime "updated_at", precision: 6, null: false
|
||||||
|
t.integer "quantity"
|
||||||
t.index ["published"], name: "index_books_on_published"
|
t.index ["published"], name: "index_books_on_published"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -2,17 +2,20 @@ books = Book.create([
|
||||||
{
|
{
|
||||||
title: 'Journey to the Center of the Earth',
|
title: 'Journey to the Center of the Earth',
|
||||||
price: 10900,
|
price: 10900,
|
||||||
published: true
|
published: true,
|
||||||
|
quantity: 100
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'From the Earth to the Moon',
|
title: 'From the Earth to the Moon',
|
||||||
price: 6300,
|
price: 6300,
|
||||||
published: false
|
published: false,
|
||||||
|
quantity: 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Imaginary trip',
|
title: 'Imaginary trip',
|
||||||
price: 3600,
|
price: 3600,
|
||||||
published: true
|
published: true,
|
||||||
|
quantity: 0
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue