books edition by admin
This commit is contained in:
parent
5095f63fd7
commit
e36cc36947
10 changed files with 119 additions and 20 deletions
5
app/controllers/authors_controller.rb
Normal file
5
app/controllers/authors_controller.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class AuthorsController < ApplicationController
|
||||||
|
def index
|
||||||
|
@authors = Author.all
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,5 +1,43 @@
|
||||||
class BooksController < ApplicationController
|
class BooksController < ApplicationController
|
||||||
|
before_action :set_book, only: [:show, :edit, :update]
|
||||||
|
before_action :ensure_admin, only: [:edit, :update]
|
||||||
|
|
||||||
def index
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,5 +4,4 @@ class Book < ApplicationRecord
|
||||||
scope :published, -> { where(published: true) }
|
scope :published, -> { where(published: true) }
|
||||||
validates :title, presence: true
|
validates :title, presence: true
|
||||||
validates :price, presence: true
|
validates :price, presence: true
|
||||||
validates :published, presence: true
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,5 +5,9 @@ class User < ApplicationRecord
|
||||||
|
|
||||||
validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
|
validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
|
||||||
validates :role, presence: true
|
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
|
end
|
||||||
|
|
|
@ -2,7 +2,11 @@ class BooksPresenter < SimpleDelegator
|
||||||
include ActiveSupport::NumberHelper
|
include ActiveSupport::NumberHelper
|
||||||
|
|
||||||
def price
|
def price
|
||||||
number_to_currency(super / 100)
|
super / 100
|
||||||
|
end
|
||||||
|
|
||||||
|
def price_with_currency
|
||||||
|
number_to_currency(price)
|
||||||
end
|
end
|
||||||
|
|
||||||
def authors
|
def authors
|
||||||
|
|
14
app/views/books/edit.html.erb
Normal file
14
app/views/books/edit.html.erb
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
<h4>Edit book</h4>
|
||||||
|
<%= form_for @book do |f|%>
|
||||||
|
<%= f.label :title %>
|
||||||
|
<%= f.text_field :title %>
|
||||||
|
<%= f.label :price %>
|
||||||
|
<%= f.number_field :price, step: 0.01 %>
|
||||||
|
<label>
|
||||||
|
<div>
|
||||||
|
<%= f.check_box :published, class: 'filled-in' %>
|
||||||
|
<span>published</span>
|
||||||
|
</div>
|
||||||
|
</label>
|
||||||
|
<%= f.submit 'Save changes', class: 'btn' %>
|
||||||
|
<%end%>
|
|
@ -1,17 +1,27 @@
|
||||||
<div class='container'>
|
<div class='container'>
|
||||||
<% @books.each do |book| %>
|
<% @books.each do |book| %>
|
||||||
<div class='row'>
|
<div class='row'>
|
||||||
<div class='col s4'>
|
<div class='col s3'>
|
||||||
Title: <%= book.title %>
|
Title: <%= link_to book.title, book %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class='col s4'>
|
<div class='col s3'>
|
||||||
Price: <%= book.price %>
|
Price: <%= book.price_with_currency %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class='col s4'>
|
<div class='col s3'>
|
||||||
Authors: <%= book.authors %>
|
Authors: <%= book.authors %>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<% if current_user.admin? %>
|
||||||
|
<div class='col s2'>
|
||||||
|
<%= book.published ? 'published' : 'unpublished' %>
|
||||||
|
</div>
|
||||||
|
<div class='col s1'>
|
||||||
|
<%= link_to 'Edit', edit_book_path(book), class: "btn" %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|
20
app/views/books/show.html.erb
Normal file
20
app/views/books/show.html.erb
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
<div class='container'>
|
||||||
|
<div class='row'>
|
||||||
|
<div class='col s4'>
|
||||||
|
Title: <%= link_to @book.title, @book %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class='col s4'>
|
||||||
|
Price: <%= @book.price_with_currency %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class='col s4'>
|
||||||
|
Authors: <%= @book.authors %>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<% if current_user&.admin? %>
|
||||||
|
<div class='row'>
|
||||||
|
<%= link_to 'Edit', edit_book_path(@book), class: "btn" %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
|
@ -12,6 +12,7 @@
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<%= link_to 'Home', '/welcome', method: :get%>
|
<%= link_to 'Home', '/welcome', method: :get%>
|
||||||
|
<%= link_to 'Books', '/books', method: :get%>
|
||||||
<% flash.each do |type, notice| %>
|
<% flash.each do |type, notice| %>
|
||||||
<div class='card-panel teal lighten-5'>
|
<div class='card-panel teal lighten-5'>
|
||||||
<% if notice.is_a? String %>
|
<% if notice.is_a? String %>
|
||||||
|
|
26
db/seeds.rb
26
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,
|
email: 'abc@o2.pl',
|
||||||
author: authors.first
|
password: 'aaaaaaaa',
|
||||||
|
role: :admin
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
book: books[1],
|
email: 'abcd@o2.pl',
|
||||||
author: authors.first
|
password: 'aaaaaaaa',
|
||||||
|
role: :customer
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
book: books[2],
|
email: 'abcde@o2.pl',
|
||||||
author: authors[1]
|
password: 'aaaaaaaa',
|
||||||
},
|
role: :customer
|
||||||
{
|
|
||||||
book: books[2],
|
|
||||||
author: authors[2]
|
|
||||||
},
|
},
|
||||||
])
|
])
|
Loading…
Add table
Add a link
Reference in a new issue