Skip to content

Commit

Permalink
Merge pull request #60 from TreinaDev/unit_type_refat
Browse files Browse the repository at this point in the history
Refatoração de tipo de unidade
  • Loading branch information
Vinigperuzzi authored Jul 5, 2024
2 parents 7b03c0e + 246bd4a commit 609f238
Show file tree
Hide file tree
Showing 24 changed files with 245 additions and 60 deletions.
8 changes: 4 additions & 4 deletions app/controllers/condos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ def create
@condo = Condo.new(condo_params)

if @condo.save
flash.alert = 'Cadastrado com sucesso!'
redirect_to @condo
redirect_to @condo, notice: t('notices.condo.created')
else
flash.now[:alert] = t('alerts.condo.not_created')
render :new, status: :unprocessable_entity
end
end

def update
if @condo.update(condo_params)
flash.alert = 'Editado com sucesso!'
redirect_to @condo
redirect_to @condo, notice: t('notices.condo.updated')
else
flash.now[:alert] = t('alerts.condo.not_updated')
render :edit, status: :unprocessable_entity
end
end
Expand Down
14 changes: 11 additions & 3 deletions app/controllers/unit_types_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class UnitTypesController < ApplicationController
before_action :set_unit_type, only: %i[edit update show]
before_action :set_condo
before_action :authenticate_manager!

def show; end

Expand All @@ -11,8 +13,10 @@ def edit; end

def create
@unit_type = UnitType.new(unit_type_params)
@unit_type.condo = @condo

if @unit_type.save
redirect_to @unit_type, notice: t('notices.unit_type.created')
redirect_to condo_unit_type_path(@condo, @unit_type), notice: t('notices.unit_type.created')
else
flash.now[:alert] = t('alerts.unit_type.not_created')
render :new, status: :unprocessable_entity
Expand All @@ -21,7 +25,7 @@ def create

def update
if @unit_type.update(unit_type_params)
redirect_to @unit_type, notice: t('notices.unit_type.updated')
redirect_to condo_unit_type_path(@condo, @unit_type), notice: t('notices.unit_type.updated')
else
flash.now[:alert] = t('alerts.unit_type.not_updated')
render :edit, status: :unprocessable_entity
Expand All @@ -34,7 +38,11 @@ def set_unit_type
@unit_type = UnitType.find(params[:id])
end

def set_condo
@condo = Condo.find(params[:condo_id])
end

def unit_type_params
params.require(:unit_type).permit(:description, :metreage)
params.require(:unit_type).permit(:description, :metreage, :fraction)
end
end
1 change: 1 addition & 0 deletions app/models/condo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Condo < ApplicationRecord
belongs_to :address
has_many :towers, dependent: :destroy
has_many :common_areas, dependent: :destroy
has_many :unit_types, dependent: :destroy

validates :name, presence: true
validates :registration_number, uniqueness: true
Expand Down
11 changes: 8 additions & 3 deletions app/models/unit_type.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
class UnitType < ApplicationRecord
has_many :units, dependent: :destroy
belongs_to :condo

validates :description, :metreage, presence: true
validates :metreage, numericality: { greater_than: 0 }
validates :description, :metreage, :fraction, presence: true
validates :metreage, :fraction, numericality: { greater_than: 0 }

def p_metreage
def metreage_to_square_meters
"#{metreage}m²"
end

def fraction_to_percentage
"#{fraction}%"
end
end
31 changes: 28 additions & 3 deletions app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
</li>
<li><hr class="nav-divider m-0"></li>
<li class="nav-item">
<%= link_to 'Criar Tipo de unidade', new_unit_type_path, class: 'nav-link custom-element text-white', data: { turbo: false } %>
<a class="nav-link custom-element text-white" href="#" data-bs-toggle="modal" data-bs-target="#condoSelectPopupForUnitTypes">Criar Tipo de Unidade</a>
</li>
<li><hr class="nav-divider m-0"></li>
<li class="nav-item">
<a class="nav-link custom-element text-white" href="#" data-bs-toggle="modal" data-bs-target="#condoSelectPopup">Criar Torre</a>
<a class="nav-link custom-element text-white" href="#" data-bs-toggle="modal" data-bs-target="#condoSelectPopupForTowers">Criar Torre</a>
</li>
</ul>
</div>
Expand All @@ -53,7 +53,7 @@
</div>
</nav>

<div class="modal fade" id="condoSelectPopup" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="condoSelectPopupLabel" aria-hidden="true" >
<div class="modal fade" id="condoSelectPopupForTowers" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="condoSelectPopupLabel" aria-hidden="true" >
<div class="modal-dialog modal-dialog-scrollable">
<div class="modal-content">
<div class="modal-header">
Expand All @@ -76,4 +76,29 @@
</div>
</div>
</div>
</div>

<div class="modal fade" id="condoSelectPopupForUnitTypes" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="condoSelectPopupLabel" aria-hidden="true" >
<div class="modal-dialog modal-dialog-scrollable">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="condoSelectPopupLabel">Selecione o Condominio</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class='container text-center'>
<div class='row'>
<% Condo.all.each do |condo| %>
<div class='col'>
<%= button_to condo.name, new_condo_unit_type_path(condo), method: :get, class:'btn btn-dark', data: { turbo: false }%>
</div>
<% end %>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>
25 changes: 16 additions & 9 deletions app/views/unit_types/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
<%= form_with(model: @unit_type) do |f| %>
<div>
<%= f.label :description %>
<%= f.text_area :description%>
</div>
<%= form_with(model: [@condo, @unit_type]) do |f| %>
<div class="form-row d-flex p-2">
<div class="form-group col-md-4 pe-3">
<%= f.label :description %>
<%= f.text_area :description, class:"form-control"%>
</div>

<div>
<%= f.label :metreage %>
<%= f.number_field :metreage, step: 0.01%>
<div class="form-group col-md-4 pe-3">
<%= f.label :metreage %>
<%= f.number_field :metreage, step: 0.01, class:"form-control"%>
</div>

<div class="form-group col-md-4 pe-3">
<%= f.label :fraction %> (Valor em porcentagem)
<%= f.number_field :fraction, step: 0.00001, class:"form-control"%>
</div>
</div>

<div>
<%= f.submit %>
<%= f.submit class:"btn btn-dark"%>
</div>
<% end %>
8 changes: 6 additions & 2 deletions app/views/unit_types/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
</div>

<div>
Metragem: <%= @unit_type.p_metreage%>
Metragem: <%= @unit_type.metreage_to_square_meters%>
</div>

<div>
<%= link_to 'Editar', edit_unit_type_path(@unit_type) %>
Fração Ideal: <%= @unit_type.fraction_to_percentage%>
</div>

<div>
<%= link_to 'Editar', edit_condo_unit_type_path(@condo, @unit_type), class:"btn btn-dark" %>
</div>
2 changes: 1 addition & 1 deletion app/views/units/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

<p><%= @unit.unit_type.description %></p>

<p><%= @unit.unit_type.p_metreage %></p>
<p><%= @unit.unit_type.metreage_to_square_meters %></p>
8 changes: 0 additions & 8 deletions config/locales/condo.pt-BR.yml

This file was deleted.

File renamed without changes.
20 changes: 20 additions & 0 deletions config/locales/models/condo.pt-BR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pt-BR:
notices:
condo:
created: 'Condomínio cadastrado com sucesso'
updated: 'Condomínio atualizado com sucesso'
alerts:
condo:
not_created: 'Erro ao cadastrar Condomínio'
not_updated: 'Erro ao atualizar Condomínio'
name: 'Nome'
registration_number: 'CNPJ'
activerecord:
models:
condo:
one: 'Condomínio'
other: 'Condomínios'
attributes:
condo:
name: 'Nome'
registration_number: 'CNPJ'
4 changes: 3 additions & 1 deletion config/locales/models/unit_type.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pt-BR:
not_updated: 'Erro ao atualizar tipo de unidade'
description: 'Descrição'
metreage: 'Metragem'
fraction: 'Fração Ideal'
activerecord:
models:
unit_type:
Expand All @@ -17,4 +18,5 @@ pt-BR:
attributes:
unit_type:
description: 'Descrição'
metreage: 'Metragem'
metreage: 'Metragem'
fraction: 'Fração Ideal'
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
devise_for :managers
resources :managers, only: [:new, :create]
resources :common_areas, only: [:show, :edit, :update]
resources :unit_types, only: [:new, :create, :show, :edit, :update]

resources :condos, only: [:new, :create, :show, :edit, :update] do
resources :common_areas, only: [:new, :create]
resources :unit_types, only: [:new, :create, :show, :edit, :update]

resources :towers, only: [:new, :create] do
member do
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240702201411_add_fraction_to_unit_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddFractionToUnitType < ActiveRecord::Migration[7.1]
def change
add_column :unit_types, :fraction, :integer
end
end
5 changes: 5 additions & 0 deletions db/migrate/20240703193313_add_condo_id_to_unit_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCondoIdToUnitTypes < ActiveRecord::Migration[7.1]
def change
add_reference :unit_types, :condo, null: false, foreign_key: true
end
end
5 changes: 5 additions & 0 deletions db/migrate/20240703200141_change_fraction_to_decimal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ChangeFractionToDecimal < ActiveRecord::Migration[7.1]
def change
change_column :unit_types, :fraction, :decimal, precision: 10, scale: 5
end
end
6 changes: 5 additions & 1 deletion db/schema.rb

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

2 changes: 2 additions & 0 deletions spec/factories/unit_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
factory :unit_type do
description { 'Apartamento de 1 quarto' }
metreage { 50.55 }
fraction { 3 }
condo { build :condo }
end
end
46 changes: 39 additions & 7 deletions spec/models/unit_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,58 @@
RSpec.describe UnitType, type: :model do
context '#valid?' do
it 'missing params' do
unit_type = UnitType.new(description: '', metreage: '')
unit_type = UnitType.new(description: '', metreage: '', fraction: '')

expect(unit_type).not_to be_valid
expect(unit_type.errors.include?(:description)).to be true
expect(unit_type.errors.include?(:metreage)).to be true
expect(unit_type.errors).to include(:description)
expect(unit_type.errors).to include(:metreage)
expect(unit_type.errors).to include(:fraction)
end

context 'Metreage must be bigger than zero' do
it 'metreage equal zero' do
unit_type = UnitType.new(metreage: 0)
unit_type.valid?

expect(unit_type.errors.include?(:metreage)).to be true
expect(unit_type).not_to be_valid
expect(unit_type.errors.full_messages).to include('Metragem deve ser maior que 0')
end
it 'metreage less than zero' do
unit_type = UnitType.new(metreage: -1)
unit_type.valid?

expect(unit_type.errors.include?(:metreage)).to be true
expect(unit_type).not_to be_valid
expect(unit_type.errors.full_messages).to include('Metragem deve ser maior que 0')
end
end

context 'Fraction must be bigger than zero' do
it 'fraction equal zero' do
unit_type = UnitType.new(fraction: 0)

expect(unit_type).not_to be_valid
expect(unit_type.errors.full_messages).to include('Fração Ideal deve ser maior que 0')
end
it 'fraction less than zero' do
unit_type = UnitType.new(fraction: -1)

expect(unit_type).not_to be_valid
expect(unit_type.errors.full_messages).to include('Fração Ideal deve ser maior que 0')
end
end
end

context 'formatted text' do
it 'show metreage value with square meter (m²)' do
unit_type = create(:unit_type, metreage: 12.45)
styled_text = unit_type.metreage_to_square_meters

expect(styled_text).to eq('12.45m²')
end

it 'show fraction value with percentage symbol (%)' do
unit_type = create(:unit_type, fraction: 10.12345)
styled_text = unit_type.fraction_to_percentage

expect(styled_text).to eq('10.12345%')
end
end
end
Loading

0 comments on commit 609f238

Please sign in to comment.