WIP: Karol's implementation #1

Closed
stan wants to merge 23 commits from karol_master into master
19 changed files with 190 additions and 75 deletions
Showing only changes of commit ccb6e23960 - Show all commits

View file

@ -24,6 +24,8 @@ gem 'bcrypt', '~> 3.1.7'
gem 'materialize-sass', '~> 1.0.0' gem 'materialize-sass', '~> 1.0.0'
gem 'jquery-rails'
# Use Active Storage variant # Use Active Storage variant
# gem 'image_processing', '~> 1.2' # gem 'image_processing', '~> 1.2'

View file

@ -91,6 +91,10 @@ GEM
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
jbuilder (2.11.2) jbuilder (2.11.2)
activesupport (>= 5.0.0) activesupport (>= 5.0.0)
jquery-rails (4.4.0)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
listen (3.4.1) listen (3.4.1)
rb-fsevent (~> 0.10, >= 0.10.3) rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10) rb-inotify (~> 0.9, >= 0.9.10)
@ -241,6 +245,7 @@ DEPENDENCIES
byebug byebug
capybara (>= 3.26) capybara (>= 3.26)
jbuilder (~> 2.7) jbuilder (~> 2.7)
jquery-rails
listen (~> 3.3) listen (~> 3.3)
materialize-sass (~> 1.0.0) materialize-sass (~> 1.0.0)
puma (~> 5.0) puma (~> 5.0)

View file

@ -10,10 +10,18 @@ class ApplicationController < ActionController::Base
!current_user.nil? !current_user.nil?
end end
protected
def notices_from_errors(record) def notices_from_errors(record)
messages = record.errors.messages.map do |attribute, messages| messages = record.errors.messages.map do |attribute, messages|
messages.map { |message| "#{attribute} #{message}".capitalize } messages.map { |message| "#{attribute} #{message}".capitalize }
end end
messages.flatten messages.flatten
end end
def ensure_admin
unless current_user&.admin?
redirect_to '/welcome', notice: 'You are not allowed to perform this action'
end
end
end end

View file

@ -1,5 +1,28 @@
class AuthorsController < ApplicationController class AuthorsController < ApplicationController
before_action :ensure_admin
before_action :set_author, only: [:edit, :update]
def index def index
@authors = Author.all @authors = Author.all
end end
def edit
@author = Author.find(params[:id])
end
def update
if @author.update(author_params)
redirect_to '/authors'
end
end
private
def set_author
@author = Author.find(params[:id])
end
def author_params
params.require(:author).permit(:first_name, :last_name)
end
end end

View file

@ -25,12 +25,6 @@ class BooksController < ApplicationController
private 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 def set_book
@book = BooksPresenter.new(Book.find(params[:id])) @book = BooksPresenter.new(Book.find(params[:id]))
end end

View file

@ -1,4 +1,10 @@
class UsersController < ApplicationController class UsersController < ApplicationController
before_action :ensure_admin, only: [:destroy]
def index
@users = User.all
end
def new def new
@user = User.new @user = User.new
end end
@ -45,4 +51,9 @@ class UsersController < ApplicationController
redirect_to '/welcome', notice: 'Recovery link expired or invalid' redirect_to '/welcome', notice: 'Recovery link expired or invalid'
end end
end end
def destroy
User.destroy(params[:id])
redirect_to '/users'
end
end end

View file

@ -4,6 +4,8 @@
// that code so it'll be compiled. // that code so it'll be compiled.
//= require materialize //= require materialize
//= require jquery
//= require jquery_ujs
import Rails from "@rails/ujs" import Rails from "@rails/ujs"
import Turbolinks from "turbolinks" import Turbolinks from "turbolinks"

View file

@ -0,0 +1,10 @@
<div class='container'>
<h4>Edit author</h4>
<%= form_for @author do |f|%>
<%= f.label :first_name %>
<%= f.text_field :first_name %>
<%= f.label :last_name %>
<%= f.text_field :last_name %>
<%= f.submit 'Save changes', class: 'btn' %>
<%end%>
</div>

View file

@ -0,0 +1,13 @@
<div class='container'>
<% @authors.each do |author| %>
<div class='row'>
<div class='col s11'>
<%= author.first_name %> <%= author.last_name %>
</div>
<div class='col s1'>
<%= link_to 'Edit', edit_author_path(author), class: "btn" %>
</div>
</div>
<% end %>
</div>

View file

@ -1,14 +1,16 @@
<h4>Edit book</h4> <div class='container'>
<%= form_for @book do |f|%> <h4>Edit book</h4>
<%= f.label :title %> <%= form_for @book do |f|%>
<%= f.text_field :title %> <%= f.label :title %>
<%= f.label :price %> <%= f.text_field :title %>
<%= f.number_field :price, step: 0.01 %> <%= f.label :price %>
<label> <%= f.number_field :price, step: 0.01 %>
<div> <label>
<%= f.check_box :published, class: 'filled-in' %> <div>
<span>published</span> <%= f.check_box :published, class: 'filled-in' %>
</div> <span>published</span>
</label> </div>
<%= f.submit 'Save changes', class: 'btn' %> </label>
<%end%> <%= f.submit 'Save changes', class: 'btn' %>
<%end%>
</div>

View file

@ -8,22 +8,40 @@
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'rails-ujs' %>
</head> </head>
<body> <body>
<%= link_to 'Home', '/welcome', method: :get%> <div class='container'>
<%= link_to 'Books', '/books', method: :get%> <div class='row'>
<% flash.each do |type, notice| %> <div class='col s1'>
<div class='card-panel teal lighten-5'> <%= link_to 'Home', '/welcome', method: :get%>
<% if notice.is_a? String %> </div>
<%= notice %>
<% else %> <div class='col s1'>
<% notice.each do |msg| %> <%= link_to 'Books', '/books', method: :get%>
<div><%= msg %></div> </div>
<% end %> <% 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 %> <% end %>
</div> </div>
<% end %> <% flash.each do |type, notice| %>
<div class='card-panel teal lighten-5'>
<% if notice.is_a? String %>
<%= notice %>
<% else %>
<% notice.each do |msg| %>
<div><%= msg %></div>
<% end %>
<% end %>
</div>
<% end %>
</div>
<%= yield %> <%= yield %>
</body> </body>
</html> </html>

View file

@ -1,9 +1,11 @@
<h4>Login</h4> <div class='container'>
<%= form_tag '/login' do %> <h4>Login</h4>
<%= label_tag :email%> <%= form_tag '/login' do %>
<%= text_field_tag :email %> <%= label_tag :email%>
<%= label_tag :password%> <%= text_field_tag :email %>
<%= password_field_tag :password%> <%= label_tag :password%>
<%= submit_tag "Login", class: 'btn' %> <%= password_field_tag :password%>
<%end%> <%= submit_tag "Login", class: 'btn' %>
<%= link_to "Password recovery", '/password_recovery_request', method: :get%> <%end%>
<%= link_to "Password recovery", '/password_recovery_request', method: :get %>
</div>

View file

@ -1,8 +1,10 @@
<h4>Welcome</h4> <div class='container'>
<% if logged_in? %> <h4>Welcome</h4>
You are Logged In, <%= current_user.email %> <% if logged_in? %>
<%= button_to "Logout", '/logout', method: :get, class: 'btn' %> You are Logged In, <%= current_user.email %>
<% else %> <%= button_to "Logout", '/logout', method: :get, class: 'btn' %>
<%= button_to "Login", '/login', method: :get, class: 'btn' %> <% else %>
<%= button_to "Sign Up", '/users/new', method: :get, class: 'btn' %> <%= button_to "Login", '/login', method: :get, class: 'btn' %>
<% end %> <%= button_to "Sign Up", '/users/new', method: :get, class: 'btn' %>
<% end %>
</div>

View file

@ -0,0 +1,15 @@
<div class='container'>
<% @users.each do |user| %>
<div class='row'>
<div class='col s3'>
<%= user.email %>
</div>
<div class='col s3'>
<%= user.role %>
</div>
<div class='col s3'>
<%= link_to 'Delete', user, method: :delete, class: "btn" %>
</div>
</div>
<% end %>
</div>

View file

@ -1,8 +1,10 @@
<h4>Sign Up</h4> <div class='container'>
<%= form_for @user do |f|%> <h4>Sign Up</h4>
<%= f.label :email%> <%= form_for @user do |f|%>
<%= f.text_field :email%> <%= f.label :email%>
<%= f.label :password%> <%= f.text_field :email%>
<%= f.password_field :password%> <%= f.label :password%>
<%= f.submit 'Sign up', class: 'btn' %> <%= f.password_field :password%>
<%end%> <%= f.submit 'Sign up', class: 'btn' %>
<%end%>
</div>

View file

@ -1,7 +1,9 @@
<h4>Password recovery</h4> <div class='container'>
Provide an email to password recovery <h4>Password recovery</h4>
<%= form_with url: "/password_recovery_request", method: :post do |f| %> Provide an email to password recovery
<%= f.label :email%><br> <%= form_with url: "/password_recovery_request", method: :post do |f| %>
<%= f.text_field :email %> <%= f.label :email%><br>
<%= f.submit 'Send email', class: 'btn' %> <%= f.text_field :email %>
<% end %> <%= f.submit 'Send email', class: 'btn' %>
<% end %>
</div>

View file

@ -1,11 +1,13 @@
<h4>Provide new password</h4> <div class='container'>
<h4>Provide new password</h4>
<%= form_with url: '/recover_password', method: :post do |f| %> <%= form_with url: '/recover_password', method: :post do |f| %>
<%= f.label :password%> <%= f.label :password%>
<%= f.password_field :password %> <%= f.password_field :password %>
<%= f.label :password_confirmation%> <%= f.label :password_confirmation%>
<%= f.password_field :password_confirmation %> <%= f.password_field :password_confirmation %>
<%= f.hidden_field :recovery_password, :value => @recovery_password %> <%= f.hidden_field :recovery_password, :value => @recovery_password %>
<%= f.hidden_field :user_id, :value => @user_id %> <%= f.hidden_field :user_id, :value => @user_id %>
<%= f.submit 'Change password', class: 'btn' %> <%= f.submit 'Change password', class: 'btn' %>
<% end %> <% end %>
</div>

View file

@ -56,7 +56,8 @@ Rails.application.configure do
# Debug mode disables concatenation and preprocessing of assets. # Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large # This option may cause significant delays in view rendering with a large
# number of complex assets. # number of complex assets.
config.assets.debug = true config.assets.debug = false
config.assets.check_precompiled_asset = false
# Suppress logger output for asset requests. # Suppress logger output for asset requests.
config.assets.quiet = true config.assets.quiet = true

View file

@ -1,5 +1,5 @@
Rails.application.routes.draw do Rails.application.routes.draw do
resources :users, only: [:new, :create] resources :users
get 'login', to: 'sessions#new' get 'login', to: 'sessions#new'
get 'logout', to: 'sessions#delete' get 'logout', to: 'sessions#delete'
post 'login', to: 'sessions#create' post 'login', to: 'sessions#create'
@ -9,4 +9,5 @@ Rails.application.routes.draw do
get 'recover_password/:id/:recovery_password', to: 'users#recover_password_form' get 'recover_password/:id/:recovery_password', to: 'users#recover_password_form'
post 'recover_password', to: 'users#recover_password' post 'recover_password', to: 'users#recover_password'
resources :books resources :books
resources :authors
end end