diff --git a/app/components/shared/in_favor_against_component.rb b/app/components/shared/in_favor_against_component.rb index 68e96e99b..c14f7008a 100644 --- a/app/components/shared/in_favor_against_component.rb +++ b/app/components/shared/in_favor_against_component.rb @@ -29,13 +29,10 @@ class Shared::InFavorAgainstComponent < ApplicationComponent def vote_in_favor_against_path(value) if user_already_voted_with(value) - remove_vote_path(value) + vote = Vote.find_by!(votable: votable, voter: current_user) + polymorphic_path(vote) else - if votable.class.name == "Debate" - debate_votes_path(votable, value: value) - else - legislation_process_proposal_votes_path(votable.process, votable, value: value) - end + polymorphic_path(Vote.new(votable: votable), value: value) end end @@ -43,15 +40,6 @@ class Shared::InFavorAgainstComponent < ApplicationComponent current_user&.voted_as_when_voted_for(votable) == parse_vote(value) end - def remove_vote_path(value) - vote = votable.votes_for.find_by!(voter: current_user) - if votable.class.name == "Debate" - debate_vote_path(votable, vote, value: value) - else - legislation_process_proposal_vote_path(votable.process, votable, vote, value: value) - end - end - def parse_vote(value) value == "yes" ? true : false end diff --git a/app/controllers/legislation/proposals/votes_controller.rb b/app/controllers/legislation/proposals/votes_controller.rb index e51d97db5..5f70a5b95 100644 --- a/app/controllers/legislation/proposals/votes_controller.rb +++ b/app/controllers/legislation/proposals/votes_controller.rb @@ -3,7 +3,9 @@ module Legislation class VotesController < ApplicationController before_action :authenticate_user! load_and_authorize_resource :process, class: "Legislation::Process" - load_and_authorize_resource :proposal, class: "Legislation::Proposal", through: :process + load_and_authorize_resource :proposal, class: "Legislation::Proposal", + through: :process, + id_param: "legislation_proposal_id" load_and_authorize_resource through: :proposal, through_association: :votes_for, only: :destroy def create diff --git a/config/routes/legislation.rb b/config/routes/legislation.rb index 9b3f1a0f9..ae37f818b 100644 --- a/config/routes/legislation.rb +++ b/config/routes/legislation.rb @@ -22,7 +22,9 @@ namespace :legislation do collection do get :suggest end + end + resources :legislation_proposals, path: "proposals", only: [] do resources :votes, controller: "proposals/votes", only: [:create, :destroy] end @@ -42,6 +44,10 @@ resolve "Legislation::Proposal" do |proposal, options| [proposal.process, :proposal, options.merge(id: proposal)] end +resolve "Vote" do |vote, options| + [*resource_hierarchy_for(vote.votable), vote, options] +end + resolve "Legislation::Question" do |question, options| [question.process, :question, options.merge(id: question)] end diff --git a/spec/routing/polymorphic_routes_spec.rb b/spec/routing/polymorphic_routes_spec.rb index 5ff9bef09..9810cfcda 100644 --- a/spec/routing/polymorphic_routes_spec.rb +++ b/spec/routing/polymorphic_routes_spec.rb @@ -16,6 +16,26 @@ describe "Polymorphic routes" do expect(polymorphic_path(proposal)).to eq legislation_process_proposal_path(process, proposal) end + it "routes legislation proposals vote" do + process = create(:legislation_process) + proposal = create(:legislation_proposal, process: process) + path = polymorphic_path(Vote.new(votable: proposal), value: true) + + expect(path).to eq legislation_process_legislation_proposal_votes_path(process, proposal, value: true) + end + + it "routes legislation proposals remove vote" do + process = create(:legislation_process) + proposal = create(:legislation_proposal, process: process) + vote = create(:vote, votable: proposal) + path = polymorphic_path(vote, value: true) + + expect(path).to eq legislation_process_legislation_proposal_vote_path(process, + proposal, + vote, + value: true) + end + it "routes legislation questions" do process = create(:legislation_process) question = create(:legislation_question, process: process)