Skip to content

Commit

Permalink
Manage status in views
Browse files Browse the repository at this point in the history
  • Loading branch information
B-Rass committed Oct 23, 2024
1 parent e6902ab commit ece6c9a
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/controllers/rooms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,6 @@ def set_room

# Never trust parameters from the scary internet, only allow the white list through.
def room_params
params.require(:room).permit(:name, :description, :display_on_home_page, :position, :site_id)
params.require(:room).permit(:name, :description, :display_on_home_page, :position, :status, :site_id)
end
end
2 changes: 1 addition & 1 deletion app/models/room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Room < ApplicationRecord
has_many :frames, through: :bays, dependent: :restrict_with_error
has_many :materials, through: :frames, dependent: :restrict_with_error

enum :status, [:active, :passive, :planned]
enum :status, { :active => 0, :passive => 1, :planned => 2 }, default: :active, validate: true

scope :sorted, -> { order(:site_id, :position, :name) }
scope :not_empty, -> { joins(:servers) }
Expand Down
2 changes: 1 addition & 1 deletion app/processors/rooms_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class RoomsProcessor < ApplicationProcessor
include Sortable
SORTABLE_FIELDS = %w[name position sites.name islets_count display_on_home_page].freeze
SORTABLE_FIELDS = %w[name position sites.name islets_count display_on_home_page status].freeze

map :q do |q:|
raw.where(Room.arel_table[:name].matches("%#{q}%"))
Expand Down
11 changes: 7 additions & 4 deletions app/views/pages/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
<div class="img-fluid m-0 py-5 px-2 rounded"
style="background: url(<%= asset_path('_78A0587-nb.jpg') %>) no-repeat center; background-size: cover;">
<div class="container-fluid mt-4">
<% Site.joins(:rooms).where('display_on_home_page = ?', true).distinct.order(:position).each do |site| %>
<% Site.joins(:rooms).where('display_on_home_page = ? and status != ?', true, Room.statuses[:passive]).distinct.order(:position).each do |site| %>
<div class="container-fluid mb-4 py-2 bg-body-tertiary bg-opacity-75 rounded border">
<div class="text-center fs-4 mb-2"><%= site %></div>
<div class="d-flex justify-content-between flex-wrap">
<% rooms = site.rooms.where(display_on_home_page: true).order(:position) %>
<% rooms = site.rooms.where(display_on_home_page: true).not_passive.order(:position) %>
<% rooms.each do |room| %>
<div class="flex-fill border border-primary-subtle m-2 p-2 rounded">
<div class="text-start mb-1 fs-5"><%= link_to room.name, room %></div>
<div class="<%= class_names("flex-fill border border-primary-subtle m-2 p-2 rounded",
"border-secondary-subtle opacity-50": room.planned?)%>">
<div class="text-start mb-1 fs-5">
<%= link_to room.name, room, class: class_names("link-secondary": room.planned?) %>
</div>
<ul>
<li><%= "#{Frame.model_name.human} : #{room.frames.count}" %></li>
<% Category.all.each do |cat| %>
Expand Down
7 changes: 7 additions & 0 deletions app/views/rooms/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
<%= f.label :display_on_home_page, class: "form-check-label" %>
</fieldset>
</div>

<fieldset class="col-12 mt-4">
<%= f.label :status, class: "form-label" %>
<%= f.select :status, RoomDecorator::status_for_options,
{ },
class: "form-select" %>
</fieldset>
<% end %>

<div class="col-12 py-4 mt-4 text-end sticky-bottom bg-body-tertiary border-top">
Expand Down
4 changes: 4 additions & 0 deletions app/views/rooms/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
<input type="checkbox" <%= room.display_on_home_page ? "checked" : "" %> disabled>
<% end %>
<% table.with_column(Room.human_attribute_name(:status), sort_by: :status) do |room| %>
<%= Room.human_attribute_name("status.#{room.status}") %>
<% end %>
<% table.with_column(style: "min-width: 140px; width: 140px") do |room| %>
<div class="btn-group btn-group-sm" role="group" aria-label="...">
<%= render partial: "rooms/export_button", locals: { room: room } %>
Expand Down
3 changes: 3 additions & 0 deletions app/views/rooms/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
<dd class="mb-0 pb-2 ps-3">
<span><%= t("boolean.#{@room.display_on_home_page}") %></span>
</dd>

<dt class="pb-2"><%= Room.human_attribute_name(:status) %></dt>
<dd class="mb-0 pb-2 ps-3"><%= Room.human_attribute_name("status.#{@room.status}") %></dd>
</dl>
<% end %>
</div>
Expand Down
15 changes: 14 additions & 1 deletion db/migrate/20241023101945_add_status_attribute_to_room.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
# frozen_string_literal: true

class MigrationRoom < ActiveRecord::Base
self.table_name = :rooms
end

class AddStatusAttributeToRoom < ActiveRecord::Migration[7.2]
def change
add_column :rooms, :status, :string
add_column :rooms, :status, :integer

up_only do
MigrationRoom.find_each do |room|
status = room.display_on_home_page ? 0 : 1
room.update(status: status)
end

change_column_null(:rooms, :status, false)
end
end
end
2 changes: 1 addition & 1 deletion db/schema.rb

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

9 changes: 9 additions & 0 deletions spec/models/room_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
it { is_expected.to have_many(:materials).through(:frames) }
end

describe "validations" do
it do
is_expected.to define_enum_for(:status) # rubocop:disable RSpec/ImplicitSubject
.with_default(:active)
.validating
.with_values([:active, :passive, :planned])
end
end

describe "#to_s" do
it { expect(room.to_s).to eq room.name }
end
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/rooms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,34 @@ one:
description: Room n°1
site_id: 1
display_on_home_page: true
status: :active

two:
id: 2
name: S2
description: Room n°2
site_id: 1
display_on_home_page: true
status: :active

three:
id: 3
name: S3
description: Room n°3
site_id: 2
display_on_home_page: false
status: :active

four:
id: 4
name: S4
description: Room n°4
site_id: 2
status: :active

five:
id: 5
name: S5
description: Room n°5
site_id: 1
status: :active

0 comments on commit ece6c9a

Please sign in to comment.