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)
|
def vote_in_favor_against_path(value)
|
||||||
if user_already_voted_with(value)
|
if user_already_voted_with(value)
|
||||||
remove_vote_path(value)
|
vote = Vote.find_by!(votable: votable, voter: current_user)
|
||||||
|
polymorphic_path(vote)
|
||||||
else
|
else
|
||||||
if votable.class.name == "Debate"
|
polymorphic_path(Vote.new(votable: votable), value: value)
|
||||||
debate_votes_path(votable, value: value)
|
|
||||||
else
|
|
||||||
legislation_process_proposal_votes_path(votable.process, votable, value: value)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -43,15 +40,6 @@ class Shared::InFavorAgainstComponent < ApplicationComponent
|
|||||||
current_user&.voted_as_when_voted_for(votable) == parse_vote(value)
|
current_user&.voted_as_when_voted_for(votable) == parse_vote(value)
|
||||||
end
|
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)
|
def parse_vote(value)
|
||||||
value == "yes" ? true : false
|
value == "yes" ? true : false
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ module Legislation
|
|||||||
class VotesController < ApplicationController
|
class VotesController < ApplicationController
|
||||||
before_action :authenticate_user!
|
before_action :authenticate_user!
|
||||||
load_and_authorize_resource :process, class: "Legislation::Process"
|
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
|
load_and_authorize_resource through: :proposal, through_association: :votes_for, only: :destroy
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
|||||||
@@ -22,7 +22,9 @@ namespace :legislation do
|
|||||||
collection do
|
collection do
|
||||||
get :suggest
|
get :suggest
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
resources :legislation_proposals, path: "proposals", only: [] do
|
||||||
resources :votes, controller: "proposals/votes", only: [:create, :destroy]
|
resources :votes, controller: "proposals/votes", only: [:create, :destroy]
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -42,6 +44,10 @@ resolve "Legislation::Proposal" do |proposal, options|
|
|||||||
[proposal.process, :proposal, options.merge(id: proposal)]
|
[proposal.process, :proposal, options.merge(id: proposal)]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
resolve "Vote" do |vote, options|
|
||||||
|
[*resource_hierarchy_for(vote.votable), vote, options]
|
||||||
|
end
|
||||||
|
|
||||||
resolve "Legislation::Question" do |question, options|
|
resolve "Legislation::Question" do |question, options|
|
||||||
[question.process, :question, options.merge(id: question)]
|
[question.process, :question, options.merge(id: question)]
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -16,6 +16,26 @@ describe "Polymorphic routes" do
|
|||||||
expect(polymorphic_path(proposal)).to eq legislation_process_proposal_path(process, proposal)
|
expect(polymorphic_path(proposal)).to eq legislation_process_proposal_path(process, proposal)
|
||||||
end
|
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
|
it "routes legislation questions" do
|
||||||
process = create(:legislation_process)
|
process = create(:legislation_process)
|
||||||
question = create(:legislation_question, process: process)
|
question = create(:legislation_question, process: process)
|
||||||
|
|||||||
Reference in New Issue
Block a user