Skip to content

Commit 7e69876

Browse files
committed
Filter offer by organizations
1 parent f6616c9 commit 7e69876

File tree

9 files changed

+95
-13
lines changed

9 files changed

+95
-13
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// app/assets/javascripts/application/organization_filter.js
2+
$(function() {
3+
// Manejar cambios en las casillas de organizaciones
4+
$(document).on('change', '.organization-checkbox', function() {
5+
// Obtener valores actuales de la URL
6+
var searchParams = new URLSearchParams(window.location.search);
7+
var cat = searchParams.get('cat');
8+
var q = searchParams.get('q');
9+
var tag = searchParams.get('tag');
10+
11+
var form = $(this).closest('form');
12+
13+
// Mantener parámetros actuales
14+
if (cat) {
15+
if (form.find('input[name="cat"]').length === 0) {
16+
form.append('<input type="hidden" name="cat" value="' + cat + '">');
17+
}
18+
}
19+
20+
if (q) {
21+
form.find('input[name="q"]').val(q);
22+
}
23+
24+
if (tag) {
25+
if (form.find('input[name="tag"]').length === 0) {
26+
form.append('<input type="hidden" name="tag" value="' + tag + '">');
27+
}
28+
}
29+
30+
// Enviar el formulario
31+
form.submit();
32+
});
33+
});

app/controllers/posts_controller.rb

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

9-
if current_organization.present?
10-
context = context.where(
11-
organization_id: current_organization.id
12-
)
13-
end
14-
159
posts = apply_scopes(context)
1610
posts = posts.search_by_query(params[:q]) if params[:q].present?
1711
posts = posts.page(params[:page]).per(25)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module OrganizationsHelper
2+
def filterable_organizations
3+
Organization.all.order(:name)
4+
end
5+
6+
def allied_organizations
7+
return [] unless current_organization
8+
9+
allied_org_ids = current_organization.accepted_alliances.map do |alliance|
10+
alliance.source_organization_id == current_organization.id ?
11+
alliance.target_organization_id : alliance.source_organization_id
12+
end
13+
14+
organizations = Organization.where(id: allied_org_ids + [current_organization.id])
15+
organizations.order(:name)
16+
end
17+
end

app/models/post.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class Post < ApplicationRecord
3030
scope :by_organization, ->(org) {
3131
where(organization_id: org) if org
3232
}
33+
scope :by_organizations, ->(org_ids) {
34+
where(organization_id: org_ids) if org_ids.present?
35+
}
3336
scope :of_active_members, -> {
3437
with_member.where("members.active")
3538
}

app/views/inquiries/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<%= render "shared/post_filters", base_path: inquiries_path %>
1212

1313
<div class="col-md-4">
14-
<% if current_user && current_organization && !params[:org] %>
14+
<% if current_user && current_organization %>
1515
<ul class="nav navbar-nav text-end">
1616
<li class="nav-item">
1717
<%= link_to new_inquiry_path, class: 'nav-link text-primary' do %>

app/views/offers/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<%= render "shared/post_filters", base_path: offers_path %>
1212

1313
<div class="col-md-4">
14-
<% if current_user && current_organization && !params[:org] %>
14+
<% if current_user && current_organization %>
1515
<ul class="nav navbar-nav text-end">
1616
<li class="nav-item">
1717
<%= link_to new_offer_path, class: 'nav-link text-primary' do %>

app/views/shared/_post_filters.html.erb

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<% @category = Category.find_by(id: params[:cat]) %>
2+
<% selected_org = Organization.find_by(id: params[:org]) %>
23

34
<div class="col-md-8">
45
<form action="<%= base_path %>"
@@ -23,12 +24,18 @@
2324
</a>
2425
<ul class="dropdown-menu" role="menu">
2526
<li>
26-
<%= link_to #{t('global.all')}", base_path, class: "dropdown-item" %>
27+
<%= link_to #{t('global.all')}", base_path + (params[:org] ? "?org=#{params[:org]}" : ""), class: "dropdown-item" %>
2728
</li>
2829
<% all_categories.each do |c| %>
2930
<% next if c == @category %>
3031
<li>
31-
<%= link_to "#{base_path}?cat=#{c.id}", class: "dropdown-item" do %>
32+
<%
33+
query_params = {}
34+
query_params[:cat] = c.id
35+
query_params[:org] = params[:org] if params[:org].present?
36+
link_path = "#{base_path}?#{query_params.to_query}"
37+
%>
38+
<%= link_to link_path, class: "dropdown-item" do %>
3239
<%= category_icon(c) %>
3340
<%= c.name %>
3441
<% end %>
@@ -37,5 +44,31 @@
3744
</ul>
3845
</li>
3946
</ul>
47+
<ul class="nav navbar-nav d-none d-sm-flex">
48+
<li class="dropdown nav-item">
49+
<a class="dropdown-toggle nav-link text-primary" href="#" data-bs-toggle="dropdown" role="button" aria-expanded="false">
50+
<%= selected_org ? selected_org.name : t("activerecord.models.organization.other") %>
51+
</a>
52+
<ul class="dropdown-menu" role="menu">
53+
<li>
54+
<%= link_to #{t('global.all')}", base_path + (params[:cat] ? "?cat=#{params[:cat]}" : ""), class: "dropdown-item" %>
55+
</li>
56+
<% allied_organizations.each do |org| %>
57+
<% next if org.id.to_s == params[:org] %>
58+
<li>
59+
<%
60+
query_params = {}
61+
query_params[:org] = org.id
62+
query_params[:cat] = params[:cat] if params[:cat].present?
63+
link_path = "#{base_path}?#{query_params.to_query}"
64+
%>
65+
<%= link_to link_path, class: "dropdown-item" do %>
66+
<%= org.name %>
67+
<% end %>
68+
</li>
69+
<% end %>
70+
</ul>
71+
</li>
72+
</ul>
4073
</form>
41-
</div>
74+
</div>

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ en:
304304
table:
305305
actions: Actions
306306
to: To
307+
filter_by_organizations: "Filter by organizations"
307308
inquiries:
308309
edit:
309310
submit: Change request

config/locales/es.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ es:
101101
one: Oferta
102102
other: Ofertas
103103
organization:
104-
one: Banco de Tiempo
105-
other: Bancos de Tiempo
104+
one: Organización
105+
other: Organizaciones
106106
post:
107107
one: Anuncio
108108
other: Anuncios
@@ -304,6 +304,7 @@ es:
304304
table:
305305
actions: Acciones
306306
to: a
307+
filter_by_organizations: "Filtrar por organizaciones"
307308
inquiries:
308309
edit:
309310
submit: Cambiar demanda

0 commit comments

Comments
 (0)