Skip to content

Commit

Permalink
Fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
murny committed Aug 24, 2024
1 parent d1ab64b commit 8562438
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
14 changes: 8 additions & 6 deletions app/controllers/admin/admins_controller.rb
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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

Expand All @@ -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

Expand Down
11 changes: 11 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
53 changes: 33 additions & 20 deletions test/controllers/admin/admins_controller_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,38 +12,42 @@ 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
end

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
Expand All @@ -51,61 +56,69 @@ def test_get_new

def test_get_edit
r :get, edit_admin_admin_path(@admin)

assert_response :success
assert assigns(:admin)
assert_template :edit
assert_select "form[action='/admin/admins/#{@admin.id}']"
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

0 comments on commit 8562438

Please sign in to comment.