As far as possible I think the code is clearer if we use CRUD actions rather than custom actions. This will make it easier to add the action to remove votes in the next commit. Note that we are adding this line as we need to validate it that a vote can be created on a debate by the current user: ```authorize! :create, Vote.new(voter: current_user, votable: @debate)``` We have done it this way and not with the following code as you might expect, as this way two votes are created instead of one. ```load_and_authorize_resource through: :debate, through_association: :votes_for``` This line tries to load the resource @debate and through the association "votes_for" it tries to create a new vote associated to that debate. Therefore a vote is created when trying to authorise the resource and then another one in the create action, when calling @debate.vote_by (which is called by @debate.register_vote).
64 lines
1.8 KiB
Ruby
64 lines
1.8 KiB
Ruby
class Legislation::ProposalsController < Legislation::BaseController
|
|
include CommentableActions
|
|
include FlagActions
|
|
include ImageAttributes
|
|
|
|
before_action :load_categories, only: [:new, :create, :edit, :summary]
|
|
before_action :load_geozones, only: [:edit, :summary]
|
|
|
|
before_action :authenticate_user!, except: [:show, :summary]
|
|
load_and_authorize_resource :process, class: "Legislation::Process"
|
|
load_and_authorize_resource :proposal, class: "Legislation::Proposal", through: :process
|
|
|
|
invisible_captcha only: [:create, :update], honeypot: :subtitle
|
|
|
|
has_orders %w[most_voted newest oldest], only: :show
|
|
|
|
helper_method :resource_model, :resource_name
|
|
respond_to :html, :js
|
|
|
|
def show
|
|
super
|
|
@document = Document.new(documentable: @proposal)
|
|
if request.path != legislation_process_proposal_path(params[:process_id], @proposal)
|
|
redirect_to legislation_process_proposal_path(params[:process_id], @proposal),
|
|
status: :moved_permanently
|
|
end
|
|
end
|
|
|
|
def create
|
|
@proposal = Legislation::Proposal.new(proposal_params.merge(author: current_user))
|
|
|
|
if @proposal.save
|
|
redirect_to legislation_process_proposal_path(params[:process_id], @proposal),
|
|
notice: I18n.t("flash.actions.create.proposal")
|
|
else
|
|
render :new
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def proposal_params
|
|
params.require(:legislation_proposal).permit(allowed_params)
|
|
end
|
|
|
|
def allowed_params
|
|
[
|
|
:legislation_process_id, :title,
|
|
:summary, :description, :video_url, :tag_list,
|
|
:terms_of_service, :geozone_id,
|
|
image_attributes: image_attributes,
|
|
documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id]
|
|
]
|
|
end
|
|
|
|
def resource_model
|
|
Legislation::Proposal
|
|
end
|
|
|
|
def resource_name
|
|
"proposal"
|
|
end
|
|
end
|