diff --git a/app/controllers/admin/server_controller.rb b/app/controllers/admin/server_controller.rb index add1cdb..d30362c 100644 --- a/app/controllers/admin/server_controller.rb +++ b/app/controllers/admin/server_controller.rb @@ -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 diff --git a/app/controllers/concerns/server_concern.rb b/app/controllers/concerns/server_concern.rb new file mode 100644 index 0000000..ad9f783 --- /dev/null +++ b/app/controllers/concerns/server_concern.rb @@ -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 diff --git a/app/models/server.rb b/app/models/server.rb index 73e12cf..092bce0 100644 --- a/app/models/server.rb +++ b/app/models/server.rb @@ -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 diff --git a/app/views/admin/server/create.turbo_stream.erb b/app/views/admin/server/create.turbo_stream.erb new file mode 100644 index 0000000..3ba4d15 --- /dev/null +++ b/app/views/admin/server/create.turbo_stream.erb @@ -0,0 +1 @@ +<%= turbo_stream.dispatch_event "modalClose" %> diff --git a/app/views/admin/server/edit.turbo_stream.erb b/app/views/admin/server/edit.turbo_stream.erb new file mode 100644 index 0000000..3874fd9 --- /dev/null +++ b/app/views/admin/server/edit.turbo_stream.erb @@ -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 } %> diff --git a/app/views/admin/server/index.html.erb b/app/views/admin/server/index.html.erb index e69de29..16d3282 100644 --- a/app/views/admin/server/index.html.erb +++ b/app/views/admin/server/index.html.erb @@ -0,0 +1,125 @@ +<% provide :page_title, "Службы" %> +<%= render 'shared/admin/navbar/admin', selected: "asrc" %> +<%= render 'shared/flash_alert' %> +
+
+ <%= render 'shared/admin/navbar/asrc_navbar', selected: "server" %> +
+
+

+ +

+
+ <%= search_form_for(@query, url: admin_server_index_path, method: :get, class: "rounded accordion-body") do |f| %> +
+ <%= 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'}) %> +
+ <%= f.submit t('b_accept'), class: 'btn btn-primary w-100 my-2'%> +
+ <%= t("b_clear")%> +
+ <% end %> +
+
+
+ <%= render 'shared/modal_button_add', path: new_admin_server_path, classes: "btn btn-primary w-100", text: t("b_add") %> +
+
+
+
+ + <%= render 'shared/ui/table/caption/all_items', + count: Server.count %> + + + + + + + + + <%= render 'shared/ui/table/header/action' %> + + + + <% @servers.each do |server| %> + + + + + <% if server.ip_address.nil? %> + + <% else %> + + <% end %> + + <% if server.inventory_id.nil? %> + + <% else %> + + <% end %> + + <% if server.service.nil? %> + + <% else %> + + <% end %> + + <% if server.room.nil? %> + + <% else %> + + <% end %> + + <%= render 'shared/ui/table/cell/action', + edit_path: edit_admin_server_path(server.id), + destroy_path: admin_server_path(server.id) %> + + <% end %> + +
<%= t('activerecord.attributes.server.id') %><%= t('activerecord.attributes.server.name') %><%= t('activerecord.attributes.server.ip_address') %><%= t('activerecord.attributes.server.inventory_id') %><%= t('activerecord.attributes.server.service') %><%= t('activerecord.attributes.server.room') %>
<%= server.id %><%= server.name %><%= "——" %><%= server.ip_address %><%= "——" %><%= server.inventory_id %><%= "——" %><%= server.service.name %><%= "——" %><%= server.room.name %>
+
+
+
+
+ +
+
+
+
+
+
+
+ <%= pagination @pagy %> +
+
+
+
+
+ diff --git a/app/views/admin/server/new.turbo_stream.erb b/app/views/admin/server/new.turbo_stream.erb new file mode 100644 index 0000000..c4ce362 --- /dev/null +++ b/app/views/admin/server/new.turbo_stream.erb @@ -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 } %> diff --git a/app/views/admin/server/update.turbo_stream.erb b/app/views/admin/server/update.turbo_stream.erb new file mode 100644 index 0000000..3ba4d15 --- /dev/null +++ b/app/views/admin/server/update.turbo_stream.erb @@ -0,0 +1 @@ +<%= turbo_stream.dispatch_event "modalClose" %> diff --git a/app/views/shared/admin/form/_server.html.erb b/app/views/shared/admin/form/_server.html.erb new file mode 100644 index 0000000..69d3663 --- /dev/null +++ b/app/views/shared/admin/form/_server.html.erb @@ -0,0 +1,12 @@ +<%= simple_form_for server, as: :server, url: path, + class: "form-group row" do |f| %> +
+ <%= 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 %> +
+ + <%= render 'shared/form/actions', f:f %> +<% end %> diff --git a/app/views/shared/admin/navbar/_asrc_navbar.html.erb b/app/views/shared/admin/navbar/_asrc_navbar.html.erb index bb98cc0..60a4ff7 100644 --- a/app/views/shared/admin/navbar/_asrc_navbar.html.erb +++ b/app/views/shared/admin/navbar/_asrc_navbar.html.erb @@ -6,7 +6,7 @@ <% end %>
- <%= 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 %> <%= 'Серверы' %> <% end %>
diff --git a/config/locales/ru.yml b/config/locales/ru.yml index ff698fd..0b0bf7a 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -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: Имя diff --git a/db/schema.rb b/db/schema.rb index f193735..c294e57 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_02_14_111919) do +ActiveRecord::Schema[7.0].define(version: 2024_02_19_101812) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -225,7 +225,7 @@ create_table "servers", force: :cascade do |t| t.string "name" - t.string "ip_adress" + t.string "ip_address" t.integer "inventory_id" t.bigint "service_id", null: false t.bigint "room_id", null: false