diff --git a/app/controllers/authors_controller.rb b/app/controllers/authors_controller.rb new file mode 100644 index 0000000..f96718c --- /dev/null +++ b/app/controllers/authors_controller.rb @@ -0,0 +1,5 @@ +class AuthorsController < ApplicationController + def index + @authors = Author.all + end +end diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index bafab99..3ef6625 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -1,5 +1,43 @@ class BooksController < ApplicationController + before_action :set_book, only: [:show, :edit, :update] + before_action :ensure_admin, only: [:edit, :update] + def index - @books = Book.published.map { |book| BooksPresenter.new(book) } + if current_user.admin? + books = Book.all + else + books = Book.published + end + @books = books.map { |book| BooksPresenter.new(book) } + end + + def show + end + + def edit + end + + def update + if @book.update(book_params) + redirect_to '/books' + end + end + + private + + def ensure_admin + unless current_user&.admin? + redirect_to '/welcome', notice: 'You are not allowed to perform this action' + end + end + + def set_book + @book = BooksPresenter.new(Book.find(params[:id])) + end + + def book_params + result = params.require(:book).permit(:title, :price, :published) + result['price'] = result['price'].to_d * 100 + result end end diff --git a/app/models/book.rb b/app/models/book.rb index 860c5d5..e0b5624 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -4,5 +4,4 @@ class Book < ApplicationRecord scope :published, -> { where(published: true) } validates :title, presence: true validates :price, presence: true - validates :published, presence: true end diff --git a/app/models/user.rb b/app/models/user.rb index 174b4bc..9731bb0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,5 +5,9 @@ class User < ApplicationRecord validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP } validates :role, presence: true - validates :password, presence: true, length: { minimum: 8 } + validates :password, { + presence: true, + length: { minimum: 8 }, + if: lambda{ new_record? || !password.nil? } + } end diff --git a/app/presenters/books_presenter.rb b/app/presenters/books_presenter.rb index 49888bb..c6b3f40 100644 --- a/app/presenters/books_presenter.rb +++ b/app/presenters/books_presenter.rb @@ -2,7 +2,11 @@ class BooksPresenter < SimpleDelegator include ActiveSupport::NumberHelper def price - number_to_currency(super / 100) + super / 100 + end + + def price_with_currency + number_to_currency(price) end def authors diff --git a/app/views/books/edit.html.erb b/app/views/books/edit.html.erb new file mode 100644 index 0000000..d45ee1c --- /dev/null +++ b/app/views/books/edit.html.erb @@ -0,0 +1,14 @@ +

Edit book

+<%= form_for @book do |f|%> + <%= f.label :title %> + <%= f.text_field :title %> + <%= f.label :price %> + <%= f.number_field :price, step: 0.01 %> + + <%= f.submit 'Save changes', class: 'btn' %> +<%end%> \ No newline at end of file diff --git a/app/views/books/index.html.erb b/app/views/books/index.html.erb index 904091b..b1bb82a 100644 --- a/app/views/books/index.html.erb +++ b/app/views/books/index.html.erb @@ -1,17 +1,27 @@
<% @books.each do |book| %>
-
- Title: <%= book.title %> +
+ Title: <%= link_to book.title, book %>
-
- Price: <%= book.price %> +
+ Price: <%= book.price_with_currency %>
-
+
Authors: <%= book.authors %> +
+ + <% if current_user.admin? %> +
+ <%= book.published ? 'published' : 'unpublished' %> +
+
+ <%= link_to 'Edit', edit_book_path(book), class: "btn" %> +
+ <% end %>
<% end %>
diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb new file mode 100644 index 0000000..5370cff --- /dev/null +++ b/app/views/books/show.html.erb @@ -0,0 +1,20 @@ +
+
+
+ Title: <%= link_to @book.title, @book %> +
+ +
+ Price: <%= @book.price_with_currency %> +
+ +
+ Authors: <%= @book.authors %> +
+
+ <% if current_user&.admin? %> +
+ <%= link_to 'Edit', edit_book_path(@book), class: "btn" %> +
+ <% end %> +
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 2a12733..455b4e4 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -12,6 +12,7 @@ <%= link_to 'Home', '/welcome', method: :get%> + <%= link_to 'Books', '/books', method: :get%> <% flash.each do |type, notice| %>
<% if notice.is_a? String %> diff --git a/db/seeds.rb b/db/seeds.rb index 1f3af18..74e1732 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -31,21 +31,25 @@ authors = Author.create([ }, ]) -BookAuthor.create([ +books.first.authors << authors.first +books.second.authors << authors.first +books.third.authors << authors.second +books.third.authors << authors.third + +User.create([ { - book: books.first, - author: authors.first + email: 'abc@o2.pl', + password: 'aaaaaaaa', + role: :admin }, { - book: books[1], - author: authors.first + email: 'abcd@o2.pl', + password: 'aaaaaaaa', + role: :customer }, { - book: books[2], - author: authors[1] - }, - { - book: books[2], - author: authors[2] + email: 'abcde@o2.pl', + password: 'aaaaaaaa', + role: :customer }, ]) \ No newline at end of file