Skip to content

Commit

Permalink
ADD: добавляет вьюшки и контроллеры для Server
Browse files Browse the repository at this point in the history
  • Loading branch information
owlscatcher committed Feb 19, 2024
1 parent ac9785b commit 0daa10b
Show file tree
Hide file tree
Showing 12 changed files with 246 additions and 5 deletions.
25 changes: 24 additions & 1 deletion app/controllers/admin/server_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
class Admin::ServerController < ApplicationController
def index; end
include ServerConcern

load_and_authorize_resource
# before_action :set_server, only: [:show, :edit, :update, :destroy]

def index
server_index
end

def new
@server = Server.new
end

def create
server_create
end

def update
server_update
end

def destroy
server_destroy
end
end
55 changes: 55 additions & 0 deletions app/controllers/concerns/server_concern.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module ServerConcern
extend ActiveSupport::Concern

included do # rubocop:disable Metrics/BlockLength
def server_index
@query = Server.ransack(params[:q])
@pagy, @servers = pagy(@query.result.order(:name))
end

def server_create
@server = Server.new(server_params)

if @server.save
redirect_back(fallback_location: root_path)
else
render(:new, status: :unprocessable_entity)
end
end

def server_update
if @server.update(server_params)
redirect_back(fallback_location: root_path)
else
render(:edit, status: :unprocessable_entity)
end
end

def server_destroy
assigned_models_count = Channel.where(server_id: params[:id]).count

if assigned_models_count.zero?
@server.destroy
else
flash[:error] = t('message.admin.server.delete.error')
end
redirect_to(admin_server_index_path)
end

private

def set_server
@server = Server.find(params[:id])
end

def server_params
params.require(:server).permit(
:name,
:ip_address,
:inventory_id,
:service_id,
:room_id,
)
end
end
end
11 changes: 10 additions & 1 deletion app/models/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ class Server < ApplicationRecord

has_many :channels

validates :ip_adress,
validates :name, :service, :room, presence: true
validates :ip_address,
presence: true,
uniqueness: true,
format: { with: Resolv::IPv4::Regex }

def self.ransackable_attributes(_auth_object = nil)
['created_at', 'id', 'inventory_id', 'ip_address', 'name', 'room_id', 'service_id', 'updated_at']
end

def self.ransackable_associations(_auth_object = nil)
['channels', 'room', 'service']
end
end
1 change: 1 addition & 0 deletions app/views/admin/server/create.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= turbo_stream.dispatch_event "modalClose" %>
3 changes: 3 additions & 0 deletions app/views/admin/server/edit.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= turbo_stream.update "modal-title", t('admin.server.edit') %>
<%= turbo_stream.update "modal-body", partial: "shared/admin/form/server",
locals: { path: admin_server_path(@server), server: @server } %>
125 changes: 125 additions & 0 deletions app/views/admin/server/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<% provide :page_title, "Службы" %>
<%= render 'shared/admin/navbar/admin', selected: "asrc" %>
<%= render 'shared/flash_alert' %>
<div class="row mx-0">
<div class="col-2 ps-0 my-4">
<%= render 'shared/admin/navbar/asrc_navbar', selected: "server" %>
<div class="accordion mb-3" id="accordionFilter">
<div class="accordion-item">
<h2 class="accordion-header" id="headingFilter">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseFilter" aria-expanded="true" aria-controls="collapseFilter">
<%= t('filtration')%>
</button>
</h2>
<div id="collapseFilter" class="accordion-collapse collapse shadow" aria-labelledby="headingFilter" data-bs-parent="#accordionFilter">
<%= search_form_for(@query, url: admin_server_index_path, method: :get, class: "rounded accordion-body") do |f| %>
<div id="for-clear">
<%= f.label :name, class: "mb-1" %>
<%= f.search_field :name_cont, class:"form-control mb-2", placeholder:"Название" %>
<%= f.label :ip_address, class: "mb-1" %>
<%= f.search_field :ip_address_cont, class:"form-control mb-2", placeholder:"192.168.1.101" %>
<%= f.label :inventory_id, class: "mb-1"%>
<%= f.search_field :inventory_id_cont, class:"form-control mb-2", placeholder:"4321" %>

<%= f.label :service_id, class: "mb-1" %>
<%= f.collection_select(:service_id_eq, Service.all,
:id, :name, {:include_blank => t('combobox_blank')}, {:class =>'form-select mb-2'}) %>
<%= f.label :room_id, class: "mb-1" %>
<%= f.collection_select(:room_id_eq, Service.all,
:id, :name, {:include_blank => t('combobox_blank')}, {:class =>'form-select mb-2'}) %>
</div>
<%= f.submit t('b_accept'), class: 'btn btn-primary w-100 my-2'%>
<div id="b_clear" onclick="formClear()" class="btn btn-secondary w-100">
<%= t("b_clear")%>
</div>
<% end %>
</div>
</div>
</div>
<%= render 'shared/modal_button_add', path: new_admin_server_path, classes: "btn btn-primary w-100", text: t("b_add") %>
</div>
<div class="col-10 my-4 p-0">
<div class="shadow rounded px-2">
<div class="table-responsive">
<table class="table table-hover">
<%= render 'shared/ui/table/caption/all_items',
count: Server.count %>
<thead>
<tr>
<th scope="col"><%= t('activerecord.attributes.server.id') %></th>
<th scope="col"><%= t('activerecord.attributes.server.name') %></th>
<th scope="col"><%= t('activerecord.attributes.server.ip_address') %></th>
<th scope="col"><%= t('activerecord.attributes.server.inventory_id') %></th>
<th scope="col"><%= t('activerecord.attributes.server.service') %></th>
<th scope="col"><%= t('activerecord.attributes.server.room') %></th>
<%= render 'shared/ui/table/header/action' %>
</tr>
</thead>
<tbody>
<% @servers.each do |server| %>
<tr class="align-middle">
<td><%= server.id %></td>
<td><%= server.name %></td>

<% if server.ip_address.nil? %>
<td><%= "——" %></td>
<% else %>
<td><%= server.ip_address %></td>
<% end %>

<% if server.inventory_id.nil? %>
<td><%= "——" %></td>
<% else %>
<td><%= server.inventory_id %></td>
<% end %>

<% if server.service.nil? %>
<td><%= "——" %></td>
<% else %>
<td><%= server.service.name %></td>
<% end %>

<% if server.room.nil? %>
<td><%= "——" %></td>
<% else %>
<td><%= server.room.name %></td>
<% end %>

<%= render 'shared/ui/table/cell/action',
edit_path: edit_admin_server_path(server.id),
destroy_path: admin_server_path(server.id) %>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="row my-5">
<div class="col-2">
</div>
<div class="col-10">
<div class="row">
<div class="col"></div>
<div class="col">
<%= pagination @pagy %>
</div>
<div class="col"></div>
</div>
</div>
</div>
<script>
function formClear(){
let form = document.getElementById('for-clear')
let inputs = Array.from(form.getElementsByTagName("input"))
let selects = Array.from(form.getElementsByTagName("select"))
inputs.forEach((input) =>{
input.value = null
})
selects.forEach((select) =>{
select.value = ""
})
}
</script>
3 changes: 3 additions & 0 deletions app/views/admin/server/new.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= turbo_stream.update "modal-title", t('admin.server.new') %>
<%= turbo_stream.update "modal-body", partial: "shared/admin/form/server",
locals: { path: admin_server_index_path, server: @server } %>
1 change: 1 addition & 0 deletions app/views/admin/server/update.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= turbo_stream.dispatch_event "modalClose" %>
12 changes: 12 additions & 0 deletions app/views/shared/admin/form/_server.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%= simple_form_for server, as: :server, url: path,
class: "form-group row" do |f| %>
<div class="row">
<%= f.input :name, placeholder: 'Название службы', input_html: {:tabindex => 1}%>
<%= f.input :ip_address, input_html: { :tabindex => 2}, include_blank: false %>
<%= f.input :inventory_id, input_html: { :tabindex => 3 }, include_blank: false %>
<%= f.association :service, input_html: { :tabindex => 4}, include_blank: false %>
<%= f.association :room, input_html: { :tabindex => 5}, include_blank: false %>
</div>

<%= render 'shared/form/actions', f:f %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/shared/admin/navbar/_asrc_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<% end %>
</div>
<div class="text-left">
<%= link_to admin_channel_index_path, class: "btn btn-sm w-100 #{ 'btn-primary' if selected == 'server' } text-start" do %>
<%= link_to admin_server_index_path, class: "btn btn-sm w-100 #{ 'btn-primary' if selected == 'server' } text-start" do %>
<i class="bi bi-door-open"></i><span> <%= 'Серверы' %></span>
<% end %>
</div>
Expand Down
9 changes: 9 additions & 0 deletions config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,15 @@ ru:
organization_id: Организация
building: Здание
building_id: Здание
server:
id: ID
name: Название
ip_address: IPv4
inventory_id: № инв.
service: Служба
service_id: Служба
room: Расположение
room_id: Расположение
user:
tabel_id: Таб. №
first_name: Имя
Expand Down
4 changes: 2 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0daa10b

Please sign in to comment.