diff --git a/Gemfile b/Gemfile index 3d49e52..2b78fd1 100644 --- a/Gemfile +++ b/Gemfile @@ -24,6 +24,8 @@ gem 'bcrypt', '~> 3.1.7' gem 'materialize-sass', '~> 1.0.0' +gem 'jquery-rails' + # Use Active Storage variant # gem 'image_processing', '~> 1.2' diff --git a/Gemfile.lock b/Gemfile.lock index 671f766..7c63543 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -91,6 +91,10 @@ GEM concurrent-ruby (~> 1.0) jbuilder (2.11.2) 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) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) @@ -241,6 +245,7 @@ DEPENDENCIES byebug capybara (>= 3.26) jbuilder (~> 2.7) + jquery-rails listen (~> 3.3) materialize-sass (~> 1.0.0) puma (~> 5.0) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 90af2ed..4e12a38 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -10,10 +10,18 @@ class ApplicationController < ActionController::Base !current_user.nil? end + protected + def notices_from_errors(record) messages = record.errors.messages.map do |attribute, messages| messages.map { |message| "#{attribute} #{message}".capitalize } end messages.flatten end + + def ensure_admin + unless current_user&.admin? + redirect_to '/welcome', notice: 'You are not allowed to perform this action' + end + end end diff --git a/app/controllers/authors_controller.rb b/app/controllers/authors_controller.rb index f96718c..abd8ecb 100644 --- a/app/controllers/authors_controller.rb +++ b/app/controllers/authors_controller.rb @@ -1,5 +1,28 @@ class AuthorsController < ApplicationController + before_action :ensure_admin + before_action :set_author, only: [:edit, :update] + def index @authors = Author.all 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 diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index 3ef6625..499d455 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -25,12 +25,6 @@ class BooksController < ApplicationController 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 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index e25d5f1..ec02862 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,10 @@ class UsersController < ApplicationController + before_action :ensure_admin, only: [:destroy] + + def index + @users = User.all + end + def new @user = User.new end @@ -45,4 +51,9 @@ class UsersController < ApplicationController redirect_to '/welcome', notice: 'Recovery link expired or invalid' end end + + def destroy + User.destroy(params[:id]) + redirect_to '/users' + end end diff --git a/app/javascript/packs/application.js b/app/javascript/packs/application.js index a5843e7..2d92af0 100644 --- a/app/javascript/packs/application.js +++ b/app/javascript/packs/application.js @@ -4,6 +4,8 @@ // that code so it'll be compiled. //= require materialize +//= require jquery +//= require jquery_ujs import Rails from "@rails/ujs" import Turbolinks from "turbolinks" diff --git a/app/views/authors/edit.html.erb b/app/views/authors/edit.html.erb new file mode 100644 index 0000000..57de572 --- /dev/null +++ b/app/views/authors/edit.html.erb @@ -0,0 +1,10 @@ +