1
0
Fork 0

Add action TelegramBot#show

This commit is contained in:
Alex Kotov 2018-12-07 06:34:57 +05:00
parent 9fa103ab75
commit 8b365a955d
No known key found for this signature in database
GPG Key ID: 4E831250F47DE154
6 changed files with 82 additions and 2 deletions

View File

@ -1,9 +1,22 @@
# frozen_string_literal: true
class TelegramBotsController < ApplicationController
before_action :set_telegram_bot, except: :index
# GET /telegram_bots
def index
authorize :telegram_bot
@telegram_bots = policy_scope(TelegramBot)
end
# GET /telegram_bots/:id
def show
authorize @telegram_bot
end
private
def set_telegram_bot
@telegram_bot = TelegramBot.find params[:id]
end
end

View File

@ -5,6 +5,10 @@ class TelegramBotPolicy < ApplicationPolicy
context.account&.is_superuser?
end
def show?
context.account&.is_superuser?
end
class Scope < Scope
def resolve
return scope.all if context.account&.is_superuser?

View File

@ -25,7 +25,14 @@
<td><%= telegram_bot.secret %></td>
<td><%= telegram_bot.api_token %></td>
<td><%= truncate telegram_bot.username, length: 20 %></td>
<td></td>
<td>
<% if policy(telegram_bot).show? %>
<%= link_to telegram_bot_path(telegram_bot),
role: :button, class: 'btn btn-light btn-sm' do %>
<i class="far fa-eye"></i>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</tbody>

View File

@ -0,0 +1,12 @@
<div class="container">
<dl>
<dt><%= TelegramBot.human_attribute_name :secret %></dt>
<dd><%= @telegram_bot.secret %></dd>
<dt><%= TelegramBot.human_attribute_name :api_token %></dt>
<dd><%= @telegram_bot.api_token %></dd>
<dt><%= TelegramBot.human_attribute_name :username %></dt>
<dd><%= truncate @telegram_bot.username %></dd>
</dl>
</div>

View File

@ -20,7 +20,7 @@ Rails.application.routes.draw do
only: %i[index create]
end
resources :telegram_bots, only: :index
resources :telegram_bots, only: %i[index show]
resources :telegram_bot_updates, only: :create
end

View File

@ -0,0 +1,44 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'GET /telegram_bots/:id' do
let!(:telegram_bot) { create :telegram_bot }
before do
sign_in current_account.user if current_account&.user
get "/telegram_bots/#{telegram_bot.id}"
end
context 'when no account is authenticated' do
let(:current_account) { nil }
specify do
expect(response).to have_http_status :unauthorized
end
end
context 'when guest account is authenticated' do
let(:current_account) { create :guest_account }
specify do
expect(response).to have_http_status :unauthorized
end
end
context 'when usual account is authenticated' do
let(:current_account) { create :account_with_user }
specify do
expect(response).to have_http_status :unauthorized
end
end
context 'when superuser account is authenticated' do
let(:current_account) { create :superuser_account }
specify do
expect(response).to have_http_status :ok
end
end
end