Skip to content

Commit 66663da

Browse files
committed
refactor code
1 parent 08a2711 commit 66663da

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

app/controllers/posts_controller.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ class PostsController < ApplicationController
66
def index
77
context = model.active.of_active_members
88

9+
if current_user.present? && current_organization.present?
10+
if params[:show_allied].present?
11+
allied_org_ids = current_organization.allied_organizations.pluck(:id)
12+
org_ids = [current_organization.id] + allied_org_ids
13+
context = context.by_organizations(org_ids)
14+
elsif !params[:org].present?
15+
context = context.by_organization(current_organization.id)
16+
end
17+
end
18+
919
posts = apply_scopes(context)
1020
posts = posts.search_by_query(params[:q]) if params[:q].present?
1121
posts = posts.page(params[:page]).per(25)
@@ -92,15 +102,6 @@ def post_params
92102
end
93103
end
94104

95-
# TODO: remove this horrible hack ASAP
96-
#
97-
# This hack set the current organization to the post's
98-
# organization, both in session and controller instance variable.
99-
#
100-
# Before changing the current organization it's important to check that
101-
# the current_user is an active member of the organization.
102-
#
103-
# @param organization [Organization]
104105
def update_current_organization!(organization)
105106
return unless current_user && current_user.active?(organization)
106107

app/models/organization.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ def rejected_alliances
8484
source_alliances.rejected.or(target_alliances.rejected)
8585
end
8686

87+
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)
90+
Organization.where(id: source_org_ids + target_org_ids)
91+
end
92+
8793
def ensure_reg_number_seq!
8894
update_column(:reg_number_seq, members.maximum(:member_uid))
8995
end

app/views/shared/_post_filters.html.erb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@
5151
</a>
5252
<ul class="dropdown-menu" role="menu">
5353
<li>
54-
<%= link_to #{t('global.all')}", base_path + (params[:cat] ? "?cat=#{params[:cat]}" : ""), class: "dropdown-item" %>
54+
<%
55+
query_params = {}
56+
query_params[:show_allied] = true
57+
query_params[:cat] = params[:cat] if params[:cat].present?
58+
link_path = "#{base_path}?#{query_params.to_query}"
59+
%>
60+
<%= link_to #{t('global.all')}", link_path, class: "dropdown-item" %>
5561
</li>
5662
<% allied_organizations.each do |org| %>
5763
<% next if org.id.to_s == params[:org] %>

0 commit comments

Comments
 (0)