From 733870ce968d8589d2d7cb283e1fe1b40843b828 Mon Sep 17 00:00:00 2001 From: Karol Selak Date: Mon, 22 Mar 2021 01:24:13 +0100 Subject: [PATCH] books quantity --- app/assets/stylesheets/application.scss | 2 +- app/controllers/books_controller.rb | 2 +- app/views/books/index.html.erb | 29 +++++++++++---- app/views/layouts/application.html.erb | 35 +++++++++++-------- .../20210321235625_add_quantity_to_books.rb | 5 +++ db/schema.rb | 3 +- db/seeds.rb | 9 +++-- 7 files changed, 58 insertions(+), 27 deletions(-) create mode 100644 db/migrate/20210321235625_add_quantity_to_books.rb diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index e1ecc73..64cc60f 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -2,5 +2,5 @@ @import "https://fonts.googleapis.com/icon?family=Material+Icons"; body { - margin: 100px; + margin: 10px; } \ No newline at end of file diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index 499d455..4eb947d 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -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 diff --git a/app/views/books/index.html.erb b/app/views/books/index.html.erb index b1bb82a..2359275 100644 --- a/app/views/books/index.html.erb +++ b/app/views/books/index.html.erb @@ -1,20 +1,35 @@
+
+
Title
+
Authors
+
Price
+
Quantity
+ +
<% @books.each do |book| %>
+
+ <%= link_to book.title, book %> +
+
- Title: <%= link_to book.title, book %> + <%= book.authors %>
-
- Price: <%= book.price_with_currency %> +
+ <%= book.price_with_currency %>
-
- Authors: <%= book.authors %> - +
+ <%= book.quantity %> +
+
+ <% if logged_in? && book.quantity.positive? %> + <%= link_to 'Add', edit_book_path(book), class: "btn" %> + <% end %>
- <% if current_user.admin? %> + <% if current_user&.admin? %>
<%= book.published ? 'published' : 'unpublished' %>
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 0bffcd9..af8d743 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -13,22 +13,29 @@
-
-
- <%= link_to 'Home', '/welcome', method: :get%> -
+
+
+
+ <%= link_to 'Home', '/welcome', method: :get%> +
-
- <%= link_to 'Books', '/books', method: :get%> +
+ <%= link_to 'Books', '/books', method: :get%> +
+ <% if current_user&.admin? %> +
+ <%= link_to 'Authors', '/authors', method: :get%> +
+
+ <%= link_to 'Users', '/users', method: :get%> +
+ <% end %> + <% if logged_in? %> +
+ <%= link_to 'Shopping cart', '/authors', method: :get%> +
+ <% end %>
- <% if current_user&.admin? %> -
- <%= link_to 'Authors', '/authors', method: :get%> -
-
- <%= link_to 'Users', '/users', method: :get%> -
- <% end %>
<% flash.each do |type, notice| %>
diff --git a/db/migrate/20210321235625_add_quantity_to_books.rb b/db/migrate/20210321235625_add_quantity_to_books.rb new file mode 100644 index 0000000..ee6dbc1 --- /dev/null +++ b/db/migrate/20210321235625_add_quantity_to_books.rb @@ -0,0 +1,5 @@ +class AddQuantityToBooks < ActiveRecord::Migration[6.1] + def change + add_column :books, :quantity, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 44467b1..27ba16b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/db/seeds.rb b/db/seeds.rb index 5f2ae2a..f2c9c04 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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 }, ])