Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

341628 Add cooling_mode and description attributes to Islet #18

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion app/controllers/islets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions app/decorators/islet_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,17 @@ def grouped_by_sites_options_for_select
[site.name, islets]
end
end

def cooling_modes_options_for_select
Islet.cooling_modes.keys.map do |cooling_mode|
[Islet.human_attribute_name("cooling_mode.#{cooling_mode}"), cooling_mode]
end
end
end

def cooling_mode_to_human
return Islet.human_attribute_name("cooling_mode.blank") unless (c_m = cooling_mode.presence)

Islet.human_attribute_name("cooling_mode.#{c_m}")
end
end
2 changes: 2 additions & 0 deletions app/models/islet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
13 changes: 13 additions & 0 deletions app/views/islets/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
<%= f.label :name, class: "form-label" %>
<%= f.text_field :name, class: "form-control" %>
</fieldset>

<fieldset class="col-12 mt-4">
<%= f.label :description, class: "form-label" %>
<%= f.text_area :description, class: "form-control" %>
</fieldset>

<fieldset class="col-12 mt-4">
<%= f.label :cooling_mode, class: "form-label" %>
<%= f.select :cooling_mode,
IsletDecorator::cooling_modes_options_for_select,
B-Rass marked this conversation as resolved.
Show resolved Hide resolved
{ include_blank: t("activerecord.attributes.islet/cooling_mode.blank") },
class: "form-select" %>
</fieldset>
<% end %>

<%= render CardComponent.new(type: :primary, extra_classes: "mt-4 bg-body-tertiary") do |card| %>
Expand Down
5 changes: 4 additions & 1 deletion app/views/islets/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
<div class="col-12 col-lg-6 pe-lg-4">
<%= render CardComponent.new(extra_classes: "mb-4 mb-lg-0") do |card| %>
<dl class="show-page_dl d-grid row-gap-2 mb-0">
<% %i[name].each do |attribute_name| %>
<% %i[name description].each do |attribute_name| %>
<dt class="pb-2"><%= Islet.human_attribute_name(attribute_name) %></dt>
<dd class="mb-0 pb-2 ps-3"><%= @islet.public_send(attribute_name) %></dd>
<% end %>

<dt class="pb-2"><%= Islet.human_attribute_name(:cooling_mode) %></dt>
<dd class="mb-0 pb-2 ps-3"><%= @islet.decorated.cooling_mode_to_human %></dd>
</dl>
<% end %>
</div>
Expand Down
5 changes: 5 additions & 0 deletions config/locales/activerecord.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,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
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20241023155125_add_new_attributes_to_islets.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion db/schema.rb

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

36 changes: 36 additions & 0 deletions spec/decorators/islet_decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe IsletDecorator, type: :decorator do
let(:islet) { islets(:one) }
let(:decorated_islet) { islet.decorated }

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_options_for_select" do
it { expect(described_class.cooling_modes_options_for_select.pluck(1)).to match_array(Islet.cooling_modes.keys) }
end

describe "#cooling_mode_to_human" do
subject(:cooling_mode_sentence) { decorated_islet.cooling_mode_to_human }

context "with no cooling mode" do
it { is_expected.to eq("Pas de confinement") }
end

context "with cooling mode set" do
before { islet.cooling_mode = "hot_containment" }

it { is_expected.to eq("Confinement chaud") }
end
end
end
8 changes: 8 additions & 0 deletions spec/models/islet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(%i[hot_containment cold_containment])
end
end

describe "#to_s" do
pending
end
Expand Down