Add action Staffs::Accounts::ContactsController#index
This commit is contained in:
parent
decd0da0f3
commit
273ce969f9
10 changed files with 148 additions and 1 deletions
23
app/controllers/staffs/accounts/contacts_controller.rb
Normal file
23
app/controllers/staffs/accounts/contacts_controller.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Staffs::Accounts::ContactsController < ApplicationController
|
||||
include PaginalController
|
||||
|
||||
before_action :set_account
|
||||
|
||||
# GET /staff/accounts/:account_nickname/contacts
|
||||
def index
|
||||
authorize [:staff, Account, Contact]
|
||||
|
||||
@contacts = policy_scope(
|
||||
@account.contact_list.contacts,
|
||||
policy_scope_class: Staff::Account::ContactPolicy::Scope,
|
||||
).page(active_page)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_account
|
||||
@account = Account.find_by! nickname: params[:account_nickname]
|
||||
end
|
||||
end
|
19
app/policies/staff/account/contact_policy.rb
Normal file
19
app/policies/staff/account/contact_policy.rb
Normal file
|
@ -0,0 +1,19 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Staff::Account::ContactPolicy < ApplicationPolicy
|
||||
def index?
|
||||
return false if restricted?
|
||||
|
||||
account&.superuser?
|
||||
end
|
||||
|
||||
class Scope < Scope
|
||||
def resolve
|
||||
return scope.none if restricted?
|
||||
|
||||
return scope.all if account&.superuser?
|
||||
|
||||
scope.none
|
||||
end
|
||||
end
|
||||
end
|
14
app/views/staffs/accounts/_nav_tabs.html.erb
Normal file
14
app/views/staffs/accounts/_nav_tabs.html.erb
Normal file
|
@ -0,0 +1,14 @@
|
|||
<div class="mb-3">
|
||||
<%= nav_tabs(
|
||||
:account,
|
||||
tab,
|
||||
overview: [
|
||||
policy([:staff, account]).show?,
|
||||
[:staff, account],
|
||||
],
|
||||
contacts: [
|
||||
policy([:staff, Account, Contact]).index?,
|
||||
staff_account_contacts_path(account),
|
||||
],
|
||||
) %>
|
||||
</div>
|
28
app/views/staffs/accounts/contacts/_table.html.erb
Normal file
28
app/views/staffs/accounts/contacts/_table.html.erb
Normal file
|
@ -0,0 +1,28 @@
|
|||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">
|
||||
<%= Contact.human_attribute_name :contact_network %>
|
||||
</th>
|
||||
<th scope="col">
|
||||
<%= Contact.human_attribute_name :value %>
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<% contacts.each do |contact| %>
|
||||
<tr>
|
||||
<td><%= contact.contact_network.name %></td>
|
||||
<td>
|
||||
<% if contact.link.nil? %>
|
||||
<%= truncate contact.value %>
|
||||
<% else %>
|
||||
<%= link_to truncate(contact.value), contact.link, target: :_blank %>
|
||||
<i class="fas fa-external-link-alt fa-xs text-muted"></i>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
14
app/views/staffs/accounts/contacts/index.html.erb
Normal file
14
app/views/staffs/accounts/contacts/index.html.erb
Normal file
|
@ -0,0 +1,14 @@
|
|||
<div class="container">
|
||||
<%= nav_breadcrumb(
|
||||
[translate(:staff_services), staff_root_path],
|
||||
[Account.model_name.human(count: 0), staff_accounts_path],
|
||||
[@account.nickname, [:staff, @account]],
|
||||
Contact.model_name.human(count: 0),
|
||||
) %>
|
||||
|
||||
<%= render partial: 'staffs/accounts/nav_tabs',
|
||||
locals: { account: @account, tab: :contacts } %>
|
||||
|
||||
<%= render partial: 'table', locals: { contacts: @contacts } %>
|
||||
<%= pagination @contacts %>
|
||||
</div>
|
|
@ -5,6 +5,9 @@
|
|||
@account.nickname,
|
||||
) %>
|
||||
|
||||
<%= render partial: 'nav_tabs',
|
||||
locals: { account: @account, tab: :overview } %>
|
||||
|
||||
<% if @account.superuser? %>
|
||||
<div>
|
||||
<span class="badge badge-primary">
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
en:
|
||||
nav_tabs:
|
||||
account:
|
||||
overview: Overview
|
||||
contacts: Contacts
|
||||
person:
|
||||
overview: Overview
|
||||
person_comments: Comments
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
ru:
|
||||
nav_tabs:
|
||||
account:
|
||||
overview: Обзор
|
||||
contacts: Контакты
|
||||
person:
|
||||
overview: Обзор
|
||||
person_comments: Комментарии
|
||||
|
|
|
@ -57,7 +57,11 @@ Rails.application.routes.draw do
|
|||
|
||||
get '/sidekiq', to: redirect('/', status: 307), as: :forbidden_sidekiq
|
||||
|
||||
resources :accounts, param: :nickname, only: %i[index show]
|
||||
resources :accounts, param: :nickname, only: %i[index show] do
|
||||
resources :contacts,
|
||||
controller: 'accounts/contacts',
|
||||
only: :index
|
||||
end
|
||||
|
||||
resources :contact_networks, only: :index
|
||||
|
||||
|
|
36
spec/requests/staff/accounts/contacts/index_spec.rb
Normal file
36
spec/requests/staff/accounts/contacts/index_spec.rb
Normal file
|
@ -0,0 +1,36 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'GET /staff/accounts/:account_nickname/contacts' do
|
||||
let(:current_account) { create :superuser_account }
|
||||
|
||||
let(:some_account) { create :initial_account }
|
||||
|
||||
let :contacts_count do
|
||||
[0, 1, rand(2..4), rand(5..10), rand(20..40)].sample
|
||||
end
|
||||
|
||||
before do
|
||||
sign_in current_account.user if current_account&.user
|
||||
|
||||
create_list :some_contact, contacts_count,
|
||||
contact_list: some_account.contact_list
|
||||
|
||||
get "/staff/accounts/#{some_account.nickname}/contacts"
|
||||
end
|
||||
|
||||
it_behaves_like 'paginal controller', :contacts_count
|
||||
|
||||
for_account_types nil, :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