-
Notifications
You must be signed in to change notification settings - Fork 0
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
murny
wants to merge
3
commits into
add-authentication-via-devise
Choose a base branch
from
add-admin-crud-ui
base: add-authentication-via-devise
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add admin crud UI #626
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
class Admin::AdminsController < Comfy::Admin::BaseController | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' %> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
6
app/views/comfy/admin/cms/partials/_navigation_inner.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
So brought in Kaminari just because we use it in Jupiter (no strong opinions otherwise)