From ed2018cf05e513616ea89af842420359014ca7ba Mon Sep 17 00:00:00 2001 From: murny <1930474+murny@users.noreply.github.com> Date: Thu, 15 Aug 2024 12:56:04 -0600 Subject: [PATCH] Fix linter issues --- app/controllers/admin/admins_controller.rb | 14 ++--- config/locales/en.yml | 11 ++++ .../admin/admins_controller_test.rb | 53 ++++++++++++------- 3 files changed, 52 insertions(+), 26 deletions(-) diff --git a/app/controllers/admin/admins_controller.rb b/app/controllers/admin/admins_controller.rb index f2d40af4..fe304167 100644 --- a/app/controllers/admin/admins_controller.rb +++ b/app/controllers/admin/admins_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Admin::AdminsController < Comfy::Admin::BaseController before_action :build_admin, only: [:new, :create] before_action :load_admin, only: [:show, :edit, :update, :destroy] @@ -20,25 +22,25 @@ def edit def create @admin.save! - flash[:success] = "Admin created" + flash[:success] = t(".admin_created_successfully") redirect_to action: :show, id: @admin rescue ActiveRecord::RecordInvalid - flash.now[:danger] = "Failed to create Admin" + flash.now[:danger] = t(".admin_creation_failed") render action: :new end def update @admin.update!(admin_params) - flash[:success] = "Admin updated" + flash[:success] = t(".admin_updated_successfully") redirect_to action: :show, id: @admin rescue ActiveRecord::RecordInvalid - flash.now[:danger] = "Failed to update Admin" + flash.now[:danger] = t(".admin_update_failed") render action: :edit end def destroy @admin.destroy - flash[:success] = "Admin deleted" + flash[:success] = t(".admin_deleted_successfully") redirect_to action: :index end @@ -51,7 +53,7 @@ def build_admin def load_admin @admin = Admin.find(params[:id]) rescue ActiveRecord::RecordNotFound - flash[:danger] = "Admin not found" + flash[:danger] = t("admin.admins.admin_not_found") redirect_to action: :index end diff --git a/config/locales/en.yml b/config/locales/en.yml index decc5a85..ec832d2c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -31,3 +31,14 @@ en: hello: "Hello world" + admin: + admins: + admin_not_found: "Admin not found" + create: + admin_created_successfully: "Admin created successfully" + admin_creation_failed: "Admin creation failed" + update: + admin_updated_successfully: "Admin updated successfully" + admin_update_failed: "Admin update failed" + destroy: + admin_deleted_successfully: "Admin deleted successfully" diff --git a/test/controllers/admin/admins_controller_test.rb b/test/controllers/admin/admins_controller_test.rb index 119c590a..9d97b63c 100644 --- a/test/controllers/admin/admins_controller_test.rb +++ b/test/controllers/admin/admins_controller_test.rb @@ -1,7 +1,8 @@ -require_relative '../../test_helper' +# frozen_string_literal: true -class Admin::AdminsControllerTest < ActionDispatch::IntegrationTest +require_relative "../../test_helper" +class Admin::AdminsControllerTest < ActionDispatch::IntegrationTest setup do @admin = admins(:default) end @@ -11,17 +12,18 @@ class Admin::AdminsControllerTest < ActionDispatch::IntegrationTest # Move this to test_helper.rb def r(verb, path, options = {}) headers = options[:headers] || {} - headers['HTTP_AUTHORIZATION'] = + headers["HTTP_AUTHORIZATION"] = ActionController::HttpAuthentication::Basic.encode_credentials( ComfortableMexicanSofa::AccessControl::AdminAuthentication.username, ComfortableMexicanSofa::AccessControl::AdminAuthentication.password ) - options.merge!(headers: headers) + options[:headers] = headers send(verb, path, options) end def test_get_index r :get, admin_admins_path + assert_response :success assert assigns(:admins) assert_template :index @@ -29,20 +31,23 @@ def test_get_index def test_get_show r :get, admin_admin_path(@admin) + assert_response :success assert assigns(:admin) assert_template :show end def test_get_show_failure - r :get, admin_admin_path('invalid') + r :get, admin_admin_path("invalid") + assert_response :redirect assert_redirected_to action: :index - assert_equal 'Admin not found', flash[:danger] + assert_equal "Admin not found", flash[:danger] end def test_get_new r :get, new_admin_admin_path + assert_response :success assert assigns(:admin) assert_template :new @@ -51,6 +56,7 @@ def test_get_new def test_get_edit r :get, edit_admin_admin_path(@admin) + assert_response :success assert assigns(:admin) assert_template :edit @@ -58,54 +64,61 @@ def test_get_edit end def test_creation - assert_difference 'Admin.count' do + assert_difference "Admin.count" do r :post, admin_admins_path, params: {admin: { - name: 'test name', + name: "test name" }} admin = Admin.last + assert_response :redirect assert_redirected_to action: :show, id: admin - assert_equal 'Admin created', flash[:success] + assert_equal "Admin created", flash[:success] end end def test_creation_failure - assert_no_difference 'Admin.count' do - r :post, admin_admins_path, params: {admin: { }} + assert_no_difference "Admin.count" do + r :post, admin_admins_path, params: {admin: {}} + assert_response :success assert_template :new - assert_equal 'Failed to create Admin', flash[:danger] + assert_equal "Failed to create Admin", flash[:danger] end end def test_update r :put, admin_admin_path(@admin), params: {admin: { - name: 'Updated' + name: "Updated" }} + assert_response :redirect assert_redirected_to action: :show, id: @admin - assert_equal 'Admin updated', flash[:success] + assert_equal "Admin updated", flash[:success] @admin.reload - assert_equal 'Updated', @admin.name + + assert_equal "Updated", @admin.name end def test_update_failure r :put, admin_admin_path(@admin), params: {admin: { - name: '' + name: "" }} + assert_response :success assert_template :edit - assert_equal 'Failed to update Admin', flash[:danger] + assert_equal "Failed to update Admin", flash[:danger] @admin.reload - refute_equal '', @admin.name + + assert_not_equal "", @admin.name end def test_destroy - assert_difference 'Admin.count', -1 do + assert_difference "Admin.count", -1 do r :delete, admin_admin_path(@admin) + assert_response :redirect assert_redirected_to action: :index - assert_equal 'Admin deleted', flash[:success] + assert_equal "Admin deleted", flash[:success] end end end