-
Notifications
You must be signed in to change notification settings - Fork 15
Fix proposals order in index #931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Stef-Rousset
wants to merge
6
commits into
develop
Choose a base branch
from
fix/proposals_order_in_index
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+535
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2c98701
feat: add proposals controller extends to fix proposals order in index
Stef-Rousset c26cae4
test: add tests for proposal controller and system test for proposals…
Stef-Rousset 43c3666
style: refacto with rubocop
Stef-Rousset 882e2c5
test: update system test
Stef-Rousset bb60801
test: refacto with rubocop
Stef-Rousset 68b1d11
Merge branch 'develop' into fix/proposals_order_in_index
luciegrau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
lib/extends/controllers/decidim/proposals/proposals_controller_extends.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "active_support/concern" | ||
|
|
||
| module Decidim | ||
| module Proposals | ||
| module ProposalsControllerExtends | ||
| extend ActiveSupport::Concern | ||
|
|
||
| included do | ||
| def index | ||
| if component_settings.participatory_texts_enabled? | ||
| @proposals = Decidim::Proposals::Proposal | ||
| .where(component: current_component) | ||
| .published | ||
| .not_hidden | ||
| .only_amendables | ||
| .includes(:category, :scope, :attachments, :coauthorships) | ||
| .order(position: :asc) | ||
| render "decidim/proposals/proposals/participatory_texts/participatory_text" | ||
| else | ||
| @proposals = search.result | ||
| @proposals = reorder(@proposals) | ||
| ids = @proposals.ids | ||
| @proposals = Decidim::Proposals::Proposal.where(id: ids) | ||
| .order(Arel.sql("position(decidim_proposals_proposals.id::text in '#{ids.join(",")}')")) | ||
| .page(params[:page]) | ||
| .per(per_page) | ||
| @proposals = @proposals.includes(:component, :coauthorships, :attachments) | ||
|
|
||
| @voted_proposals = if current_user | ||
| ProposalVote.where( | ||
| author: current_user, | ||
| proposal: @proposals.pluck(:id) | ||
| ).pluck(:decidim_proposal_id) | ||
| else | ||
| [] | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| Decidim::Proposals::ProposalsController.include(Decidim::Proposals::ProposalsControllerExtends) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "spec_helper" | ||
|
|
||
| module Decidim | ||
| module Proposals | ||
| describe ProposalsController do | ||
| routes { Decidim::Proposals::Engine.routes } | ||
|
|
||
| let(:user) { create(:user, :confirmed, organization: component.organization) } | ||
|
|
||
| let(:proposal_params) do | ||
| { | ||
| component_id: component.id | ||
| } | ||
| end | ||
| let(:params) { { proposal: proposal_params } } | ||
|
|
||
| before do | ||
| request.env["decidim.current_organization"] = component.organization | ||
| request.env["decidim.current_participatory_space"] = component.participatory_space | ||
| request.env["decidim.current_component"] = component | ||
| stub_const("Decidim::Paginable::OPTIONS", [100]) | ||
| end | ||
|
|
||
| describe "GET index" do | ||
| context "when participatory texts are disabled" do | ||
| let(:component) { create(:proposal_component, :with_geocoding_enabled) } | ||
|
|
||
| it "sorts proposals by search defaults" do | ||
| get :index | ||
| expect(response).to have_http_status(:ok) | ||
| expect(subject).to render_template(:index) | ||
| expect(assigns(:proposals).order_values).to eq(["position(decidim_proposals_proposals.id::text in '')"]) | ||
| end | ||
|
|
||
| it "sets two different collections" do | ||
| geocoded_proposals = create_list(:proposal, 10, component:, latitude: 1.1, longitude: 2.2) | ||
| non_geocoded_proposals = create_list(:proposal, 2, component:, latitude: nil, longitude: nil) | ||
|
|
||
| get :index | ||
| expect(response).to have_http_status(:ok) | ||
| expect(subject).to render_template(:index) | ||
|
|
||
| expect(assigns(:proposals).count).to eq 12 | ||
| expect(assigns(:proposals)).to match_array(geocoded_proposals + non_geocoded_proposals) | ||
| end | ||
| end | ||
|
|
||
| context "when participatory texts are enabled" do | ||
| let(:component) { create(:proposal_component, :with_participatory_texts_enabled) } | ||
|
|
||
| it "sorts proposals by position" do | ||
| get :index | ||
| expect(response).to have_http_status(:ok) | ||
| expect(subject).to render_template(:participatory_text) | ||
| expect(assigns(:proposals).order_values.first.expr.name).to eq("position") | ||
| end | ||
|
|
||
| context "when emendations exist" do | ||
| let!(:amendable) { create(:proposal, component:) } | ||
| let!(:emendation) { create(:proposal, component:) } | ||
| let!(:amendment) { create(:amendment, amendable:, emendation:, state: "accepted") } | ||
|
|
||
| it "does not include emendations" do | ||
| get :index | ||
| expect(response).to have_http_status(:ok) | ||
| emendations = assigns(:proposals).select(&:emendation?) | ||
| expect(emendations).to be_empty | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String interpolation in Arel.sql() bypasses ActiveRecord's SQL injection protections.
Even though
idsis safe here, this pattern could create risk if refactored in the future.More importantly, I'm trying to understand the flow here:
L22 -> @proposals = search.result
L23 -> @proposals = reorder(@proposals)
L24 -> ids = @proposals.ids
L25-28 -> @proposals = Proposal.where(id: ids).order(Arel.sql(...))
Why do we execute the query to get ids (L24), then re-query the same proposals (L25-28) ?
Is this to preserve a specific ordering that reorder() doesn't handle ? Or could we
apply the ordering directly to the search.result relation ?
I might be missing something about how reorder() works here, I just want to be sure we're not making "useless requests" but the code seems good tho, well done !