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

Implementa Registro de Mandato de Sindico #108

Merged
merged 14 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
84 changes: 84 additions & 0 deletions app/controllers/superintendents_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
class SuperintendentsController < ApplicationController
before_action :authenticate_manager!, only: %i[show new create edit update]
before_action :set_condo, only: %i[new create edit update]
before_action :set_superintendent, only: %i[show edit update]
before_action :set_breadcrumbs_condo, only: %i[new create edit update]
before_action :set_breadcrumbs_for_register, only: %i[new create]
before_action :set_breadcrumbs_for_edit, only: %i[edit update]
before_action -> { authorize_condo_manager!(@condo) }, only: %i[show new create edit update]

def show
@condo = @superintendent.condo
@tenant = @superintendent.tenant
add_breadcrumb @condo.name.to_s, @condo
add_breadcrumb I18n.t('breadcrumb.superintendent.show')
end

def new
if @condo.superintendent
return redirect_to superintendent_path(@condo.superintendent),
alert: t('alerts.superintendent.exists')
end

@superintendent = Superintendent.new(condo: @condo)
@tenants = @condo.tenants
end

def edit
@tenants = @condo.tenants
end

def create
@superintendent = Superintendent.new(superintendent_params.merge!(condo: @condo))

if @superintendent.save
redirect_to @superintendent, notice: t('notices.superintendent.created')
else
@tenants = @condo.tenants
flash.now[:alert] = t('alerts.superintendent.not_created')
render 'new', status: :unprocessable_entity
end
DaniloRibeiro07 marked this conversation as resolved.
Show resolved Hide resolved
end

def update
if @superintendent.update(superintendent_params)
redirect_to @superintendent, notice: t('notices.superintendent.updated')
else
@tenants = @condo.tenants
flash.now[:alert] = t('alerts.superintendent.not_updated')
render 'new', status: :unprocessable_entity
end
end

private

def authenticate_manager!
return redirect_to root_path if resident_signed_in?

super
end

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

def set_superintendent
@superintendent = Superintendent.find params[:id]
end

def superintendent_params
params.require(:superintendent).permit :start_date, :end_date, :tenant_id
end

def set_breadcrumbs_for_register
add_breadcrumb I18n.t('breadcrumb.superintendent.new')
end

def set_breadcrumbs_condo
add_breadcrumb @condo.name.to_s, @condo
end

def set_breadcrumbs_for_edit
add_breadcrumb I18n.t('breadcrumb.superintendent.edit')
end
end
1 change: 1 addition & 0 deletions app/models/condo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Condo < ApplicationRecord
has_many :units, through: :floors
has_many :owners, through: :units
has_many :tenants, through: :units
has_one :superintendent, dependent: :destroy

delegate :city, to: :address
delegate :state, to: :address
Expand Down
2 changes: 2 additions & 0 deletions app/models/resident.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Resident < ApplicationRecord
has_one :residence, class_name: 'Unit', foreign_key: 'tenant_id', dependent: :nullify, inverse_of: :tenant
has_many :properties, class_name: 'Unit', foreign_key: 'owner_id', dependent: :nullify, inverse_of: :owner
has_one :superintendent, class_name: 'Superintendent', foreign_key: 'tenant_id', dependent: :nullify,
inverse_of: :tenant

devise :database_authenticatable, :recoverable, :rememberable, :validatable
validate :valid_registration_number
Expand Down
18 changes: 18 additions & 0 deletions app/models/superintendent.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Superintendent < ApplicationRecord
belongs_to :tenant, class_name: 'Resident'
belongs_to :condo, dependent: :destroy

validates :start_date, :end_date, presence: true

validate :start_date_must_be_valid, :end_date_must_be_valid, on: :create

def start_date_must_be_valid
errors.add(:start_date, 'deve ser atual ou futura') if start_date&.past?
DaniloRibeiro07 marked this conversation as resolved.
Show resolved Hide resolved
end

def end_date_must_be_valid
return unless end_date && start_date

errors.add(:end_date, 'deve ser maior que a data de ínicio') if start_date >= end_date
end
end
9 changes: 8 additions & 1 deletion app/views/condos/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
<div class="p-0" style="width: fit-content;">
<h1 class="text-center"><%= @condo.name %></h1>
<p class="mb-1 text-secondary" style="font-size: 14px;"><%= @condo.full_address %></p>
<p class="text-secondary" style="font-size: 14px;">CNPJ: <%= @condo.registration_number %></p>
<p class="mb-1 text-secondary" style="font-size: 14px;">CNPJ: <%= @condo.registration_number %></p>
<p class="mb-1 text-secondary" style="font-size: 14px;">Síndico:
<% if @condo.superintendent%>
<%= link_to @condo.superintendent.tenant.full_name, superintendent_path(@condo.superintendent) %>
<% else %>
Não há um síndico ativo
<% end %>
</p>
</div>
</div>

Expand Down
34 changes: 34 additions & 0 deletions app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
</a>
<ul class="dropdown-menu dropdown-menu-dark" style="background-color: #4c677f">
<li><%= link_to 'Cadastrar Morador', new_resident_path, class: "dropdown-item" %></li>
<li><hr class="nav-divider m-0"></li>
<li class="nav-item">
<a class="dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#condoSelectPopupForSuperintendent">Cadastrar Síndico</a>
</li>
<% if current_manager.is_super %>
<li><hr class="nav-divider m-0"></li>
<li><%= link_to 'Cadastrar Administrador', new_manager_path, class: "dropdown-item" %></li>
Expand Down Expand Up @@ -181,6 +185,36 @@
</div>
</div>

<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>

<div class="modal fade" id="condoSelectPopupForSuperintendent" 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 Condomínio</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'>
<% if current_manager %>
<% condos = current_manager.is_super ? Condo.all : current_manager.condos %>
<% condos.each do |condo| %>
<div class='col'>
<%= button_to condo.name, new_condo_superintendent_path(condo), method: :get, class:'btn btn-dark', data: { turbo: false } %>
</div>
<% end %>
<% end %>
</div>
</div>
</div>
DaniloRibeiro07 marked this conversation as resolved.
Show resolved Hide resolved

<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Fechar</button>
</div>
Expand Down
33 changes: 33 additions & 0 deletions app/views/superintendents/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

<section class="bg-white rounded-5 py-4 px-5 shadow">

<%= form_with model: [@condo, @superintendent] do |f| %>

<div class="form-row p-2">
<div class="form-group col-md-12 pe-3">
<%= f.label :start_date %>
<%= f.date_field :start_date, class:"form-control" %>
</div>
</div>

<div class="form-row p-2">
<div class="form-group col-md-12 pe-3">
<%= f.label :end_date %>
<%= f.date_field :end_date, class:"form-control" %>
</div>
</div>

<div class="form-row d-flex p-2">
<div class="form-group col-md-12 pe-3">
<%= f.label :tenant_id %>
<%= f.collection_select :tenant_id, @tenants, :id, :full_name, {} ,class: 'form-control form-select' %>
</div>
</div>

<div class="form-group d-flex justify-content-center">
<%= f.submit 'Enviar', class:'btn btn-dark rounded-pill px-4 mt-3' %>
</div>

<%end%>
DaniloRibeiro07 marked this conversation as resolved.
Show resolved Hide resolved

</section>
4 changes: 4 additions & 0 deletions app/views/superintendents/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= render 'shared/errors', model: @superintendent %>
<h1>Editar Mandato de Síndico para <%= @condo.name %></h1>

<%= render 'form' %>
4 changes: 4 additions & 0 deletions app/views/superintendents/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= render 'shared/errors', model: @superintendent %>
<h1>Registrar Mandato de Síndico para <%= @condo.name %></h1>

<%= render 'form' %>
34 changes: 34 additions & 0 deletions app/views/superintendents/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<h1>Síndico do <%= @condo.name %></h1>

<div class="bg-white rounded-5 py-4 px-5 shadow">
<div class="d-flex align-items-center">
<div class="current-photo mb-4">
<% if @tenant.user_image.attached? %>
<%= image_tag @tenant.user_image, alt: 'Current Image', style: "width: 200px; height: 200px; border-radius: 20%;" %>
DaniloRibeiro07 marked this conversation as resolved.
Show resolved Hide resolved
<% else %>
<%= image_tag 'blank-profile-icon.webp', alt: 'No image profile', id: 'resident-profile-image', style:"width: 200px; height: 200px; border-radius: 20%;" %>
<% end %>
</div>
<div class="ms-4" id="superintendent_data">
<h2>Dados do Síndico</h2>
<div class="mb-3">
<p><strong>Nome Completo:</strong> <%= @tenant.full_name %></p>
<p><strong>E-mail:</strong> <%= @tenant.email %></p>
<p><strong>Condomínio:</strong> <%= @tenant.residence.condo.name %></p>
</div>
<div>
<p><strong>Torre:</strong> <%= @tenant.residence.tower.name %></p>
<p><strong>Unidade:</strong> <%= @tenant.residence.short_identifier %></p>
<p><strong>Ínicio do mandato:</strong> <%= I18n.l(@superintendent.start_date) %></p>
<p><strong>Fim do mandato:</strong> <%= I18n.l(@superintendent.end_date) %></p>
</div>
</div>
</div>

<% if manager_signed_in? %>
<div class="form-group d-flex justify-content-center">
<%= button_to 'Editar Síndico', edit_condo_superintendent_path(@condo, @superintendent), method: :get, class:'btn btn-dark rounded-pill px-4 mt-3' %>
</div>
<% end %>

</div>
4 changes: 4 additions & 0 deletions config/locales/breadcrumb.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ pt-BR:
new: 'Adicionar Propriedade'
tenant:
new: 'Adicionar Moradia'
superintendent:
new: 'Cadastrar Síndico'
show: 'Síndico'
edit: 'Atualizar Síndico'
21 changes: 21 additions & 0 deletions config/locales/models/superintendent.pt-BR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pt-BR:
notices:
superintendent:
created: 'Mandato de síndico cadastro com sucesso!'
updated: 'Mandato de síndico atualizado com sucesso!'
alerts:
superintendent:
not_created: 'Não foi possível cadastrar o mandato.'
not_updated: 'Não foi possível atualizar o mandato.'
exists: 'Esse condomínio já possui um síndico cadastrado!'
activerecord:
models:
superintendent:
one: 'Síndico'
other: 'Síndicos'
attributes:
superintendent:
start_date: 'Data de ínicio'
end_date: 'Data de conclusão'
tenant_id: 'Morador'
tenant: 'Morador'
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@

resources :common_areas, only: [:show, :edit, :update]
resources :unit_types, only: [:show, :edit, :update]
resources :superintendents, only: [:show]

resources :condos, only: [:new, :create, :show, :edit, :update] do
get 'residents', on: :member
resources :common_areas, only: [:new, :create]
resources :unit_types, only: [:new, :create]
resources :visitor_entries, only: [:index, :new, :create]
resources :superintendents, only: [:new, :create, :edit, :update]

resources :towers, only: [:new, :create] do
member do
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20240717022634_create_superintendents.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateSuperintendents < ActiveRecord::Migration[7.1]
def change
create_table :superintendents do |t|
t.references :resident, null: false, foreign_key: true
t.date :start_date, null: false
t.date :end_date, null: false

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20240717033254_add_condo_to_superintendents.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCondoToSuperintendents < ActiveRecord::Migration[7.1]
def change
add_reference :superintendents, :condo, null: false, foreign_key: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveResidentFromSuperintendents < ActiveRecord::Migration[7.1]
def change
remove_reference :superintendents, :resident, null: false, foreign_key: true
end
end
5 changes: 5 additions & 0 deletions db/migrate/20240717033755_add_tenant_to_superintendents.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTenantToSuperintendents < ActiveRecord::Migration[7.1]
def change
add_reference :superintendents, :tenant, null: false, foreign_key: { to_table: :residents }
end
end
15 changes: 14 additions & 1 deletion db/schema.rb

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

8 changes: 8 additions & 0 deletions spec/factories/superintendents.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :superintendent do
tenant { build :resident }
condo { build :condo }
start_date { Time.zone.today }
end_date { Time.zone.today >> 6 }
end
end
Loading
Loading