Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 0 additions & 59 deletions app/controllers/channel_zones_controller.rb

This file was deleted.

139 changes: 30 additions & 109 deletions app/controllers/zones_controller.rb
Original file line number Diff line number Diff line change
@@ -1,119 +1,61 @@
class ZonesController < ApplicationController
# Only run these for nested routes (when codeplug_id is present)
before_action :set_codeplug, if: :nested_route?
before_action :authorize_codeplug, if: :nested_route?
before_action :set_zone, only: [ :show, :edit, :update, :destroy, :update_positions ]
before_action :authorize_zone, only: [ :show, :edit, :update, :destroy, :update_positions ], unless: :nested_route?
before_action :authorize_zone_view, only: [ :show ]
before_action :authorize_zone_edit, only: [ :edit, :update, :destroy, :update_positions ]

def index
if nested_route?
# Nested route: show zones for specific codeplug
@zones = @codeplug.zones.order(:name)
else
# Standalone route: show zones available to current user
@zones = Zone.available_to_user(current_user).order(:name)
end
@zones = Zone.available_to_user(current_user).order(:name)
end

def show
if nested_route?
# Nested route: Get channels for codeplug
channel_ids_in_zone = @zone.channel_zones.pluck(:channel_id)

@available_channels = @codeplug.channels
.where.not(id: channel_ids_in_zone)
.order(:long_name)
else
# Standalone route: Show zone systems (placeholder for future functionality)
@zone_systems = @zone.zone_systems.includes(:system).order(:position)
end
@zone_systems = @zone.zone_systems.includes(:system).order(:position)
end

def new
if nested_route?
@zone = @codeplug.zones.new
else
@zone = Zone.new
end
@zone = Zone.new
end

def edit
# Edit zone form
end

def create
if nested_route?
@zone = @codeplug.zones.new(zone_params)
@zone.user = current_user

if @zone.save
redirect_to codeplug_zone_path(@codeplug, @zone), notice: "Zone was successfully created."
else
render :new, status: :unprocessable_entity
end
else
@zone = Zone.new(zone_params)
@zone.user = current_user
@zone = Zone.new(zone_params)
@zone.user = current_user

if @zone.save
redirect_to zone_path(@zone), notice: "Zone was successfully created."
else
render :new, status: :unprocessable_entity
end
if @zone.save
redirect_to zone_path(@zone), notice: "Zone was successfully created."
else
render :new, status: :unprocessable_entity
end
end

def update
if @zone.update(zone_params)
if nested_route?
redirect_to codeplug_zone_path(@codeplug, @zone), notice: "Zone was successfully updated."
else
redirect_to zone_path(@zone), notice: "Zone was successfully updated."
end
redirect_to zone_path(@zone), notice: "Zone was successfully updated."
else
render :edit, status: :unprocessable_entity
end
end

def destroy
@zone.destroy!
if nested_route?
redirect_to codeplug_zones_path(@codeplug), notice: "Zone was successfully deleted."
else
redirect_to zones_path, notice: "Zone was successfully deleted."
end
redirect_to zones_path, notice: "Zone was successfully deleted."
end

def update_positions
positions_params = params.permit(positions: [ :id, :position ])

ActiveRecord::Base.transaction do
if nested_route?
# Update channel positions for codeplug zones
# First pass: Set temporary positions to avoid uniqueness conflicts
positions_params[:positions].each_with_index do |position_data, index|
channel_zone = @zone.channel_zones.find(position_data[:id])
channel_zone.update_column(:position, 1000 + index)
end

# Second pass: Set actual positions
positions_params[:positions].each do |position_data|
channel_zone = @zone.channel_zones.find(position_data[:id])
channel_zone.update!(position: position_data[:position])
end
else
# Update zone_system positions for standalone zones
# First pass: Set temporary positions to avoid uniqueness conflicts
positions_params[:positions].each_with_index do |position_data, index|
zone_system = @zone.zone_systems.find(position_data[:id])
zone_system.update_column(:position, 1000 + index)
end

# Second pass: Set actual positions
positions_params[:positions].each do |position_data|
zone_system = @zone.zone_systems.find(position_data[:id])
zone_system.update!(position: position_data[:position])
end
# First pass: Set temporary positions to avoid uniqueness conflicts
positions_params[:positions].each_with_index do |position_data, index|
zone_system = @zone.zone_systems.find(position_data[:id])
zone_system.update_column(:position, 1000 + index)
end

# Second pass: Set actual positions
positions_params[:positions].each do |position_data|
zone_system = @zone.zone_systems.find(position_data[:id])
zone_system.update!(position: position_data[:position])
end
end

Expand All @@ -122,43 +64,22 @@ def update_positions

private

def set_codeplug
@codeplug = Codeplug.find(params[:codeplug_id])
end

def set_zone
if nested_route?
@zone = @codeplug.zones.find(params[:id])
else
@zone = Zone.find(params[:id])
end
@zone = Zone.find(params[:id])
end

def authorize_codeplug
unless @codeplug.user == current_user
redirect_to codeplugs_path, alert: "You don't have permission to access this codeplug."
def authorize_zone_view
unless @zone.viewable_by?(current_user)
head :forbidden
end
end

def authorize_zone
# For standalone routes, check if user can view/edit the zone
action = action_name.to_sym

if [ :show ].include?(action)
unless @zone.viewable_by?(current_user)
head :forbidden
end
elsif [ :edit, :update, :destroy, :update_positions ].include?(action)
unless @zone.editable_by?(current_user)
head :forbidden
end
def authorize_zone_edit
unless @zone.editable_by?(current_user)
head :forbidden
end
end

def nested_route?
params[:codeplug_id].present?
end

def zone_params
params.require(:zone).permit(:name, :long_name, :short_name, :public)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/codeplug.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class Codeplug < ApplicationRecord
# Associations
belongs_to :user
has_many :zones, dependent: :destroy
has_many :channels, dependent: :destroy
has_many :codeplug_zones, dependent: :destroy
has_many :zones, through: :codeplug_zones

# Validations
validates :name, presence: true
Expand Down
1 change: 0 additions & 1 deletion app/models/zone.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class Zone < ApplicationRecord
# Associations
belongs_to :user
belongs_to :codeplug, optional: true
has_many :channel_zones, dependent: :destroy
has_many :channels, through: :channel_zones
has_many :codeplug_zones, dependent: :destroy
Expand Down
33 changes: 1 addition & 32 deletions app/views/codeplugs/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -146,38 +146,7 @@
</div>
</div>

<%# Legacy Zones Section (codeplug-owned zones) %>
<div class="col-md-6 mb-4">
<div class="card h-100">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center mb-3">
<h5 class="card-title mb-0">Zones</h5>
<%= link_to "Manage Zones", codeplug_zones_path(@codeplug), class: "btn btn-sm btn-primary" %>
</div>

<% if @codeplug.zones.any? %>
<p class="text-muted mb-2"><strong><%= pluralize(@codeplug.zones.count, "zone") %></strong></p>
<ul class="list-unstyled">
<% @codeplug.zones.limit(5).each do |zone| %>
<li class="mb-1">
<i class="bi bi-folder"></i> <%= zone.long_name || zone.name %>
<span class="badge bg-secondary"><%= zone.channels.count %> channels</span>
</li>
<% end %>
<% if @codeplug.zones.count > 5 %>
<li class="text-muted mt-2">
<small>+ <%= @codeplug.zones.count - 5 %> more zones</small>
</li>
<% end %>
</ul>
<% else %>
<p class="text-muted">No zones configured yet.</p>
<% end %>
</div>
</div>
</div>

<div class="col-md-6 mb-4">
<div class="col-md-12 mb-4">
<div class="card h-100" id="channels-section">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center mb-3">
Expand Down
22 changes: 8 additions & 14 deletions app/views/zones/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= form_with model: @codeplug ? [ @codeplug, zone ] : zone do |f| %>
<%= form_with model: zone do |f| %>
<% if zone.errors.any? %>
<div class="alert alert-danger">
<h5><%= pluralize(zone.errors.count, "error") %> prohibited this zone from being saved:</h5>
Expand Down Expand Up @@ -28,22 +28,16 @@
<div class="form-text">Abbreviated name (for radios with limited display)</div>
</div>

<% unless @codeplug %>
<div class="mb-3">
<div class="form-check">
<%= f.check_box :public, class: "form-check-input" %>
<%= f.label :public, "Make this zone public", class: "form-check-label" %>
<div class="form-text">Public zones can be viewed by all users. Private zones are only visible to you.</div>
</div>
<div class="mb-3">
<div class="form-check">
<%= f.check_box :public, class: "form-check-input" %>
<%= f.label :public, "Make this zone public", class: "form-check-label" %>
<div class="form-text">Public zones can be viewed by all users. Private zones are only visible to you.</div>
</div>
<% end %>
</div>

<div class="d-flex gap-2 mt-4">
<%= f.submit class: "btn btn-primary" %>
<% if @codeplug %>
<%= link_to "Cancel", zone.persisted? ? codeplug_zone_path(@codeplug, zone) : codeplug_zones_path(@codeplug), class: "btn btn-secondary" %>
<% else %>
<%= link_to "Cancel", zone.persisted? ? zone_path(zone) : zones_path, class: "btn btn-secondary" %>
<% end %>
<%= link_to "Cancel", zone.persisted? ? zone_path(zone) : zones_path, class: "btn btn-secondary" %>
</div>
<% end %>
6 changes: 1 addition & 5 deletions app/views/zones/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
<div class="container mt-4">
<h1>Edit Zone - <%= @zone.name %></h1>

<% if @codeplug %>
<%= link_to "← Back to Zone", codeplug_zone_path(@codeplug, @zone), class: "btn btn-secondary mb-3" %>
<% else %>
<%= link_to "← Back to Zone", zone_path(@zone), class: "btn btn-secondary mb-3" %>
<% end %>
<%= link_to "← Back to Zone", zone_path(@zone), class: "btn btn-secondary mb-3" %>

<div class="card">
<div class="card-body">
Expand Down
Loading