From 0c49d61e4f6e242f2da6abaf088b05a4e7ca138f Mon Sep 17 00:00:00 2001 From: B_Rass Date: Thu, 24 Oct 2024 11:02:10 +0200 Subject: [PATCH] Add cooling_mode and description attributes to Islet --- app/controllers/islets_controller.rb | 2 +- app/decorators/islet_decorator.rb | 6 ++++++ app/models/islet.rb | 2 ++ app/views/islets/_form.html.erb | 13 +++++++++++++ app/views/islets/show.html.erb | 11 ++++++++++- config/locales/activerecord.fr.yml | 5 +++++ ...1023155125_add_new_attributes_to_islets.rb | 10 ++++++++++ db/schema.rb | 4 +++- spec/decorators/islet_decorator_spec.rb | 19 +++++++++++++++++++ spec/models/islet_spec.rb | 8 ++++++++ 10 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20241023155125_add_new_attributes_to_islets.rb create mode 100644 spec/decorators/islet_decorator_spec.rb diff --git a/app/controllers/islets_controller.rb b/app/controllers/islets_controller.rb index 107aada3e..ac07fb770 100644 --- a/app/controllers/islets_controller.rb +++ b/app/controllers/islets_controller.rb @@ -86,7 +86,7 @@ def set_islet # Never trust parameters from the scary internet, only allow the white list through. def islet_params - params.require(:islet).permit(:name, :room_id, :position) + params.require(:islet).permit(:name, :room_id, :position, :description, :cooling_mode) end def set_room diff --git a/app/decorators/islet_decorator.rb b/app/decorators/islet_decorator.rb index ec7d51827..8a28f7106 100644 --- a/app/decorators/islet_decorator.rb +++ b/app/decorators/islet_decorator.rb @@ -12,5 +12,11 @@ def grouped_by_sites_options_for_select [site.name, islets] end end + + def cooling_modes_for_options + Islet.cooling_modes.keys.map do |cooling_mode| + [Islet.human_attribute_name("cooling_mode.#{cooling_mode}"), cooling_mode] + end + end end end diff --git a/app/models/islet.rb b/app/models/islet.rb index ec8374b81..d4c6ee74b 100644 --- a/app/models/islet.rb +++ b/app/models/islet.rb @@ -10,6 +10,8 @@ class Islet < ApplicationRecord has_many :servers, through: :frames has_many :materials, through: :frames + enum :cooling_mode, { hot_containment: 0, cold_containment: 1 }, validate: { allow_nil: true } + scope :sorted, -> { order(:room_id, :position, :name) } scope :not_empty, -> { joins(:materials) } scope :has_name, -> { where.not(name: nil) } diff --git a/app/views/islets/_form.html.erb b/app/views/islets/_form.html.erb index b018562ae..1142821b8 100644 --- a/app/views/islets/_form.html.erb +++ b/app/views/islets/_form.html.erb @@ -5,6 +5,19 @@ <%= f.label :name, class: "form-label" %> <%= f.text_field :name, class: "form-control" %> + +
+ <%= f.label :description, class: "form-label" %> + <%= f.text_area :description, class: "form-control" %> +
+ +
+ <%= f.label :cooling_mode, class: "form-label" %> + <%= f.select :cooling_mode, + IsletDecorator::cooling_modes_for_options, + { include_blank: t("activerecord.attributes.islet/cooling_mode.blank") }, + class: "form-select" %> +
<% end %> <%= render CardComponent.new(type: :primary, extra_classes: "mt-4 bg-body-tertiary") do |card| %> diff --git a/app/views/islets/show.html.erb b/app/views/islets/show.html.erb index 5bb33352b..9644c5a73 100644 --- a/app/views/islets/show.html.erb +++ b/app/views/islets/show.html.erb @@ -12,10 +12,19 @@
<%= render CardComponent.new(extra_classes: "mb-4 mb-lg-0") do |card| %>
- <% %i[name].each do |attribute_name| %> + <% %i[name description].each do |attribute_name| %>
<%= Islet.human_attribute_name(attribute_name) %>
<%= @islet.public_send(attribute_name) %>
<% end %> + +
<%= Islet.human_attribute_name(:cooling_mode) %>
+
+ <% if @islet.cooling_mode.present? %> + <%= Islet.human_attribute_name("cooling_mode.#{@islet.cooling_mode}") %> + <% else %> + <%= Islet.human_attribute_name("cooling_mode.blank") %> + <% end %> +
<% end %>
diff --git a/config/locales/activerecord.fr.yml b/config/locales/activerecord.fr.yml index 29da0d6ee..56f7775ae 100644 --- a/config/locales/activerecord.fr.yml +++ b/config/locales/activerecord.fr.yml @@ -70,10 +70,15 @@ fr: room_id: Salle room: Salle # TODO: check if we should keep this line? position: Position + cooling_mode: Mode de refroidissement frames_count: zero: 0 châssis one: 1 châssis other: "%{count} châssis" + islet/cooling_mode: + blank: Pas de confinement + hot_containment: Confinement chaud + cold_containment: Confinement froid frame/view_sides: front: Vue avant back: Vue arrière diff --git a/db/migrate/20241023155125_add_new_attributes_to_islets.rb b/db/migrate/20241023155125_add_new_attributes_to_islets.rb new file mode 100644 index 000000000..702ef090f --- /dev/null +++ b/db/migrate/20241023155125_add_new_attributes_to_islets.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class AddNewAttributesToIslets < ActiveRecord::Migration[7.2] + def change + change_table :islets, bulk: true do |t| + t.text :description + t.integer :cooling_mode + end + end +end diff --git a/db/schema.rb b/db/schema.rb index b8ac4555d..25251d9fe 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_10_17_143515) do +ActiveRecord::Schema[7.2].define(version: 2024_10_23_155125) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -296,6 +296,8 @@ t.datetime "created_at", precision: nil, null: false t.datetime "updated_at", precision: nil, null: false t.integer "position" + t.text "description" + t.integer "cooling_mode" t.index ["room_id"], name: "index_islets_on_room_id" end diff --git a/spec/decorators/islet_decorator_spec.rb b/spec/decorators/islet_decorator_spec.rb new file mode 100644 index 000000000..5a9bb0905 --- /dev/null +++ b/spec/decorators/islet_decorator_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe IsletDecorator, type: :decorator do + describe ".grouped_by_sites_options_for_select" do + let(:expected_response) do + { + "Site 1" => [["S1 Ilot Islet1", 1], ["S1 Ilot Islet2", 2]] + } + end + + it { expect(described_class.grouped_by_sites_options_for_select).to match(expected_response) } + end + + describe ".cooling_modes_for_options" do + it { expect(described_class.cooling_modes_for_options.pluck(1)).to match_array(Islet.cooling_modes.keys) } + end +end diff --git a/spec/models/islet_spec.rb b/spec/models/islet_spec.rb index 1f4bd02a6..8ce6eff18 100644 --- a/spec/models/islet_spec.rb +++ b/spec/models/islet_spec.rb @@ -16,6 +16,14 @@ it { is_expected.to have_many(:materials).through(:frames) } end + describe "validations" do + it do + is_expected.to define_enum_for(:cooling_mode) # rubocop:disable RSpec/ImplicitSubject + .validating(allowing_nil: true) + .with_values([:hot_containment, :cold_containment]) + end + end + describe "#to_s" do pending end