-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
283 additions
and
0 deletions.
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,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 |
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
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 |