Updated

app/controllers/admin/archived / petitions_controller.rb

B
100 lines of codes
15 methods
5.9 complexity/method
46 churn
87.77 complexity
0 duplications
class Admin::Archived::PetitionsController < Admin::AdminController
  1. Admin::Archived::PetitionsController assumes too much for instance variable '@parliament'
  2. Admin::Archived::PetitionsController assumes too much for instance variable '@petitions'
  3. Admin::Archived::PetitionsController has no descriptive comment
before_action :redirect_to_show_page, only: [:index], if: :petition_id? before_action :fetch_parliament, only: [:index] before_action :redirect_to_admin_hub, only: [:index], unless: :parliament_present? before_action :fetch_petitions, only: [:index] before_action :fetch_petition, only: [:show] 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 protected def petition_id? /^\d+$/ =~ params[:q].to_s end def parliament_present? @parliament.present? end def redirect_to_show_page redirect_to admin_archived_petition_url(params[:q].to_i) end def redirect_to_admin_hub redirect_to admin_root_url, notice: "There are no archived petitions" end def scope
  1. Admin::Archived::PetitionsController#scope has a flog score of 29
if params[:match] == "none"
  1. Admin::Archived::PetitionsController#scope calls 'params[:match]' 2 times Locations: 0 1
@parliament.petitions.untagged
  1. Admin::Archived::PetitionsController#scope calls '@parliament.petitions' 4 times Locations: 0 1 2 3
elsif params[:tags].present?
  1. Admin::Archived::PetitionsController#scope calls 'params[:tags]' 3 times Locations: 0 1 2
if params[:match] == "all"
  1. Admin::Archived::PetitionsController#scope calls 'params[:match]' 2 times Locations: 0 1
@parliament.petitions.tagged_with_all(params[:tags])
  1. Admin::Archived::PetitionsController#scope calls '@parliament.petitions' 4 times Locations: 0 1 2 3
  2. Admin::Archived::PetitionsController#scope calls 'params[:tags]' 3 times Locations: 0 1 2
else @parliament.petitions.tagged_with_any(params[:tags])
  1. Admin::Archived::PetitionsController#scope calls '@parliament.petitions' 4 times Locations: 0 1 2 3
  2. Admin::Archived::PetitionsController#scope calls 'params[:tags]' 3 times Locations: 0 1 2
end else @parliament.petitions.all
  1. Admin::Archived::PetitionsController#scope calls '@parliament.petitions' 4 times Locations: 0 1 2 3
end end def parliament_id params[:parliament].to_i end def fetch_parliament if params.key?(:parliament) @parliament = Parliament.archived.find(parliament_id)
  1. Admin::Archived::PetitionsController#fetch_parliament calls 'Parliament.archived' 2 times Locations: 0 1
else @parliament = Parliament.archived.first
  1. Admin::Archived::PetitionsController#fetch_parliament calls 'Parliament.archived' 2 times Locations: 0 1
end end def fetch_petitions @petitions = scope.search(params) end def fetch_petition @petition = ::Archived::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