Skip to content

Commit

Permalink
Centralizando atualização status em um método
Browse files Browse the repository at this point in the history
  • Loading branch information
FernandoKGA committed Dec 4, 2019
1 parent 71bf68a commit 49608f2
Show file tree
Hide file tree
Showing 5 changed files with 1,791 additions and 44 deletions.
47 changes: 13 additions & 34 deletions src/app/app/controllers/college_whitelists_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CollegeWhitelistsController < ApplicationController
before_action :require_logged_in_user
before_action :set_college
before_action :set_college_whitelist, only: [:show, :edit, :update, :destroy]
before_action :set_college_whitelist, only: [:show, :edit, :destroy]

def verify_permission
if @college.request_to_participate == "0"
Expand All @@ -15,7 +15,7 @@ def verify_permission
# GET /college_whitelists.json
def index
if isPageAdmin(params[:college_id])
@college_whitelists = CollegeWhitelist.where(college_id: params[:college_id])
set_whitelist_by_college
render :index
else
flash[:danger] = 'Área restrita. Você não é administrador da página.'
Expand Down Expand Up @@ -50,38 +50,10 @@ def create
end
end

def revoke
@college_whitelist = CollegeWhitelist.find(params[:college_whitelist_id])
respond_to do |format|
if @college_whitelist.update(status: :pending)
format.html { redirect_to college_college_whitelists_path }
else
format.html {
flash[:danger] = "Não foi possível revogar a credencial do usuário para pendente."
render :index
}
end
end
end

def aprove
@college_whitelist = CollegeWhitelist.find(params[:college_whitelist_id])
respond_to do |format|
if @college_whitelist.update(status: :approved)
format.html { redirect_to college_college_whitelists_path }
else
format.html {
flash[:danger] = "Não foi possível alterar a credencial do usuário para aprovado."
render :index
}
end
end
end

def reject
@college_whitelist = CollegeWhitelist.find(params[:college_whitelist_id])
def update
@college_whitelist = CollegeWhitelist.find(params[:id])
respond_to do |format|
if @college_whitelist.update(status: :rejected)
if @college_whitelist.update(status: params[:college_whitelist][:status])
format.html { redirect_to college_college_whitelists_path }
else
format.html {
Expand All @@ -107,6 +79,10 @@ def college_whitelist_params
params.require(:college_whitelist).permit(:status)
end

def set_whitelist_by_college
@college_whitelists = CollegeWhitelist.where(college_id: params[:college_id])
end

def check_college_access_permission
@college_whitelist = CollegeWhitelist.find_by(user_id: current_user.id, college_id: @college.id)
if !@college_whitelist.nil?
Expand All @@ -128,5 +104,8 @@ def check_request_access_status(college_whitelist)
end
end


def return_status_enum(status)
p status
CollegeWhitelist.statuses[status]
end
end
8 changes: 4 additions & 4 deletions src/app/app/views/college_whitelists/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
<td class="column-name"><%= college_whitelist.user.first_name + ' ' + college_whitelist.user.last_name %></td>
<td class="column-btn">
<div class="btn-area">
<%= button_to 'Aprovar', { action: "aprove", :college_whitelist => {:status => college_whitelist.status}, :college_id => college_whitelist.college.id, :college_whitelist_id => college_whitelist.id }, method: "patch", class: "botao super small" %>
<%= button_to 'Rejeitar', { action: "reject", :college_whitelist => {:status => college_whitelist.status}, :college_id => college_whitelist.college.id, :college_whitelist_id => college_whitelist.id }, method: "patch", class: "botao super small" %>
<%= button_to 'Aprovar', { action: "update", :college_whitelist => {:status => :approved}, :college_id => college_whitelist.college_id, :id => college_whitelist.id}, method: "patch", class: "botao super small" %>
<%= button_to 'Rejeitar', { action: "update", :college_whitelist => {:status => :rejected}, :college_id => college_whitelist.college_id, :id => college_whitelist.id}, method: "patch", class: "botao super small" %>
</div>
</td>
</tr>
Expand All @@ -47,7 +47,7 @@
<tr>
<td class="column-name"><%= college_whitelist.user.first_name + ' ' + college_whitelist.user.last_name %></td>
<% if !isUserAdminOfPage(college_whitelist.college.id, college_whitelist.user.id) %>
<td class="column-btn"><%= button_to 'Revogar', { action: "revoke", :college_whitelist => {:status => college_whitelist.status}, :college_id => college_whitelist.college.id, :college_whitelist_id => college_whitelist.id }, method: "patch", class: "botao super small" %></td>
<td class="column-btn"><%= button_to 'Revogar', { action: "update", :college_whitelist => {:status => :pending}, :college_id => college_whitelist.college_id, :id => college_whitelist.id}, method: "patch", class: "botao super small" %></td>
<% end %>
</tr>
<% end %>
Expand All @@ -70,7 +70,7 @@
<% if (college_whitelist.status == 'rejected') %>
<tr>
<td class="column-name"><%= college_whitelist.user.first_name + ' ' + college_whitelist.user.last_name %></td>
<td class="column-btn"><%= button_to 'Revogar', { action: "revoke", :college_whitelist => {:status => college_whitelist.status}, :college_id => college_whitelist.college.id, :college_whitelist_id => college_whitelist.id }, method: "patch", class: "botao super small" %></td>
<td class="column-btn"><%= button_to 'Revogar', { action: "update", :college_whitelist => {:status => :pending}, :college_id => college_whitelist.college_id, :id => college_whitelist.id}, method: "patch", class: "botao super small" %></td>
</tr>
<% end %>
<% end %>
Expand Down
7 changes: 1 addition & 6 deletions src/app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,8 @@
end

resources :colleges, only: [:new, :create, :show, :index, :edit, :update] do
resources :college_whitelists, only: [:new, :create, :index] do
patch 'aprove', to: 'college_whitelists#aprove'
patch 'reject', to: 'college_whitelists#reject'
patch 'revoke', to: 'college_whitelists#revoke'
end
resources :college_whitelists, only: [:new, :create, :index, :update]
get 'verify_permission', to: 'college_whitelists#verify_permission'

end

resources :spotteds, only: [:new, :create, :show, :index] do
Expand Down
Loading

0 comments on commit 49608f2

Please sign in to comment.