Add action Staffs::AccountsController#index
This commit is contained in:
parent
ef89b1f381
commit
11da69af35
7 changed files with 119 additions and 0 deletions
12
app/controllers/staffs/accounts_controller.rb
Normal file
12
app/controllers/staffs/accounts_controller.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Staffs::AccountsController < ApplicationController
|
||||
# GET /staff/accounts
|
||||
def index
|
||||
authorize %i[staff account]
|
||||
@accounts = policy_scope(
|
||||
Account,
|
||||
policy_scope_class: Staff::AccountPolicy::Scope,
|
||||
)
|
||||
end
|
||||
end
|
15
app/policies/staff/account_policy.rb
Normal file
15
app/policies/staff/account_policy.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Staff::AccountPolicy < ApplicationPolicy
|
||||
def index?
|
||||
account&.superuser?
|
||||
end
|
||||
|
||||
class Scope < Scope
|
||||
def resolve
|
||||
return scope.all if account&.superuser?
|
||||
|
||||
scope.none
|
||||
end
|
||||
end
|
||||
end
|
49
app/views/staffs/accounts/index.html.erb
Normal file
49
app/views/staffs/accounts/index.html.erb
Normal file
|
@ -0,0 +1,49 @@
|
|||
<div class="container">
|
||||
<nav aria-label="breadcrumb">
|
||||
<ol class="breadcrumb">
|
||||
<li class="breadcrumb-item">
|
||||
<%= link_to translate(:staff_services), staff_root_path %>
|
||||
</li>
|
||||
|
||||
<li class="breadcrumb-item active" aria-current="page">
|
||||
<%= Account.model_name.human count: 0 %>
|
||||
</li>
|
||||
</ol>
|
||||
</nav>
|
||||
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">
|
||||
<%= Account.human_attribute_name :id %>
|
||||
</th>
|
||||
<th scope="col">
|
||||
<%= Account.human_attribute_name :nickname %>
|
||||
</th>
|
||||
<th scope="col">
|
||||
<%= Account.human_attribute_name :public_name %>
|
||||
</th>
|
||||
<th scope="col"></th>
|
||||
<th scope="col"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<% @accounts.each do |account| %>
|
||||
<tr>
|
||||
<td scope="row"><%= account.id %></td>
|
||||
<td><%= account.nickname %></td>
|
||||
<td><%= account.public_name %></td>
|
||||
<td>
|
||||
<% if account.superuser? %>
|
||||
<span class="badge badge-primary">
|
||||
<%= translate :superuser %>
|
||||
</span>
|
||||
<% end %>
|
||||
</td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
|
@ -8,6 +8,12 @@
|
|||
</nav>
|
||||
|
||||
<ul>
|
||||
<% if policy(%i[staff account]).index? %>
|
||||
<li>
|
||||
<%= link_to Account.model_name.human(count: 0), staff_accounts_path %>
|
||||
</li>
|
||||
<% end %>
|
||||
|
||||
<% if policy(%i[staff person]).index? %>
|
||||
<li>
|
||||
<%= link_to Person.model_name.human(count: 0), staff_people_path %>
|
||||
|
|
|
@ -48,6 +48,8 @@ Rails.application.routes.draw do
|
|||
|
||||
get '/sidekiq', to: redirect('/', status: 307), as: :forbidden_sidekiq
|
||||
|
||||
resources :accounts, only: :index
|
||||
|
||||
resources :people, only: %i[index show] do
|
||||
resources :person_comments,
|
||||
path: 'comments',
|
||||
|
|
7
spec/policies/staff/account_policy_spec.rb
Normal file
7
spec/policies/staff/account_policy_spec.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Staff::AccountPolicy do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
28
spec/requests/staff/accounts/index_spec.rb
Normal file
28
spec/requests/staff/accounts/index_spec.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'GET /staff/accounts' do
|
||||
before do
|
||||
sign_in current_account.user if current_account&.user
|
||||
|
||||
create :guest_account
|
||||
create :usual_account
|
||||
create :personal_account
|
||||
create :superuser_account
|
||||
|
||||
get '/staff/accounts'
|
||||
end
|
||||
|
||||
for_account_types nil, :guest, :usual do
|
||||
specify do
|
||||
expect(response).to have_http_status :forbidden
|
||||
end
|
||||
end
|
||||
|
||||
for_account_types :superuser do
|
||||
specify do
|
||||
expect(response).to have_http_status :ok
|
||||
end
|
||||
end
|
||||
end
|
Reference in a new issue