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 all 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
83 changes: 83 additions & 0 deletions app/controllers/superintendents_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
class SuperintendentsController < ApplicationController
before_action :authenticate_manager!, only: %i[new create edit update]
before_action :set_condo, only: %i[show 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_user(@condo) }, only: %i[show new create edit update]

def show
@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 condo_superintendent_path(@condo, @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))

unless @superintendent.save
@tenants = @condo.tenants
flash.now[:alert] = t('alerts.superintendent.not_created')
return render 'new', status: :unprocessable_entity
end

redirect_to condo_superintendent_path(@condo, @superintendent), notice: t('notices.superintendent.created')
end

def update
unless @superintendent.update(superintendent_params)
@tenants = @condo.tenants
flash.now[:alert] = t('alerts.superintendent.not_updated')
return render 'new', status: :unprocessable_entity
end

redirect_to condo_superintendent_path(@condo, @superintendent), notice: t('notices.superintendent.updated')
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 @@ -11,6 +11,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
Expand Up @@ -3,6 +3,8 @@ class Resident < ApplicationRecord
has_many :properties, class_name: 'Unit', foreign_key: 'owner_id', dependent: :nullify, inverse_of: :owner
has_many :reservations, dependent: :destroy
has_many :visitors, dependent: :destroy
has_one :superintendent, class_name: 'Superintendent', foreign_key: 'tenant_id', dependent: :nullify,
inverse_of: :tenant

delegate :condo, to: :residence, allow_nil: true

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

def start_date_must_be_valid
errors.add :start_date, 'deve ser atual ou futura' if start_date&.past?
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 w-fit">
<h1 class="text-center"><%= @condo.name %></h1>
<p class="mb-1 text-secondary fs-sm" ><%= @condo.full_address %></p>
<p class="text-secondary fs-sm">CNPJ: <%= @condo.registration_number %></p>
<p class="mb-1 text-secondary fs-sm">CNPJ: <%= @condo.registration_number %></p>
<p class="mb-1 text-secondary fs-sm">Síndico:
<% if @condo.superintendent%>
<%= link_to @condo.superintendent.tenant.full_name, condo_superintendent_path(@condo, @condo.superintendent) %>
<% else %>
Não há um síndico ativo
<% end %>
</p>
</div>
</div>

Expand Down
36 changes: 36 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 bg-medium-blue">
<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 @@ -187,6 +191,38 @@
</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-lg 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-fluid'>
<div class='row'>
<% if current_manager %>
<% condos = current_manager.is_super ? Condo.all : current_manager.condos %>
<% condos.each do |condo| %>
<div class='col-auto'>
<%= link_to new_condo_superintendent_path(condo), class:"btn btn-dark rounded-pill d-flex mb-2 shadow-sm", data: { turbo: false } do %>
<i class="bi bi-bookmark-plus me-2"></i> <p style="margin: 0; font-size: 14px;"><%= condo.name %></p>
<% end %>
</div>
<% end %>
<% end %>
</div>
</div>
</div>

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

<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 #{'is-invalid'if @superintendent.errors[:start_date].any?}", min: Date.today %>
<%= render("shared/errors", model: @superintendent, attribute: :start_date) if @superintendent.errors[:start_date].any? %>
</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 #{'is-invalid'if @superintendent.errors[:end_date].any?}", min: Date.today %>
<%= render("shared/errors", model: @superintendent, attribute: :end_date) if @superintendent.errors[:end_date].any? %>
</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 #{'is-invalid'if @superintendent.errors[:tenant].any?}" %>
<%= render("shared/errors", model: @superintendent, attribute: :tenant) if @superintendent.errors[:tenant].any? %>
</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 %>

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

<%= render 'form' %>
3 changes: 3 additions & 0 deletions app/views/superintendents/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<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', class:"user-image-200" %>
<% else %>
<%= image_tag 'blank-profile-icon.webp', alt: 'No image profile', id: 'resident-profile-image', class:"user-image-200" %>
<% 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 @@ -26,6 +26,10 @@ pt-BR:
new: 'Adicionar Propriedade'
tenant:
new: 'Adicionar Moradia'
superintendent:
new: 'Cadastrar Síndico'
show: 'Síndico'
edit: 'Atualizar Síndico'
announcement:
index: 'Avisos'
new: 'Novo Aviso'
Expand Down
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'
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
resources :common_areas, only: [:new, :create]
resources :unit_types, only: [:new, :create]
resources :visitor_entries, only: [:index, :new, :create]
resources :superintendents, only: [:show, :new, :create, :edit, :update]
resources :announcements, only: [:index, :new, :create]

resources :visitors 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
11 changes: 11 additions & 0 deletions 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
condo { build :condo }
tenant { create(:resident, :with_residence, condo:) }
start_date { Time.zone.today }
end_date { Time.zone.today >> 6 }
end
end
29 changes: 29 additions & 0 deletions spec/models/superintendent_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'rails_helper'

RSpec.describe Superintendent, type: :model do
describe '#valid?' do
it 'missing params' do
superintendent = build :superintendent, start_date: nil, end_date: nil, tenant: nil, condo: nil

expect(superintendent).not_to be_valid
expect(superintendent.errors).to include :start_date
expect(superintendent.errors).to include :end_date
expect(superintendent.errors).to include :tenant
expect(superintendent.errors).to include :condo
end

it 'start date greater than or equal to today' do
superintendent = build :superintendent, start_date: Date.yesterday

expect(superintendent).not_to be_valid
expect(superintendent.errors).to include :start_date
end

it 'end date greater than to start_date' do
superintendent = build :superintendent, start_date: Time.zone.today, end_date: Time.zone.today

expect(superintendent).not_to be_valid
expect(superintendent.errors).to include :end_date
end
end
end
Loading
Loading