Skip to content

Commit

Permalink
Fix state filtering in ProposalsController (#36)
Browse files Browse the repository at this point in the history
* Fix state filtering in 0.27

* Fix failing spec
  • Loading branch information
alecslupu authored Mar 26, 2024
1 parent 4d08345 commit 4e8f8e9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions lib/decidim/custom_proposal_states/overrides/proposal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?

Expand Down Expand Up @@ -75,6 +100,8 @@ def process_amendment_state_change!
end
end
end
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/proposals/system/amendable/amendment_diff_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 4e8f8e9

Please sign in to comment.