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).
73 lines
1.9 KiB
Ruby
73 lines
1.9 KiB
Ruby
class DebatesController < ApplicationController
|
|
include FeatureFlags
|
|
include CommentableActions
|
|
include FlagActions
|
|
include Translatable
|
|
|
|
before_action :authenticate_user!, except: [:index, :show]
|
|
before_action :set_view, only: :index
|
|
before_action :debates_recommendations, only: :index, if: :current_user
|
|
|
|
feature_flag :debates
|
|
|
|
invisible_captcha only: [:create, :update], honeypot: :subtitle
|
|
|
|
has_orders ->(c) { Debate.debates_orders(c.current_user) }, only: :index
|
|
has_orders %w[most_voted newest oldest], only: :show
|
|
|
|
load_and_authorize_resource
|
|
helper_method :resource_model, :resource_name
|
|
respond_to :html, :js
|
|
|
|
def index_customization
|
|
@featured_debates = @debates.featured
|
|
end
|
|
|
|
def show
|
|
super
|
|
redirect_to debate_path(@debate), status: :moved_permanently if request.path != debate_path(@debate)
|
|
end
|
|
|
|
def unmark_featured
|
|
@debate.update!(featured_at: nil)
|
|
redirect_to debates_path
|
|
end
|
|
|
|
def mark_featured
|
|
@debate.update!(featured_at: Time.current)
|
|
redirect_to debates_path
|
|
end
|
|
|
|
def disable_recommendations
|
|
if current_user.update(recommended_debates: false)
|
|
redirect_to debates_path, notice: t("debates.index.recommendations.actions.success")
|
|
else
|
|
redirect_to debates_path, error: t("debates.index.recommendations.actions.error")
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def debate_params
|
|
params.require(:debate).permit(allowed_params)
|
|
end
|
|
|
|
def allowed_params
|
|
[:tag_list, :terms_of_service, :related_sdg_list, translation_params(Debate)]
|
|
end
|
|
|
|
def resource_model
|
|
Debate
|
|
end
|
|
|
|
def set_view
|
|
@view = (params[:view] == "minimal") ? "minimal" : "default"
|
|
end
|
|
|
|
def debates_recommendations
|
|
if Setting["feature.user.recommendations_on_debates"] && current_user.recommended_debates
|
|
@recommended_debates = Debate.recommendations(current_user).sort_by_random.limit(3)
|
|
end
|
|
end
|
|
end
|