Skip to content

Commit

Permalink
FIX: Исправляет баги и тесты
Browse files Browse the repository at this point in the history
  • Loading branch information
owlscatcher committed Feb 15, 2024
1 parent 9bf6b47 commit e75c3d7
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 19 deletions.
9 changes: 7 additions & 2 deletions app/controllers/admin/channel_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ def create
channel_create
end

def update; end
def update
channel_update
end

def destroy; end
def destroy
authorize!(:destroy, :channel_admin)
channel_destroy
end
end
27 changes: 25 additions & 2 deletions app/controllers/concerns/channel_concern.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ChannelConcern
extend ActiveSupport::Concern

included do
included do # rubocop:disable Metrics/BlockLength
def channel_index
@query = Channel.ransack(params[:q])
@pagy, @channels = pagy(@query.result.order(:id))
Expand All @@ -17,10 +17,33 @@ def channel_create
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(channel_params[:id])
@channel = Channel.find(params[:id])
end

def channel_params
Expand Down
2 changes: 1 addition & 1 deletion 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
4 changes: 4 additions & 0 deletions app/models/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ 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
Expand Down
19 changes: 17 additions & 2 deletions app/views/admin/channel/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,23 @@
<div id="collapseFilter" class="accordion-collapse collapse shadow" aria-labelledby="headingFilter" data-bs-parent="#accordionFilter">
<%= search_form_for(@query, url: admin_channel_index_path, method: :get, class: "rounded accordion-body") do |f| %>
<div id="for-clear">
<%= f.label :id, class: "mb-2" %>
<%= f.search_field :id_cont, class:"form-control", placeholder:"ID" %>
<%= f.label :id, class: "mb-1" %>
<%= f.search_field :id_eq, class:"form-control mb-2", placeholder:"ID" %>

<%= f.label :channel_id, class: "mb-1" %>
<%= f.search_field :channel_id_eq, class:"form-control mb-2", placeholder:"25" %>

<%= f.label :server_id, class: "mb-1" %>
<%= f.collection_select(:server_id_eq, Server.all,
:id, :name, {:include_blank => t('combobox_blank')}, {:class=>'form-select mb-2'}) %>

<%= f.label :control_point_id, class: "mb-1" %>
<%= f.collection_select(:control_point_id_eq, ControlPoint.all,
:id, :name, {:include_blank => t('combobox_blank')}, {:class=>'form-select mb-2'}) %>

<%= 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'}) %>
</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">
Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/control_point/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<%= f.label :name, class: "mb-2" %>
<%= f.search_field :name_cont, class:"form-control", placeholder:"Название" %>
<%= f.label :room_id, class:"mb-1" %>
<%= f.collection_select(:room_id_eq, Room.all,
:id, :name, {:include_blank => t('combobox_blank')}, {:class=>'form-select form-select'}) %>
<%= f.collection_select(:room_id_eq, Room.all,
:id, :name, {:include_blank => t('combobox_blank')}, {:class=>'form-select form-select'}) %>
</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">
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/admin/form/_channel.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@

<% if current_user.admin? %>
<div class="row">
<%= f.association :service, input_html: { :tabindex => 10} %>
<%= f.association :service, input_html: { :tabindex => 10 }, include_blank: false %>
</div>
<% end %>

Expand Down
9 changes: 1 addition & 8 deletions app/views/shared/form/_device.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,7 @@
<% else %>
<%= f.input :service_id, label: false, input_html: {hidden: true, value: current_user.service_id} %>
<% end %>
<div class="col">
<%= f.label :control_point %>
<div>
<% ControlPoint.where(device_id: device.id).each do |e| %>
<%= content_tag(:span, e.name, class: "btn btn-light btn-sm") %>
<% end %>
</div>
</div>

</div>

<%= render 'shared/form/actions', f:f %>
Expand Down
2 changes: 1 addition & 1 deletion test/factories/channels.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
self_background { 1.5 }
pre_emergency_limit { 1.5 }
emergency_limit { 1.5 }
consumptiom { 1.5 }
consumption { 1.5 }
conversion_coefficient { 1.5 }
event_system_value { 1.5 }
event_not_system_value { 1.5 }
Expand Down
1 change: 1 addition & 0 deletions test/factories/control_points.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
channel { nil }
device { nil }
service { association :service }
device_id { association :device }
end
end

0 comments on commit e75c3d7

Please sign in to comment.