-
Notifications
You must be signed in to change notification settings - Fork 70
Acceptance between administrators #790
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
Open
gmartincor
wants to merge
11
commits into
time-between-banks-base
Choose a base branch
from
acceptance-between-administrators
base: time-between-banks-base
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+859
−26
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
88c808b
alliance between diffents timebanks
gmartincor 838cbbe
Bottom correction
gmartincor 3d63279
class bottom corrected
gmartincor f6616c9
tests
gmartincor c93d0a6
Refactor Code
gmartincor 61888ac
Correct comments #790
gmartincor 560fdca
structure.sql
gmartincor a60e50a
.
gmartincor e14570c
.
gmartincor 13cd2c4
.
gmartincor 48d9950
.
gmartincor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
class OrganizationAlliancesController < ApplicationController | ||
before_action :authenticate_user! | ||
before_action :member_should_exist_and_be_active | ||
before_action :authorize_admin | ||
before_action :find_alliance, only: [:update, :destroy] | ||
|
||
def index | ||
@status = params[:status] || "pending" | ||
|
||
@alliances = case @status | ||
when "pending" | ||
current_organization.pending_alliances.includes(:source_organization, :target_organization) | ||
when "accepted" | ||
current_organization.accepted_alliances.includes(:source_organization, :target_organization) | ||
when "rejected" | ||
current_organization.rejected_alliances.includes(:source_organization, :target_organization) | ||
else | ||
[] | ||
end | ||
end | ||
|
||
def create | ||
alliance = OrganizationAlliance.new( | ||
source_organization: current_organization, | ||
target_organization_id: alliance_params[:target_organization_id], | ||
status: "pending" | ||
) | ||
|
||
if alliance.save | ||
flash[:notice] = t("organization_alliances.created") | ||
else | ||
flash[:error] = alliance.errors.full_messages.to_sentence | ||
end | ||
|
||
redirect_back fallback_location: organizations_path | ||
end | ||
|
||
def update | ||
if @alliance.update(status: params[:status]) | ||
flash[:notice] = t("organization_alliances.updated") | ||
else | ||
flash[:error] = @alliance.errors.full_messages.to_sentence | ||
end | ||
|
||
redirect_to organization_alliances_path | ||
end | ||
|
||
def destroy | ||
if @alliance.destroy | ||
flash[:notice] = t("organization_alliances.destroyed") | ||
else | ||
flash[:error] = t("organization_alliances.error_destroying") | ||
end | ||
|
||
redirect_to organization_alliances_path | ||
end | ||
|
||
private | ||
|
||
def find_alliance | ||
@alliance = OrganizationAlliance.find(params[:id]) | ||
end | ||
|
||
def authorize_admin | ||
return if current_user.manages?(current_organization) | ||
|
||
flash[:error] = t("organization_alliances.not_authorized") | ||
redirect_to root_path | ||
end | ||
|
||
def alliance_params | ||
params.require(:organization_alliance).permit(:target_organization_id) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module OrganizationsHelper | ||
def filterable_organizations | ||
Organization.all.order(:name) | ||
end | ||
|
||
def allied_organizations | ||
return [] unless current_organization | ||
|
||
allied_org_ids = current_organization.accepted_alliances.map do |alliance| | ||
alliance.source_organization_id == current_organization.id ? | ||
alliance.target_organization_id : alliance.source_organization_id | ||
end | ||
|
||
organizations = Organization.where(id: allied_org_ids + [current_organization.id]) | ||
organizations.order(:name) | ||
end | ||
|
||
def alliance_initiator?(alliance) | ||
alliance.source_organization_id == current_organization.id | ||
end | ||
|
||
def alliance_recipient(alliance) | ||
alliance_initiator?(alliance) ? alliance.target_organization : alliance.source_organization | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class OrganizationAlliance < ApplicationRecord | ||
belongs_to :source_organization, class_name: "Organization" | ||
belongs_to :target_organization, class_name: "Organization" | ||
|
||
enum status: { pending: 0, accepted: 1, rejected: 2 } | ||
|
||
validates :source_organization_id, presence: true | ||
validates :target_organization_id, presence: true | ||
validates :target_organization_id, uniqueness: { scope: :source_organization_id } | ||
validate :cannot_ally_with_self | ||
|
||
scope :pending, -> { where(status: "pending") } | ||
scope :accepted, -> { where(status: "accepted") } | ||
scope :rejected, -> { where(status: "rejected") } | ||
|
||
private | ||
|
||
def cannot_ally_with_self | ||
if source_organization_id == target_organization_id | ||
errors.add(:base, "Cannot create an alliance with yourself") | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<h1><%= t('organization_alliances.title') %></h1> | ||
|
||
<div class="row"> | ||
<div class="col-12 col-sm-12 col-md-12 col-lg-12"> | ||
<ul class="nav nav-pills actions-menu"> | ||
<li class="nav-item"> | ||
<%= link_to organization_alliances_path(status: 'pending'), class: "nav-link #{'active' if @status == 'pending'}" do %> | ||
<%= glyph :time %> | ||
<%= t('organization_alliances.status.pending') %> | ||
<% end %> | ||
</li> | ||
<li class="nav-item"> | ||
<%= link_to organization_alliances_path(status: 'accepted'), class: "nav-link #{'active' if @status == 'accepted'}" do %> | ||
<%= glyph :ok %> | ||
<%= t('organization_alliances.status.accepted') %> | ||
<% end %> | ||
</li> | ||
<li class="nav-item"> | ||
<%= link_to organization_alliances_path(status: 'rejected'), class: "nav-link #{'active' if @status == 'rejected'}" do %> | ||
<%= glyph :remove %> | ||
<%= t('organization_alliances.status.rejected') %> | ||
<% end %> | ||
</li> | ||
<li class="nav-item ms-auto"> | ||
<%= link_to organizations_path, class: "text-primary" do %> | ||
<%= glyph :search %> | ||
<%= t('organization_alliances.search_organizations') %> | ||
<% end %> | ||
</li> | ||
</ul> | ||
</div> | ||
</div> | ||
|
||
<div class="row"> | ||
<div class="col-md-12"> | ||
<div class="card"> | ||
<div class="card-body table-responsive"> | ||
<table class="table table-hover table-sm"> | ||
<thead> | ||
<tr> | ||
<th><%= t('organization_alliances.organization') %></th> | ||
<th><%= t('organization_alliances.city') %></th> | ||
<th><%= t('organization_alliances.members') %></th> | ||
<th><%= t('organization_alliances.type') %></th> | ||
<% if @status != 'rejected' %> | ||
<th><%= t('organization_alliances.actions') %></th> | ||
<% end %> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% @alliances.each do |alliance| %> | ||
<% other_org = alliance_recipient(alliance) %> | ||
<tr> | ||
<td><%= link_to other_org.name, other_org %></td> | ||
<td><%= other_org.city %></td> | ||
<td><%= other_org.members.count %></td> | ||
<td> | ||
<%= t("organization_alliances.#{alliance_initiator?(alliance) ? 'sent' : 'received'}") %> | ||
</td> | ||
<% if @status == 'pending' %> | ||
<td> | ||
<% if alliance_initiator?(alliance) %> | ||
<%= link_to t('organization_alliances.cancel_request'), | ||
organization_alliance_path(alliance), | ||
method: :delete, | ||
class: 'btn btn-danger', | ||
data: { confirm: t('organization_alliances.confirm_cancel') } %> | ||
<% else %> | ||
<div> | ||
<%= link_to t('organization_alliances.accept'), | ||
organization_alliance_path(alliance, status: 'accepted'), | ||
method: :put, | ||
class: 'btn btn-primary' %> | ||
<%= link_to t('organization_alliances.reject'), | ||
organization_alliance_path(alliance, status: 'rejected'), | ||
method: :put, | ||
class: 'btn btn-danger' %> | ||
</div> | ||
<% end %> | ||
</td> | ||
<% elsif @status == 'accepted' %> | ||
<td> | ||
<%= link_to t('organization_alliances.end_alliance'), | ||
organization_alliance_path(alliance), | ||
method: :delete, | ||
class: 'btn btn-danger', | ||
data: { confirm: t('organization_alliances.confirm_end') } %> | ||
</td> | ||
<% end %> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<% if current_user&.manages?(current_organization) && organization != current_organization %> | ||
<% alliance = current_organization.alliance_with(organization) %> | ||
<% if alliance.nil? %> | ||
<%= link_to t('organization_alliances.request_alliance'), | ||
organization_alliances_path(organization_alliance: { target_organization_id: organization.id }), | ||
method: :post, | ||
class: 'btn btn-secondary', | ||
aria: { label: t('organization_alliances.request_alliance_for', org: organization.name) } %> | ||
<% elsif alliance.pending? %> | ||
<span class="badge rounded-pill bg-secondary"><%= t('organization_alliances.pending') %></span> | ||
<% elsif alliance.accepted? %> | ||
<span class="badge rounded-pill bg-primary"><%= t('organization_alliances.active') %></span> | ||
<% elsif alliance.rejected? %> | ||
<span class="badge rounded-pill bg-danger"><%= t('organization_alliances.rejected') %></span> | ||
<% end %> | ||
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
col-12 is enough, since you are not giving other sizes