Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add admin crud UI #626

Draft
wants to merge 3 commits into
base: add-authentication-via-devise
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scaffold uses .page helper and assumes pagination. Comfy mexican suggests either Will Paginate or Kaminari gems:

https://github.com/comfy/comfortable-mexican-sofa?tab=readme-ov-file#dependencies

Pagination is handled by kaminari or will_paginate. Please add one of those to your Gemfile.

So brought in Kaminari just because we use it in Jupiter (no strong opinions otherwise)


# 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
63 changes: 63 additions & 0 deletions app/controllers/admin/admins_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

class Admin::AdminsController < Comfy::Admin::BaseController
Copy link
Collaborator Author

@murny murny Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly all this code below is from the scaffold generator: https://github.com/comfy/comfortable-mexican-sofa/wiki/HowTo:-Reusing-Admin-Area

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] = t(".admin_created_successfully")
redirect_to action: :show, id: @admin
rescue ActiveRecord::RecordInvalid
flash.now[:danger] = t(".admin_creation_failed")
render action: :new
end

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

def destroy
@admin.destroy
flash[:success] = t(".admin_deleted_successfully")
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] = t("admin.admins.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>

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"
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
102 changes: 102 additions & 0 deletions test/controllers/admin/admins_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# frozen_string_literal: true

require_relative "../../test_helper"

class Admin::AdminsControllerTest < ActionDispatch::IntegrationTest
setup do
@admin = admins(:admin)
sign_in @admin
end

def test_get_index
get admin_admins_path

assert_response :success
end

def test_get_show
get admin_admin_path(@admin)

assert_response :success
end

def test_get_show_failure
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
get new_admin_admin_path

assert_response :success
assert_select "form[action='/admin/admins']"
end

def test_get_edit
get edit_admin_admin_path(@admin)

assert_response :success
assert_select "form[action='/admin/admins/#{@admin.id}']"
end

def test_creation
assert_difference "Admin.count" do
post admin_admins_path, params: {admin: {
email: "[email protected]",
password: "password"
}}
admin = Admin.last

assert_response :redirect
assert_redirected_to action: :show, id: admin
assert_equal "Admin created successfully", flash[:success]
end
end

def test_creation_failure
assert_no_difference "Admin.count" do
post admin_admins_path, params: {admin: {}}

assert_response :success
assert_equal "Admin creation failed", flash[:danger]
end
end

def test_update
put admin_admin_path(@admin), params: {admin: {
email: "[email protected]"
}}

assert_response :redirect
assert_redirected_to action: :show, id: @admin
assert_equal "Admin updated successfully", flash[:success]
@admin.reload

assert_equal "[email protected]", @admin.email
end

def test_update_failure
put admin_admin_path(@admin), params: {admin: {
email: ""
}}

assert_response :success
assert_equal "Admin update failed", flash[:danger]
@admin.reload

assert_not_equal "", @admin.email
end

def test_destroy
assert_difference "Admin.count", -1 do
delete admin_admin_path(@admin)

assert_response :redirect
assert_redirected_to action: :index
assert_equal "Admin deleted successfully", flash[:success]
end
end
end
Loading