Skip to content

Organization-based filtering for posts #791

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
wants to merge 9 commits into
base: acceptance-between-administrators
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions app/assets/javascripts/application/organizations_filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
$(function() {
$(document).on('change', '.organization-checkbox', function() {
var searchParams = new URLSearchParams(window.location.search);
var cat = searchParams.get('cat');
var q = searchParams.get('q');
var tag = searchParams.get('tag');

var form = $(this).closest('form');

if (cat) {
if (form.find('input[name="cat"]').length === 0) {
form.append('<input type="hidden" name="cat" value="' + cat + '">');
}
}

if (q) {
form.find('input[name="q"]').val(q);
}

if (tag) {
if (form.find('input[name="tag"]').length === 0) {
form.append('<input type="hidden" name="tag" value="' + tag + '">');
}
}

form.submit();
});
});
23 changes: 9 additions & 14 deletions app/controllers/organization_alliances_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ def index

@alliances = case @status
when "pending"
current_organization.pending_sent_alliances.includes(:source_organization, :target_organization) +
current_organization.pending_received_alliances.includes(:source_organization, :target_organization)
current_organization.pending_alliances.includes(:source_organization, :target_organization)
when "accepted"
current_organization.accepted_alliances.includes(:source_organization, :target_organization)
when "rejected"
Expand All @@ -21,24 +20,22 @@ def index
end

def create
@alliance = OrganizationAlliance.new(
alliance = OrganizationAlliance.new(
source_organization: current_organization,
target_organization_id: params[:organization_alliance][:target_organization_id],
target_organization_id: alliance_params[:target_organization_id],
status: "pending"
)

if @alliance.save
if alliance.save
flash[:notice] = t("organization_alliances.created")
else
flash[:error] = @alliance.errors.full_messages.to_sentence
flash[:error] = alliance.errors.full_messages.to_sentence
end

redirect_back fallback_location: organizations_path
end

def update
authorize @alliance

if @alliance.update(status: params[:status])
flash[:notice] = t("organization_alliances.updated")
else
Expand All @@ -49,8 +46,6 @@ def update
end

def destroy
authorize @alliance

if @alliance.destroy
flash[:notice] = t("organization_alliances.destroyed")
else
Expand All @@ -67,10 +62,10 @@ def find_alliance
end

def authorize_admin
unless current_user.manages?(current_organization)
flash[:error] = t("organization_alliances.not_authorized")
redirect_to root_path
end
return if current_user.manages?(current_organization)

flash[:error] = t("organization_alliances.not_authorized")
redirect_to root_path
end

def alliance_params
Expand Down
21 changes: 8 additions & 13 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ class PostsController < ApplicationController
def index
context = model.active.of_active_members

if current_organization.present?
context = context.where(
organization_id: current_organization.id
)
if current_user.present? && current_organization.present?
if params[:show_allied].present?
allied_org_ids = current_organization.allied_organizations.pluck(:id)
org_ids = [current_organization.id] + allied_org_ids
context = context.by_organizations(org_ids)
elsif !params[:org].present?
context = context.by_organization(current_organization.id)
end
end

posts = apply_scopes(context)
Expand Down Expand Up @@ -98,15 +102,6 @@ def post_params
end
end

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

Expand Down
25 changes: 25 additions & 0 deletions app/helpers/organizations_helper.rb
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
31 changes: 14 additions & 17 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class Organization < ApplicationRecord
has_many :inquiries
has_many :documents, as: :documentable, dependent: :destroy
has_many :petitions, dependent: :delete_all
has_many :source_alliances, class_name: "OrganizationAlliance", foreign_key: "source_organization_id", dependent: :destroy
has_many :target_alliances, class_name: "OrganizationAlliance", foreign_key: "target_organization_id", dependent: :destroy
has_many :initiated_alliances, class_name: "OrganizationAlliance", foreign_key: "source_organization_id", dependent: :destroy
has_many :received_alliances, class_name: "OrganizationAlliance", foreign_key: "target_organization_id", dependent: :destroy

validates :name, presence: true, uniqueness: true

Expand Down Expand Up @@ -54,34 +54,31 @@ def display_name_with_uid
self
end

# Returns the id to be displayed in the :new transfer page with the given
# destination_accountable
#
# @params destination_accountable [Organization | Object] target of a transfer
# @return [Integer | String]
def display_id
account.accountable_id
end

def alliance_with(organization)
source_alliances.find_by(target_organization: organization) ||
target_alliances.find_by(source_organization: organization)
initiated_alliances.find_by(target_organization: organization) ||
received_alliances.find_by(source_organization: organization)
end

def pending_sent_alliances
source_alliances.pending
end

def pending_received_alliances
target_alliances.pending
def pending_alliances
initiated_alliances.pending.or(received_alliances.pending)
end

def accepted_alliances
source_alliances.accepted.or(target_alliances.accepted)
initiated_alliances.accepted.or(received_alliances.accepted)
end

def rejected_alliances
source_alliances.rejected.or(target_alliances.rejected)
initiated_alliances.rejected.or(received_alliances.rejected)
end

def allied_organizations
source_org_ids = initiated_alliances.accepted.pluck(:target_organization_id)
target_org_ids = received_alliances.accepted.pluck(:source_organization_id)
Organization.where(id: source_org_ids + target_org_ids)
end

def ensure_reg_number_seq!
Expand Down
3 changes: 3 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Post < ApplicationRecord
scope :by_organization, ->(org) {
where(organization_id: org) if org
}
scope :by_organizations, ->(org_ids) {
where(organization_id: org_ids) if org_ids.present?
}
scope :of_active_members, -> {
with_member.where("members.active")
}
Expand Down
11 changes: 0 additions & 11 deletions app/policies/organization_alliance_policy.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/inquiries/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<%= render "shared/post_filters", base_path: inquiries_path %>

<div class="col-md-4">
<% if current_user && current_organization && !params[:org] %>
<% if current_user && current_organization %>
<ul class="nav navbar-nav text-end">
<li class="nav-item">
<%= link_to new_inquiry_path, class: 'nav-link text-primary' do %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/offers/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<%= render "shared/post_filters", base_path: offers_path %>

<div class="col-md-4">
<% if current_user && current_organization && !params[:org] %>
<% if current_user && current_organization %>
<ul class="nav navbar-nav text-end">
<li class="nav-item">
<%= link_to new_offer_path, class: 'nav-link text-primary' do %>
Expand Down
13 changes: 4 additions & 9 deletions app/views/organization_alliances/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,17 @@
</thead>
<tbody>
<% @alliances.each do |alliance| %>
<% is_sender = (alliance.source_organization_id == current_organization.id) %>
<% other_org = is_sender ? alliance.target_organization : alliance.source_organization %>
<% 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>
<% if is_sender %>
<%= t('organization_alliances.sent') %>
<% else %>
<%= t('organization_alliances.received') %>
<% end %>
<%= t("organization_alliances.#{alliance_initiator?(alliance) ? 'sent' : 'received'}") %>
</td>
<% if @status == 'pending' %>
<td>
<% if is_sender %>
<% if alliance_initiator?(alliance) %>
<%= link_to t('organization_alliances.cancel_request'),
organization_alliance_path(alliance),
method: :delete,
Expand Down Expand Up @@ -99,4 +94,4 @@
</div>
</div>
</div>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/organizations/_alliance_button.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
<% elsif alliance.rejected? %>
<span class="badge rounded-pill bg-danger"><%= t('organization_alliances.rejected') %></span>
<% end %>
<% end %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/organizations/_organizations_row.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
<%= render "organizations/alliance_button", organization: org %>
</td>
<% end %>
</tr>
</tr>
2 changes: 1 addition & 1 deletion app/views/organizations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@
<%= paginate @organizations %>
</div>
</div>
</div>
</div>
45 changes: 42 additions & 3 deletions app/views/shared/_post_filters.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<% @category = Category.find_by(id: params[:cat]) %>
<% selected_org = Organization.find_by(id: params[:org]) %>

<div class="col-md-8">
<form action="<%= base_path %>"
Expand All @@ -23,12 +24,18 @@
</a>
<ul class="dropdown-menu" role="menu">
<li>
<%= link_to "× #{t('global.all')}", base_path, class: "dropdown-item" %>
<%= link_to "× #{t('global.all')}", base_path + (params[:org] ? "?org=#{params[:org]}" : ""), class: "dropdown-item" %>
</li>
<% all_categories.each do |c| %>
<% next if c == @category %>
<li>
<%= link_to "#{base_path}?cat=#{c.id}", class: "dropdown-item" do %>
<%
query_params = {}
query_params[:cat] = c.id
query_params[:org] = params[:org] if params[:org].present?
link_path = "#{base_path}?#{query_params.to_query}"
%>
<%= link_to link_path, class: "dropdown-item" do %>
<%= category_icon(c) %>
<%= c.name %>
<% end %>
Expand All @@ -37,5 +44,37 @@
</ul>
</li>
</ul>
<ul class="nav navbar-nav d-none d-sm-flex">
<li class="dropdown nav-item">
<a class="dropdown-toggle nav-link text-primary" href="#" data-bs-toggle="dropdown" role="button" aria-expanded="false">
<%= selected_org ? selected_org.name : t("activerecord.models.organization.other") %>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<%
query_params = {}
query_params[:show_allied] = true
query_params[:cat] = params[:cat] if params[:cat].present?
link_path = "#{base_path}?#{query_params.to_query}"
%>
<%= link_to "× #{t('global.all')}", link_path, class: "dropdown-item" %>
</li>
<% allied_organizations.each do |org| %>
<% next if org.id.to_s == params[:org] %>
<li>
<%
query_params = {}
query_params[:org] = org.id
query_params[:cat] = params[:cat] if params[:cat].present?
link_path = "#{base_path}?#{query_params.to_query}"
%>
<%= link_to link_path, class: "dropdown-item" do %>
<%= org.name %>
<% end %>
</li>
<% end %>
</ul>
</li>
</ul>
</form>
</div>
</div>
7 changes: 4 additions & 3 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ en:
one: Offer
other: Offers
organization:
one: Time Bank
other: Time Banks
one: Organization
other: Organizations
post:
one: Post
other: Posts
Expand Down Expand Up @@ -304,6 +304,7 @@ en:
table:
actions: Actions
to: To
filter_by_organizations: "Filter by organizations"
inquiries:
edit:
submit: Change request
Expand Down Expand Up @@ -620,4 +621,4 @@ en:
last: Last
next: Next
previous: Previous
truncate: Truncate
truncate: Truncate
Loading