books quantity

This commit is contained in:
Karol Selak 2021-03-22 01:24:13 +01:00
parent c2dfc1f04a
commit 733870ce96
7 changed files with 58 additions and 27 deletions

View file

@ -2,5 +2,5 @@
@import "https://fonts.googleapis.com/icon?family=Material+Icons";
body {
margin: 100px;
margin: 10px;
}

View file

@ -3,7 +3,7 @@ class BooksController < ApplicationController
before_action :ensure_admin, only: [:edit, :update]
def index
if current_user.admin?
if current_user&.admin?
books = Book.all
else
books = Book.published

View file

@ -1,20 +1,35 @@
<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| %>
<div class='row'>
<div class='col s3'>
Title: <%= link_to book.title, book %>
<div class='col s2'>
<%= link_to book.title, book %>
</div>
<div class='col s3'>
Price: <%= book.price_with_currency %>
<%= book.authors %>
</div>
<div class='col s3'>
Authors: <%= book.authors %>
<div class='col s1'>
<%= book.price_with_currency %>
</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'>
<%= book.published ? 'published' : 'unpublished' %>
</div>

View file

@ -13,22 +13,29 @@
<body>
<div class='container'>
<div class='row'>
<div class='col s1'>
<%= link_to 'Home', '/welcome', method: :get%>
</div>
<div class='card-panel'>
<div class='row'>
<div class='col s2'>
<%= link_to 'Home', '/welcome', method: :get%>
</div>
<div class='col s1'>
<%= link_to 'Books', '/books', method: :get%>
<div class='col s2'>
<%= 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>
<% 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>
<% flash.each do |type, notice| %>
<div class='card-panel teal lighten-5'>

View 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
View file

@ -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_225317) do
ActiveRecord::Schema.define(version: 2021_03_21_235625) do
create_table "audit_records", force: :cascade do |t|
t.string "model"
@ -43,6 +43,7 @@ ActiveRecord::Schema.define(version: 2021_03_21_225317) do
t.boolean "published"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "quantity"
t.index ["published"], name: "index_books_on_published"
end

View file

@ -2,17 +2,20 @@ books = Book.create([
{
title: 'Journey to the Center of the Earth',
price: 10900,
published: true
published: true,
quantity: 100
},
{
title: 'From the Earth to the Moon',
price: 6300,
published: false
published: false,
quantity: 0
},
{
title: 'Imaginary trip',
price: 3600,
published: true
published: true,
quantity: 0
},
])