Skip to content

Commit

Permalink
Merge branch 'feature/device-cp-improving' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
owlscatcher committed Feb 19, 2024
2 parents f80c7f4 + d7107f0 commit db93389
Show file tree
Hide file tree
Showing 67 changed files with 1,382 additions and 688 deletions.
26 changes: 26 additions & 0 deletions app/controllers/admin/channel_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Admin::ChannelController < ApplicationController
include ChannelConcern
load_and_authorize_resource
before_action :set_channel, only: [:show, :edit, :update, :destroy]

def index
channel_index
end

def new
@channel = Channel.new
end

def create
channel_create
end

def update
channel_update
end

def destroy
authorize!(:destroy, :channel_admin)
channel_destroy
end
end
2 changes: 0 additions & 2 deletions app/controllers/admin/control_point_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ def update

def destroy
assigned_models_count =
Room.where(control_point_id: params[:id]).count +
Device.where(control_point_id: params[:id]).count +
Channel.where(control_point_id: params[:id]).count

if assigned_models_count.zero?
Expand Down
26 changes: 26 additions & 0 deletions app/controllers/admin/server_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Admin::ServerController < ApplicationController
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
64 changes: 64 additions & 0 deletions app/controllers/concerns/channel_concern.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module ChannelConcern
extend ActiveSupport::Concern

included do # rubocop:disable Metrics/BlockLength
def channel_index
@query = Channel.ransack(params[:q])
@pagy, @channels = pagy(@query.result.order(:id))
end

def channel_create
@channel = Channel.new(channel_params)

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

def channel_update
if @channel.update(channel_params)
redirect_back(fallback_location: root_path)
else
render(:new, status: :unprocessable_entity)
end
end

def channel_destroy
assigned_histories = History.where(channel_id: @channel.id).count

if assigned_histories.zero?
if @channel.destroy
flash[:success] = t('message.admin.channel.delete.success')
else
flash[:error] = t('message.admin.channel.delete.error')
end
else
flash[:error] = t('message.admin.channel.delete.error')
end
redirect_to(admin_channel_index_path)
end

private

def set_channel
@channel = Channel.find(params[:id])
end

def channel_params
params.require(:channel).permit(
:channel_id,
:server_id,
:control_point_id,
:location_description,
:self_background,
:pre_emergency_limit,
:emergency_limit,
:consumption,
:conversion_coefficient,
:service_id,
)
end
end
end
4 changes: 2 additions & 2 deletions app/controllers/concerns/device_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def device_update(device)
def device_destroy(device, path_success, path_failure)
assigned_inspections_count = Inspection.where(device_id: device.id).count

if assigned_inspections_count.zero? && device.control_point_id?
if assigned_inspections_count.zero? && device.control_point.nil?
device.destroy
redirect_to(path_success)
else
Expand Down Expand Up @@ -87,7 +87,7 @@ def device_params
:year_of_production,
:year_of_commissioning,
:supplementary_kit_id,
:control_point_id,
:control_point_ids,
:service_id)
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/organization_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def organization_params
:zip_code,
:phone,
:fax,
:email
:email,
)
end
end
Expand Down
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
4 changes: 2 additions & 2 deletions app/javascript/Components/Armstrong/Armstrong.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function Armstrong() {
case 'normal':
return <i className="bi bi-circle-fill text-success" />;
case 'warning':
return <i className="bi bi-circle-warning" />;
return <i className="bi bi-circle-fill text-warning" />;
case 'danger':
return <i className="bi bi-circle-fill text-danger" />;
default:
Expand Down Expand Up @@ -96,7 +96,7 @@ export default function Armstrong() {
return (
<div className="row mx-0">
<div className="shadow rounded mb-4 pt-3">
<Filter className="ps-0 pe-0 mb-3" filter={filter} onFilterChange={handleFilterChange} />
<Filter className="ps-0 pe-0" filter={filter} onFilterChange={handleFilterChange} />
<Table data={filteredData} openModal={openModal} />
{isModalOpen && (
<ModalComponent
Expand Down
8 changes: 7 additions & 1 deletion app/javascript/Components/Armstrong/Filter/Filter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import PropTypes from 'prop-types';
export default function Filter({ filter, onFilterChange }) {
return (
<div>
<input className="form-control rounded" type="text" placeholder="Поиск..." value={filter} onChange={onFilterChange} />
<input
className="form-control rounded mb-2"
type="text"
placeholder="Поиск..."
value={filter}
onChange={onFilterChange}
/>
</div>
);
}
Expand Down
8 changes: 7 additions & 1 deletion app/javascript/Components/Armstrong/Table/TableBody.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ export default function TableBody({ columns, data, openModal }) {
let tData = row[accessor] ? row[accessor] : '——';
if (accessor === 'chart') {
tData = (
<Button type="button" onClick={openModal} className="btn btn-light" data-id={row.id} data-pointname={row.name}>
<Button
type="button"
onClick={openModal}
className="btn btn-light btn-sm"
data-id={row.id}
data-pointname={row.name}
>
<Icon.GraphUp />
</Button>
);
Expand Down
13 changes: 13 additions & 0 deletions app/models/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,17 @@ class Channel < ApplicationRecord

has_many :history
belongs_to :control_point

channel_id_msg = 'должен быть в диапозоне от 1 до 48'

validates :channel_id, presence: true, numericality: { in: 1..48, message: channel_id_msg }

def self.ransackable_associations(_auth_object = nil)
['control_point', 'history', 'server', 'service']
end

def self.ransackable_attributes(_auth_object = nil)
['channel_id', 'consumption', 'control_point_id', 'conversion_coefficient', 'created_at', 'emergency_limit', 'event_count',
'event_datetime', 'event_error_count', 'event_impulse_value', 'event_not_system_value', 'event_system_value', 'id', 'is_online', 'is_special_control', 'location_description', 'pre_emergency_limit', 'self_background', 'server_id', 'service_id', 'state', 'updated_at']
end
end
2 changes: 1 addition & 1 deletion app/models/control_point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class ControlPoint < ApplicationRecord
belongs_to :service

has_one :channel
has_one :device
belongs_to :device, optional: true

validates :name, presence: true

Expand Down
4 changes: 3 additions & 1 deletion app/models/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ class Device < ApplicationRecord

current_year = Date.today.year
year_error_msg = "должен быть больше 1900 и меньше или равен #{current_year}"
inspection_interval_msg = 'должен быть от 0.1 (раз в месяц) до 10.0 (раз в 10 лет)'

belongs_to :device_model
belongs_to :device_reg_group
belongs_to :supplementary_kit, optional: true
belongs_to :room, optional: true
belongs_to :service
belongs_to :control_point, optional: true

has_many :control_point
has_many :inspections

validates :inventory_id, numericality: { less_than_or_equal_to: 2147483647 }, uniqueness: true, allow_nil: true
validates :serial_id, :tabel_id, presence: true, uniqueness: true
validates :year_of_commissioning, :year_of_production, numericality: { in: 1900..current_year, message: year_error_msg }, allow_nil: true
validates :year_of_production, presence: true
validates :inspection_interval, presence: true, numericality: { in: 0.1..10.0, message: inspection_interval_msg }

STATUS = {
verified: 'verified',
Expand Down
4 changes: 2 additions & 2 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Organization < ApplicationRecord

validates :name, presence: true

def self.ransackable_attributes(auth_object = nil)
["created_at", "email", "fax", "full_address", "id", "name", "phone", "updated_at", "zip_code"]
def self.ransackable_attributes(_auth_object = nil)
['created_at', 'email', 'fax', 'full_address', 'id', 'name', 'phone', 'updated_at', 'zip_code']
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
Loading

0 comments on commit db93389

Please sign in to comment.