diff --git a/app/commands/decidim/challenges/invalid_broadcast_error.rb b/app/commands/decidim/challenges/invalid_broadcast_error.rb new file mode 100644 index 00000000..24a57b2d --- /dev/null +++ b/app/commands/decidim/challenges/invalid_broadcast_error.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Decidim + module Challenges + class InvalidBroadcastError < StandardError + end + end +end diff --git a/app/commands/decidim/challenges/invalid_form_broadcast_error.rb b/app/commands/decidim/challenges/invalid_form_broadcast_error.rb new file mode 100644 index 00000000..8260187e --- /dev/null +++ b/app/commands/decidim/challenges/invalid_form_broadcast_error.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Decidim + module Challenges + class InvalidFormBroadcastError < StandardError + end + end +end diff --git a/app/controllers/decidim/problems/problems_controller.rb b/app/controllers/decidim/problems/problems_controller.rb index e7f691a7..cfbd3f06 100644 --- a/app/controllers/decidim/problems/problems_controller.rb +++ b/app/controllers/decidim/problems/problems_controller.rb @@ -36,14 +36,14 @@ def challenge_scope def default_filter_params { - search_text: "", - category_id: default_filter_category_params, - state: %w(proposal execution finished), - sectorial_scope_id: default_filter_scope_params, - technological_scope_id: default_filter_scope_params, - territorial_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_sectorial_scope_id: default_filter_scope_params, + with_any_technological_scope_id: default_filter_scope_params, + with_any_territorial_scope_id: default_filter_scope_params, + with_related_to: "", + sdgs_codes_cont: [], } end @@ -64,11 +64,11 @@ def default_filter_scope_params end def problems - @problems ||= paginate(search.result.published) + @problems ||= paginate(search.result) end - def search_klass - Decidim::Problems::ProblemSearch + def search_collection + ::Decidim::Problems::Problem.published end end end diff --git a/app/controllers/decidim/solutions/solutions_controller.rb b/app/controllers/decidim/solutions/solutions_controller.rb index 4263873f..68ff8149 100644 --- a/app/controllers/decidim/solutions/solutions_controller.rb +++ b/app/controllers/decidim/solutions/solutions_controller.rb @@ -36,11 +36,11 @@ def show def default_filter_params { - search_text: "", - category_id: default_filter_category_params, - territorial_scope_id: default_filter_scope_params, - related_to: "", - sdgs_codes: [], + search_text_cont: "", + with_any_category_id: default_filter_category_params, + with_any_territorial_scope_id: default_filter_scope_params, + with_related_to: "", + sdgs_codes_cont: [], } end @@ -61,7 +61,7 @@ def default_filter_scope_params end def solutions - @solutions ||= paginate(search.result.published) + @solutions ||= paginate(search.result) end def solution @@ -89,8 +89,8 @@ def technological_scope @technological_scope ||= current_organization.scopes.find_by(id: @solution.problem.decidim_technological_scope_id) end - def search_klass - Decidim::Solutions::SolutionSearch + def search_collection + ::Decidim::Solutions::Solution.published end end end diff --git a/app/models/decidim/challenges/challenge.rb b/app/models/decidim/challenges/challenge.rb index 1301600c..6508321e 100644 --- a/app/models/decidim/challenges/challenge.rb +++ b/app/models/decidim/challenges/challenge.rb @@ -43,12 +43,20 @@ 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) { + scope :with_any_state, lambda { |*values| where(state: Array(values).map(&:to_sym) & VALID_STATES) } + scope :with_search_text, lambda { |search_text| + where("title ->> '#{I18n.locale}' ILIKE ?", "%#{search_text}%") + } + + scope :with_sdgs_codes, lambda { |sdgs_codes| + where(sdg_code: sdgs_codes) + } + def self.ransackable_scopes(_auth_object = nil) - [:with_any_state] + [:with_any_state, :with_search_text, :with_sdgs_codes] end searchable_fields({ diff --git a/app/models/decidim/problems/problem.rb b/app/models/decidim/problems/problem.rb index 7a629731..b082ae92 100644 --- a/app/models/decidim/problems/problem.rb +++ b/app/models/decidim/problems/problem.rb @@ -5,6 +5,7 @@ module Problems # The data store for a Problem in the Decidim::Problems component. class Problem < Decidim::ApplicationRecord include Decidim::HasComponent + include Decidim::FilterableResource include Decidim::Loggable include Decidim::Publicable include Decidim::Resourceable @@ -37,6 +38,64 @@ class Problem < Decidim::ApplicationRecord scope :in_execution, -> { where(state: VALID_STATES.index(:execution)) } scope :in_finished, -> { where(state: VALID_STATES.index(:finished)) } + scope :with_any_state, lambda { |*values| + where(state: Array(values).map(&:to_sym) & VALID_STATES) + } + + scope :with_search_text, lambda { |search_text| + where("title ->> '#{I18n.locale}' ILIKE ?", "%#{search_text}%") + } + + scope :with_sdgs_codes, lambda { |sdgs_codes| + joins(:challenge).where("decidim_challenges_challenges" => { sdg_code: sdgs_codes }) + } + + scope :with_any_sectorial_scope_id, lambda { |*sectorial_scope_id| + if sectorial_scope_id.include?("all") + all + else + clean_scope_ids = sectorial_scope_id + + conditions = [] + conditions << "#{model_name.plural}.decidim_sectorial_scope_id IS NULL" if clean_scope_ids.delete("global") + conditions.concat(["? = ANY(decidim_scopes.part_of)"] * clean_scope_ids.count) if clean_scope_ids.any? + + includes(:sectorial_scope).references(:decidim_scopes).where(conditions.join(" OR "), *clean_scope_ids.map(&:to_i)) + end + } + + scope :with_any_technological_scope_id, lambda { |*technological_scope_id| + if technological_scope_id.include?("all") + all + else + clean_scope_ids = technological_scope_id + + conditions = [] + conditions << "#{model_name.plural}.decidim_technological_scope_id IS NULL" if clean_scope_ids.delete("global") + conditions.concat(["? = ANY(decidim_scopes.part_of)"] * clean_scope_ids.count) if clean_scope_ids.any? + + includes(:technological_scope).references(:decidim_scopes).where(conditions.join(" OR "), *clean_scope_ids.map(&:to_i)) + end + } + + scope :with_any_territorial_scope_id, lambda { |*territorial_scope_id| + if territorial_scope_id.include?("all") + all + else + clean_scope_ids = territorial_scope_id + + conditions = [] + conditions << "decidim_challenges_challenges.decidim_scope_id IS NULL" if clean_scope_ids.delete("global") + conditions.concat(["? = ANY(decidim_scopes.part_of)"] * clean_scope_ids.count) if clean_scope_ids.any? + + includes(challenge: :scope).references(:decidim_scopes).where(conditions.join(" OR "), *clean_scope_ids.map(&:to_i)) + end + } + + def self.ransackable_scopes(_auth_object = nil) + [:with_any_state, :with_search_text, :with_sdgs_codes, :with_any_sectorial_scope_id, :with_any_technological_scope_id, :with_any_territorial_scope_id] + end + searchable_fields({ scope_id: "decidim_sectorial_scope_id", participatory_space: :itself, diff --git a/app/models/decidim/solutions/solution.rb b/app/models/decidim/solutions/solution.rb index a924dd41..cb1ac533 100644 --- a/app/models/decidim/solutions/solution.rb +++ b/app/models/decidim/solutions/solution.rb @@ -5,6 +5,7 @@ module Solutions # The data store for a Solution in the Decidim::Solutions component. class Solution < Solutions::ApplicationRecord include Decidim::HasComponent + include Decidim::FilterableResource include Decidim::Loggable include Decidim::Publicable include Decidim::Resourceable @@ -20,6 +21,32 @@ class Solution < Solutions::ApplicationRecord scope :published, -> { where.not(published_at: nil) } + scope :with_search_text, lambda { |search_text| + where("title ->> '#{I18n.locale}' ILIKE ?", "%#{search_text}%") + } + + scope :with_sdgs_codes, lambda { |sdgs_codes| + joins(:challenge).where("decidim_challenges_challenges" => { sdg_code: sdgs_codes }) + } + + scope :with_any_territorial_scope_id, lambda { |*territorial_scope_id| + if territorial_scope_id.include?("all") + all + else + clean_scope_ids = territorial_scope_id + + conditions = [] + conditions << "decidim_challenges_challenges.decidim_scope_id IS NULL" if clean_scope_ids.delete("global") + conditions.concat(["? = ANY(decidim_scopes.part_of)"] * clean_scope_ids.count) if clean_scope_ids.any? + + includes(problem: { challenge: :scope }).references(:decidim_scopes).where(conditions.join(" OR "), *clean_scope_ids.map(&:to_i)) + end + } + + def self.ransackable_scopes(_auth_object = nil) + [:with_search_text, :with_sdgs_codes, :with_any_territorial_scope_id] + end + searchable_fields({ participatory_space: :itself, A: :title, diff --git a/app/views/decidim/problems/problems/_filters.html.erb b/app/views/decidim/problems/problems/_filters.html.erb index 5e13b8fe..7d2747b2 100644 --- a/app/views/decidim/problems/problems/_filters.html.erb +++ b/app/views/decidim/problems/problems/_filters.html.erb @@ -2,7 +2,7 @@