Use polimorphic path in favor against component
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user