diff --git a/lib/decidim/custom_proposal_states/overrides/proposal.rb b/lib/decidim/custom_proposal_states/overrides/proposal.rb index c1f30db..75b87ea 100644 --- a/lib/decidim/custom_proposal_states/overrides/proposal.rb +++ b/lib/decidim/custom_proposal_states/overrides/proposal.rb @@ -4,6 +4,8 @@ module Decidim module CustomProposalStates module Overrides module Proposal + # rubocop:disable Metrics/CyclomaticComplexity + # rubocop:disable Metrics/PerceivedComplexity def self.prepended(base) base.class_eval do before_create :set_default_state @@ -41,6 +43,29 @@ def self.prepended(base) scope :withdrawn, -> { joins(:proposal_state).where(decidim_proposals_proposal_states: { token: :withdrawn }) } scope :except_withdrawn, -> { joins(:proposal_state).where.not(decidim_proposals_proposal_states: { token: :withdrawn }) } + scope :with_any_state, lambda { |*value_keys| + possible_scopes = [:state_not_published, :state_published] + custom_states = Decidim::CustomProposalStates::ProposalState.distinct.pluck(:token) + + search_values = value_keys.compact.compact_blank + + conditions = possible_scopes.map do |scope| + search_values.member?(scope.to_s) ? try(scope) : nil + end.compact + + additional_conditions = search_values & custom_states + conditions.push(state_published.only_status(additional_conditions)) if additional_conditions.any? + + return self unless conditions.any? + + scoped_query = where(id: conditions.shift) + conditions.each do |condition| + scoped_query = scoped_query.or(where(id: condition)) + end + + scoped_query + } + def set_default_state return if proposal_state.present? @@ -75,6 +100,8 @@ def process_amendment_state_change! end end end + # rubocop:enable Metrics/CyclomaticComplexity + # rubocop:enable Metrics/PerceivedComplexity end end end diff --git a/spec/proposals/system/amendable/amendment_diff_spec.rb b/spec/proposals/system/amendable/amendment_diff_spec.rb index 746f86e..22f6f6b 100644 --- a/spec/proposals/system/amendable/amendment_diff_spec.rb +++ b/spec/proposals/system/amendable/amendment_diff_spec.rb @@ -136,11 +136,11 @@ let(:user) { proposal.creator_author } before do + login_as user, scope: :user proposal.update(title: { en: "Updated long enough title" }, body: { en: "Updated one liner body" }) # The last version of the emendation should hold the amending attribute values. emendation.update(title: { en: "Amended long enough title" }, body: { en: "Amended one liner body" }) visit emendation_path - login_as user, scope: :user visit decidim.review_amend_path(amendment) end