diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index 0766a1914..d9374702e 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -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 diff --git a/app/models/room.rb b/app/models/room.rb index e3c10341b..e5b569f77 100644 --- a/app/models/room.rb +++ b/app/models/room.rb @@ -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) } diff --git a/app/processors/rooms_processor.rb b/app/processors/rooms_processor.rb index ce1b7f285..a23a2ed4c 100644 --- a/app/processors/rooms_processor.rb +++ b/app/processors/rooms_processor.rb @@ -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}%")) diff --git a/app/views/pages/index.html.erb b/app/views/pages/index.html.erb index e1f170fb4..f4017a39e 100644 --- a/app/views/pages/index.html.erb +++ b/app/views/pages/index.html.erb @@ -4,14 +4,17 @@
- <% 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| %>
<%= site %>
- <% 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| %> -
-
<%= link_to room.name, room %>
+
"> +
+ <%= link_to room.name, room, class: class_names("link-secondary": room.planned?) %> +
  • <%= "#{Frame.model_name.human} : #{room.frames.count}" %>
  • <% Category.all.each do |cat| %> diff --git a/app/views/rooms/_form.html.erb b/app/views/rooms/_form.html.erb index b380c9e5e..ec2b5d33a 100644 --- a/app/views/rooms/_form.html.erb +++ b/app/views/rooms/_form.html.erb @@ -33,6 +33,13 @@ <%= f.label :display_on_home_page, class: "form-check-label" %>
+ +
+ <%= f.label :status, class: "form-label" %> + <%= f.select :status, RoomDecorator::status_for_options, + { }, + class: "form-select" %> +
<% end %>
diff --git a/app/views/rooms/index.html.erb b/app/views/rooms/index.html.erb index d7c717ab6..3cda0433f 100644 --- a/app/views/rooms/index.html.erb +++ b/app/views/rooms/index.html.erb @@ -69,6 +69,10 @@ 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| %>
<%= render partial: "rooms/export_button", locals: { room: room } %> diff --git a/app/views/rooms/show.html.erb b/app/views/rooms/show.html.erb index c8c1c5b34..785b5317e 100644 --- a/app/views/rooms/show.html.erb +++ b/app/views/rooms/show.html.erb @@ -50,6 +50,9 @@
<%= t("boolean.#{@room.display_on_home_page}") %>
+ +
<%= Room.human_attribute_name(:status) %>
+
<%= Room.human_attribute_name("status.#{@room.status}") %>
<% end %>
diff --git a/db/migrate/20241023101945_add_status_attribute_to_room.rb b/db/migrate/20241023101945_add_status_attribute_to_room.rb index 06b25dce1..0089d7eb8 100644 --- a/db/migrate/20241023101945_add_status_attribute_to_room.rb +++ b/db/migrate/20241023101945_add_status_attribute_to_room.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 65d9855be..5d5497e2f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -416,7 +416,7 @@ t.boolean "display_on_home_page" t.integer "site_id", null: false t.integer "islets_count", default: 0 - t.string "status" + t.integer "status", null: false t.index ["site_id"], name: "index_rooms_on_site_id" t.index ["slug"], name: "index_rooms_on_slug", unique: true end diff --git a/spec/models/room_spec.rb b/spec/models/room_spec.rb index b2399f552..7fd6430d1 100644 --- a/spec/models/room_spec.rb +++ b/spec/models/room_spec.rb @@ -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 diff --git a/test/fixtures/rooms.yml b/test/fixtures/rooms.yml index 3f8b70d31..82bf451ab 100644 --- a/test/fixtures/rooms.yml +++ b/test/fixtures/rooms.yml @@ -6,6 +6,7 @@ one: description: Room n°1 site_id: 1 display_on_home_page: true + status: :active two: id: 2 @@ -13,6 +14,7 @@ two: description: Room n°2 site_id: 1 display_on_home_page: true + status: :active three: id: 3 @@ -20,15 +22,18 @@ three: 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