Skip to content

Commit

Permalink
Add Admins CRUD to Admin area
Browse files Browse the repository at this point in the history
  • Loading branch information
murny committed Aug 24, 2024
1 parent d3df3a8 commit d1ab64b
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ gem "stimulus-rails"

gem "rollbar"

gem "kaminari"

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem "jbuilder", "~> 2.12"

Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ GEM
jsbundling-rails (1.3.1)
railties (>= 6.0.0)
json (2.7.2)
kaminari (1.2.2)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.2)
kaminari-activerecord (= 1.2.2)
kaminari-core (= 1.2.2)
kaminari-actionview (1.2.2)
actionview
kaminari-core (= 1.2.2)
kaminari-activerecord (1.2.2)
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
kramdown (2.4.0)
rexml
language_server-protocol (3.17.0.3)
Expand Down Expand Up @@ -403,6 +415,7 @@ DEPENDENCIES
image_processing (~> 1.13)
jbuilder (~> 2.12)
jsbundling-rails
kaminari
listen (>= 3.0.5, < 3.10)
mysql2 (~> 0.5.6)
puma (~> 6.4)
Expand Down
61 changes: 61 additions & 0 deletions app/controllers/admin/admins_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class Admin::AdminsController < Comfy::Admin::BaseController
before_action :build_admin, only: [:new, :create]
before_action :load_admin, only: [:show, :edit, :update, :destroy]

def index
@admins = Admin.page(params[:page])
end

def show
render
end

def new
render
end

def edit
render
end

def create
@admin.save!
flash[:success] = "Admin created"
redirect_to action: :show, id: @admin
rescue ActiveRecord::RecordInvalid
flash.now[:danger] = "Failed to create Admin"
render action: :new
end

def update
@admin.update!(admin_params)
flash[:success] = "Admin updated"
redirect_to action: :show, id: @admin
rescue ActiveRecord::RecordInvalid
flash.now[:danger] = "Failed to update Admin"
render action: :edit
end

def destroy
@admin.destroy
flash[:success] = "Admin deleted"
redirect_to action: :index
end

protected

def build_admin
@admin = Admin.new(admin_params)
end

def load_admin
@admin = Admin.find(params[:id])
rescue ActiveRecord::RecordNotFound
flash[:danger] = "Admin not found"
redirect_to action: :index
end

def admin_params
params.fetch(:admin, {}).permit(:email, :password)
end
end
7 changes: 7 additions & 0 deletions app/views/admin/admins/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= form.text_field :email %>
<%= form.password_field :password %>

<%= form.form_actions do %>
<%= form.submit class: "btn btn-primary ml-sm-1" %>
<%= link_to 'Cancel', admin_admins_path, class: "btn btn-link" %>
<% end %>
7 changes: 7 additions & 0 deletions app/views/admin/admins/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="page-header">
<h2>Edit Admin</h2>
</div>

<%= comfy_form_with model: @admin, url: [:admin, @admin] do |form| %>
<%= render form %>
<% end %>
32 changes: 32 additions & 0 deletions app/views/admin/admins/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<div class="page-header">
<%= link_to 'New Admin', new_admin_admin_path, class: 'btn btn-secondary float-right' %>
<h2>Admins</h2>
</div>

<%= paginate @admins, theme: 'comfy' %>


<ul class="list">
<% @admins.each do |admin| %>
<li>
<div class="row">
<div class="col-md-8 item">
<div class="item-content">
<div class="item-title">
<%= link_to admin.email, admin_admin_path(admin) %>
</div>
</div>
</div>
<div class="col-md-4 d-flex align-items-center justify-content-md-end">
<div class="btn-group btn-group-sm">
<%= link_to 'Edit', edit_admin_admin_path(admin), class: 'btn btn-outline-secondary' %>
<%= link_to 'Delete', admin_admin_path(admin), method: :delete, data: {confirm: 'Are you sure?'}, class: 'btn btn-danger' %>
</div>
</div>
</div>
</li>
<% end %>
</ul>

<%= paginate @admins, theme: 'comfy' %>

7 changes: 7 additions & 0 deletions app/views/admin/admins/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="page-header">
<h2>New Admin</h2>
</div>

<%= comfy_form_with model: @admin, url: [:admin, @admin] do |form| %>
<%= render form %>
<% end %>
33 changes: 33 additions & 0 deletions app/views/admin/admins/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<div class="page-header">
<h2>Admin</h2>
</div>

<dl>
<dt>Email</dt>
<dd><%= @admin.email %></dd>

<dt>Created at</dt>
<dd><%= @admin.created_at %></dd>

<dt>Updated at</dt>
<dd><%= @admin.updated_at %></dd>

<dt>Sign in count</dt>
<dd><%= @admin.sign_in_count %></dd>

<dt>Current sign in at</dt>
<dd><%= @admin.current_sign_in_at %></dd>

<dt>Last sign in at</dt>
<dd><%= @admin.last_sign_in_at %></dd>

<dt>Current sign in ip</dt>
<dd><%= @admin.current_sign_in_ip %></dd>

<dt>Last sign in ip</dt>
<dd><%= @admin.last_sign_in_ip %></dd>
</dl>

<%= link_to 'Edit', edit_admin_admin_path(@admin), class: 'btn btn-secondary' %>
<%= link_to 'Delete', admin_admin_path(@admin), method: :delete, data: {confirm: 'Are you sure?'}, class: 'btn btn-danger' %>
<%= link_to 'Back', admin_admins_path, class: 'btn btn-secondary' %>
6 changes: 6 additions & 0 deletions app/views/comfy/admin/cms/partials/_navigation_inner.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

<li class="nav-item">
<%= active_link_to 'Admins', admin_admins_path, class: 'nav-link' %>
<%= link_to 'Log out', destroy_admin_session_path, method: :delete, class: 'nav-link' %>
</li>

4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

Rails.application.routes.draw do
namespace :admin do
resources :admins
end

root controller: "comfy/cms/content", cms_path: "", action: "show"

devise_for :admins
Expand Down
111 changes: 111 additions & 0 deletions test/controllers/admin/admins_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
require_relative '../../test_helper'

class Admin::AdminsControllerTest < ActionDispatch::IntegrationTest

setup do
@admin = admins(:default)
end

# Vanilla CMS has BasicAuth, so we need to send that with each request.
# Change this to fit your app's authentication strategy.
# Move this to test_helper.rb
def r(verb, path, options = {})
headers = options[:headers] || {}
headers['HTTP_AUTHORIZATION'] =
ActionController::HttpAuthentication::Basic.encode_credentials(
ComfortableMexicanSofa::AccessControl::AdminAuthentication.username,
ComfortableMexicanSofa::AccessControl::AdminAuthentication.password
)
options.merge!(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')
assert_response :redirect
assert_redirected_to action: :index
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
assert_select "form[action='/admin/admins']"
end

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
r :post, admin_admins_path, params: {admin: {
name: 'test name',
}}
admin = Admin.last
assert_response :redirect
assert_redirected_to action: :show, id: admin
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_response :success
assert_template :new
assert_equal 'Failed to create Admin', flash[:danger]
end
end

def test_update
r :put, admin_admin_path(@admin), params: {admin: {
name: 'Updated'
}}
assert_response :redirect
assert_redirected_to action: :show, id: @admin
assert_equal 'Admin updated', flash[:success]
@admin.reload
assert_equal 'Updated', @admin.name
end

def test_update_failure
r :put, admin_admin_path(@admin), params: {admin: {
name: ''
}}
assert_response :success
assert_template :edit
assert_equal 'Failed to update Admin', flash[:danger]
@admin.reload
refute_equal '', @admin.name
end

def test_destroy
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]
end
end
end

0 comments on commit d1ab64b

Please sign in to comment.