Skip to content

Commit

Permalink
Add cooling_mode and description attributes to Islet
Browse files Browse the repository at this point in the history
  • Loading branch information
B-Rass committed Oct 24, 2024
1 parent 5af7c3c commit a5fa6ad
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 3 deletions.
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
6 changes: 6 additions & 0 deletions app/decorators/islet_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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_for_options,
{ 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
11 changes: 10 additions & 1 deletion app/views/islets/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@
<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">
<% if @islet.cooling_mode.present? %>
<%= Islet.human_attribute_name("cooling_mode.#{@islet.cooling_mode}") %>
<% else %>
<%= Islet.human_attribute_name("cooling_mode.blank") %>
<% end %>
</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 @@ -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
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.

19 changes: 19 additions & 0 deletions spec/decorators/islet_decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -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
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([:hot_containment, :cold_containment])
end
end

describe "#to_s" do
pending
end
Expand Down

0 comments on commit a5fa6ad

Please sign in to comment.