mysociety/alaveteli

View on GitHub
app/controllers/reports_controller.rb

Summary

Maintainability
A
45 mins
Test Coverage
class ReportsController < ApplicationController
  before_action :set_info_request
  before_action :set_comment
  before_action :set_reportable
  before_action :set_report_reasons

  def create
    @reason = params[:reason]
    @message = params[:message] || ""
    if @reason.empty?
      flash[:error] = _("Please choose a reason")
      render "new"
      return
    end
    if !authenticated?
      flash[:notice] = _("You need to be logged in to report a request for administrator attention")
    elsif @info_request.attention_requested
      flash[:notice] = _("This request has already been reported for administrator attention")
    else
      @reportable.report!(@reason, @message, @user)
      flash[:notice] = if @comment
        _("This annotation has been reported for administrator attention")
      else
        _("This request has been reported for administrator attention")
      end
    end
    redirect_to request_url(@info_request)
  end

  def new
    @title = if @comment
      _("Report annotation on request: {{title}}",
        title: @info_request.title)
    else
      _("Report request: {{title}}", title: @info_request.title)
    end

    return if authenticated?

    ask_to_login(
      web: _('To report this request'),
      email: _("Then you can report the request '{{title}}'",
               title: @info_request.title),
      email_subject: _('Report an offensive or unsuitable request'),
      comment_id: params[:comment_id]
    )
  end

  private

  def set_info_request
    @info_request = InfoRequest.not_embargoed.
      find_by_url_title!(params[:request_url_title])
  end

  def set_comment
    @comment = unless params[:comment_id].blank?
      @info_request.comments.where(id: params[:comment_id]).first!
    end
  end

  def set_reportable
    @reportable = @comment || @info_request
  end

  def set_report_reasons
    @report_reasons = @reportable.report_reasons
  end
end