Skip to content

Commit

Permalink
WIP apply ransack to challenges filter
Browse files Browse the repository at this point in the history
  • Loading branch information
tramuntanal committed Sep 6, 2023
1 parent e91ec59 commit 173981c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
18 changes: 9 additions & 9 deletions app/controllers/decidim/challenges/challenges_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ def challenge_scope

def default_filter_params
{
search_text: "",
category_id: default_filter_category_params,
state: %w(proposal execution finished),
scope_id: default_filter_scope_params,
related_to: "",
sdgs_codes: [],
search_text_cont: "",
with_any_category_id: default_filter_category_params,
with_any_state: %w(proposal execution finished),
with_any_scope_id: default_filter_scope_params,
with_related_to: "",
sdgs_codes_cont: [],
}
end

Expand All @@ -64,11 +64,11 @@ def default_filter_scope_params
end

def challenges
@challenges ||= paginate(search.result.published)
@challenges ||= paginate(search.result)
end

def search_klass
Decidim::Challenges::ChallengeSearch
def search_collection
::Decidim::Challenges::Challenge.published
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions app/models/decidim/challenges/challenge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Challenges
# The data store for a Challenge in the Decidim::Challenges component.
class Challenge < Decidim::ApplicationRecord
include Decidim::HasComponent
include Decidim::FilterableResource
include Decidim::Loggable
include Decidim::Publicable
include Decidim::Resourceable
Expand Down Expand Up @@ -42,6 +43,14 @@ class Challenge < Decidim::ApplicationRecord
scope :in_execution, -> { where(state: VALID_STATES.index(:execution)) }
scope :in_finished, -> { where(state: VALID_STATES.index(:finished)) }

scope :with_any_state, ->(*values) {
where(state: Array(values).map(&:to_sym) & VALID_STATES)
}

def self.ransackable_scopes(_auth_object = nil)
[:with_any_state]
end

searchable_fields({
scope_id: :decidim_scope_id,
participatory_space: :itself,
Expand Down
8 changes: 4 additions & 4 deletions app/views/decidim/challenges/challenges/_filters.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="filters__section">
<div class="filters__search">
<div class="input-group">
<%= form.search_field :search_text, label: false, class: "input-group-field", placeholder: t(".search") %>
<%= form.search_field :search_text_cont, label: false, class: "input-group-field", placeholder: t(".search") %>
<div class="input-group-button">
<button type="submit" class="button button--muted">
<%= icon "magnifying-glass", aria_label: t(".search") %>
Expand All @@ -12,14 +12,14 @@
</div>
</div>

<%= form.check_boxes_tree :state, filter_challenges_state_values, legend_title: t(".state") %>
<%= form.check_boxes_tree :with_any_state, filter_challenges_state_values, legend_title: t(".state") %>
<% if current_participatory_space.has_subscopes? %>
<%= form.check_boxes_tree :scope_id, filter_scopes_values, legend_title: t(".scope") %>
<%= form.check_boxes_tree :with_any_scope_id, filter_scopes_values, legend_title: t(".scope") %>
<% end %>
<% if current_component.categories.any? %>
<%= form.check_boxes_tree :category_id, filter_categories_values, legend_title: t(".category") %>
<%= form.check_boxes_tree :with_any_category_id, filter_categories_values, legend_title: t(".category") %>
<% end %>
<% if current_participatory_space.components.published.where(manifest_name: "sdgs").any? %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<legend class="mini-title"><%= t("activemodel.attributes.challenge.sdg") %></legend>
<button class="" data-open="sdgs-modal"><%= t(".select") %></button>
<% Decidim::Sdgs::Sdg::SDGS.each do |sdg_code| %>
<input type="hidden" name="filter[sdgs_codes][]" value="<%= form.object.sdgs_codes.include?(sdg_code.to_s) ? sdg_code : '' %>" data-value="<%= sdg_code %>">
<input type="hidden" name="filter[sdgs_codes_cont][]" value="<%= form.object.sdgs_codes_cont.include?(sdg_code.to_s) ? sdg_code : '' %>" data-value="<%= sdg_code %>">
<% end %>
</div>

0 comments on commit 173981c

Please sign in to comment.