Updated

app/controllers/admin / petitions_controller.rb

B
83 lines of codes
12 methods
5.9 complexity/method
47 churn
71.07 complexity
0 duplications
class Admin::PetitionsController < Admin::AdminController
  1. Admin::PetitionsController assumes too much for instance variable '@petition'
  2. Admin::PetitionsController assumes too much for instance variable '@petitions'
  3. Admin::PetitionsController has no descriptive comment
before_action :redirect_to_show_page, only: [:index], if: :petition_id? before_action :fetch_petitions, only: [:index] before_action :fetch_petition, only: [:show, :resend] rescue_from ActiveRecord::RecordNotFound do redirect_to admin_root_url, alert: "Sorry, we couldn't find petition #{params[:id]}" end def index respond_to do |format| format.html format.csv { render_csv } end end def show respond_to do |format| format.html end end def resend GatherSponsorsForPetitionEmailJob.perform_later(@petition, Site.feedback_email) redirect_to admin_petition_url(@petition), notice: :email_resent_to_creator end protected def petition_id? /^\d+$/ =~ params[:q].to_s end def redirect_to_show_page redirect_to admin_petition_url(params[:q].to_i) end def scope if params[:match] == "none"
  1. Admin::PetitionsController#scope calls 'params[:match]' 2 times Locations: 0 1
Petition.untagged elsif params[:tags].present?
  1. Admin::PetitionsController#scope calls 'params[:tags]' 3 times Locations: 0 1 2
if params[:match] == "all"
  1. Admin::PetitionsController#scope calls 'params[:match]' 2 times Locations: 0 1
Petition.tagged_with_all(params[:tags])
  1. Admin::PetitionsController#scope calls 'params[:tags]' 3 times Locations: 0 1 2
else Petition.tagged_with_any(params[:tags])
  1. Admin::PetitionsController#scope calls 'params[:tags]' 3 times Locations: 0 1 2
end else Petition.all end end def fetch_petitions @petitions = scope.search(params) end def fetch_petition @petition = Petition.find(params[:id]) end def render_csv set_file_headers set_streaming_headers #setting the body to an enumerator, rails will iterate this enumerator self.response_body = PetitionsCSVPresenter.new(@petitions).render end def set_file_headers headers["Content-Type"] = "text/csv" headers["Content-Disposition"] = "attachment; filename=#{csv_filename}" end def set_streaming_headers #nginx doc: Setting this to "no" will allow unbuffered responses suitable for Comet and HTTP streaming applications headers['X-Accel-Buffering'] = 'no' headers["Cache-Control"] ||= "no-cache" headers.delete("Content-Length") end def csv_filename "#{@petitions.scope.to_s.dasherize}-petitions-#{Time.current.to_s(:number)}.csv" end end