Skip to content

Commit 615eb44

Browse files
committed
Correct comments #790
1 parent 66663da commit 615eb44

File tree

11 files changed

+42
-67
lines changed

11 files changed

+42
-67
lines changed

app/controllers/organization_alliances_controller.rb

+9-14
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ def index
99

1010
@alliances = case @status
1111
when "pending"
12-
current_organization.pending_sent_alliances.includes(:source_organization, :target_organization) +
13-
current_organization.pending_received_alliances.includes(:source_organization, :target_organization)
12+
current_organization.pending_alliances.includes(:source_organization, :target_organization)
1413
when "accepted"
1514
current_organization.accepted_alliances.includes(:source_organization, :target_organization)
1615
when "rejected"
@@ -21,24 +20,22 @@ def index
2120
end
2221

2322
def create
24-
@alliance = OrganizationAlliance.new(
23+
alliance = OrganizationAlliance.new(
2524
source_organization: current_organization,
26-
target_organization_id: params[:organization_alliance][:target_organization_id],
25+
target_organization_id: alliance_params[:target_organization_id],
2726
status: "pending"
2827
)
2928

30-
if @alliance.save
29+
if alliance.save
3130
flash[:notice] = t("organization_alliances.created")
3231
else
33-
flash[:error] = @alliance.errors.full_messages.to_sentence
32+
flash[:error] = alliance.errors.full_messages.to_sentence
3433
end
3534

3635
redirect_back fallback_location: organizations_path
3736
end
3837

3938
def update
40-
authorize @alliance
41-
4239
if @alliance.update(status: params[:status])
4340
flash[:notice] = t("organization_alliances.updated")
4441
else
@@ -49,8 +46,6 @@ def update
4946
end
5047

5148
def destroy
52-
authorize @alliance
53-
5449
if @alliance.destroy
5550
flash[:notice] = t("organization_alliances.destroyed")
5651
else
@@ -67,10 +62,10 @@ def find_alliance
6762
end
6863

6964
def authorize_admin
70-
unless current_user.manages?(current_organization)
71-
flash[:error] = t("organization_alliances.not_authorized")
72-
redirect_to root_path
73-
end
65+
return if current_user.manages?(current_organization)
66+
67+
flash[:error] = t("organization_alliances.not_authorized")
68+
redirect_to root_path
7469
end
7570

7671
def alliance_params

app/helpers/organizations_helper.rb

+8
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,12 @@ def allied_organizations
1414
organizations = Organization.where(id: allied_org_ids + [current_organization.id])
1515
organizations.order(:name)
1616
end
17+
18+
def alliance_initiator?(alliance)
19+
alliance.source_organization_id == current_organization.id
20+
end
21+
22+
def alliance_recipient(alliance)
23+
alliance_initiator?(alliance) ? alliance.target_organization : alliance.source_organization
24+
end
1725
end

app/models/organization.rb

+10-19
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class Organization < ApplicationRecord
2424
has_many :inquiries
2525
has_many :documents, as: :documentable, dependent: :destroy
2626
has_many :petitions, dependent: :delete_all
27-
has_many :source_alliances, class_name: "OrganizationAlliance", foreign_key: "source_organization_id", dependent: :destroy
28-
has_many :target_alliances, class_name: "OrganizationAlliance", foreign_key: "target_organization_id", dependent: :destroy
27+
has_many :initiated_alliances, class_name: "OrganizationAlliance", foreign_key: "source_organization_id", dependent: :destroy
28+
has_many :received_alliances, class_name: "OrganizationAlliance", foreign_key: "target_organization_id", dependent: :destroy
2929

3030
validates :name, presence: true, uniqueness: true
3131

@@ -54,39 +54,30 @@ def display_name_with_uid
5454
self
5555
end
5656

57-
# Returns the id to be displayed in the :new transfer page with the given
58-
# destination_accountable
59-
#
60-
# @params destination_accountable [Organization | Object] target of a transfer
61-
# @return [Integer | String]
6257
def display_id
6358
account.accountable_id
6459
end
6560

6661
def alliance_with(organization)
67-
source_alliances.find_by(target_organization: organization) ||
68-
target_alliances.find_by(source_organization: organization)
62+
initiated_alliances.find_by(target_organization: organization) ||
63+
received_alliances.find_by(source_organization: organization)
6964
end
7065

71-
def pending_sent_alliances
72-
source_alliances.pending
73-
end
74-
75-
def pending_received_alliances
76-
target_alliances.pending
66+
def pending_alliances
67+
initiated_alliances.pending.or(received_alliances.pending)
7768
end
7869

7970
def accepted_alliances
80-
source_alliances.accepted.or(target_alliances.accepted)
71+
initiated_alliances.accepted.or(received_alliances.accepted)
8172
end
8273

8374
def rejected_alliances
84-
source_alliances.rejected.or(target_alliances.rejected)
75+
initiated_alliances.rejected.or(received_alliances.rejected)
8576
end
8677

8778
def allied_organizations
88-
source_org_ids = source_alliances.accepted.pluck(:target_organization_id)
89-
target_org_ids = target_alliances.accepted.pluck(:source_organization_id)
79+
source_org_ids = initiated_alliances.accepted.pluck(:target_organization_id)
80+
target_org_ids = received_alliances.accepted.pluck(:source_organization_id)
9081
Organization.where(id: source_org_ids + target_org_ids)
9182
end
9283

app/policies/organization_alliance_policy.rb

-11
This file was deleted.

app/views/organization_alliances/index.html.erb

+4-9
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,17 @@
4949
</thead>
5050
<tbody>
5151
<% @alliances.each do |alliance| %>
52-
<% is_sender = (alliance.source_organization_id == current_organization.id) %>
53-
<% other_org = is_sender ? alliance.target_organization : alliance.source_organization %>
52+
<% other_org = alliance_recipient(alliance) %>
5453
<tr>
5554
<td><%= link_to other_org.name, other_org %></td>
5655
<td><%= other_org.city %></td>
5756
<td><%= other_org.members.count %></td>
5857
<td>
59-
<% if is_sender %>
60-
<%= t('organization_alliances.sent') %>
61-
<% else %>
62-
<%= t('organization_alliances.received') %>
63-
<% end %>
58+
<%= t("organization_alliances.#{alliance_initiator?(alliance) ? 'sent' : 'received'}") %>
6459
</td>
6560
<% if @status == 'pending' %>
6661
<td>
67-
<% if is_sender %>
62+
<% if alliance_initiator?(alliance) %>
6863
<%= link_to t('organization_alliances.cancel_request'),
6964
organization_alliance_path(alliance),
7065
method: :delete,
@@ -99,4 +94,4 @@
9994
</div>
10095
</div>
10196
</div>
102-
</div>
97+
</div>

app/views/organizations/_alliance_button.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
<% elsif alliance.rejected? %>
1414
<span class="badge rounded-pill bg-danger"><%= t('organization_alliances.rejected') %></span>
1515
<% end %>
16-
<% end %>
16+
<% end %>

app/views/organizations/_organizations_row.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
<%= render "organizations/alliance_button", organization: org %>
1313
</td>
1414
<% end %>
15-
</tr>
15+
</tr>

app/views/organizations/index.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@
4040
<%= paginate @organizations %>
4141
</div>
4242
</div>
43-
</div>
43+
</div>

config/locales/en.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -621,4 +621,4 @@ en:
621621
last: Last
622622
next: Next
623623
previous: Previous
624-
truncate: Truncate
624+
truncate: Truncate

config/locales/es.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ es:
386386
actions: "Acciones"
387387
sent: "Enviadas"
388388
received: "Recibidas"
389-
pending: "Pending"
389+
pending: "Pendientes"
390390
active: "Activa"
391391
rejected: "Rechazada"
392392
request_alliance: "Solicitar alianza"
@@ -621,4 +621,4 @@ es:
621621
last: Ultima &raquo;
622622
next: Siguiente &rsaquo;
623623
previous: "&lsaquo; Anterior"
624-
truncate: "&hellip;"
624+
truncate: "&hellip;"

spec/models/organization_spec.rb

+5-8
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,11 @@
136136
)
137137
end
138138

139-
it "returns pending sent alliances" do
140-
expect(organization.pending_sent_alliances).to include(@pending_sent)
141-
expect(organization.pending_sent_alliances).not_to include(@pending_received)
142-
end
143-
144-
it "returns pending received alliances" do
145-
expect(organization.pending_received_alliances).to include(@pending_received)
146-
expect(organization.pending_received_alliances).not_to include(@pending_sent)
139+
it "returns pending alliances" do
140+
expect(organization.pending_alliances).to include(@pending_sent, @pending_received)
141+
expect(organization.pending_alliances).not_to include(
142+
@accepted_sent, @accepted_received, @rejected_sent, @rejected_received
143+
)
147144
end
148145

149146
it "returns accepted alliances" do

0 commit comments

Comments
 (0)