Updated

app/controllers / local_petitions_controller.rb

A
85 lines of codes
13 methods
3.7 complexity/method
16 churn
48.68 complexity
0 duplications
require 'postcode_sanitizer' require 'csv' class LocalPetitionsController < ApplicationController
  1. LocalPetitionsController assumes too much for instance variable '@constituency'
  2. LocalPetitionsController assumes too much for instance variable '@postcode'
  3. LocalPetitionsController has no descriptive comment
before_action :sanitize_postcode, only: :index before_action :find_by_postcode, if: :postcode?, only: :index before_action :find_by_slug, only: [:show, :all] before_action :find_petitions, if: :constituency?, only: :show before_action :find_all_petitions, if: :constituency?, only: :all before_action :redirect_to_constituency, if: :constituency?, only: :index after_action :set_content_disposition, if: :csv_request?, only: [:show, :all] def index respond_to do |format| format.html end end def show respond_to do |format| format.html
  1. LocalPetitionsController#show refers to 'format' more than self (maybe move it to another class?) Locations: 0 1 2
format.json
  1. LocalPetitionsController#show refers to 'format' more than self (maybe move it to another class?) Locations: 0 1 2
format.csv
  1. LocalPetitionsController#show refers to 'format' more than self (maybe move it to another class?) Locations: 0 1 2
end end def all respond_to do |format| format.html
  1. LocalPetitionsController#all refers to 'format' more than self (maybe move it to another class?) Locations: 0 1 2
format.json
  1. LocalPetitionsController#all refers to 'format' more than self (maybe move it to another class?) Locations: 0 1 2
format.csv
  1. LocalPetitionsController#all refers to 'format' more than self (maybe move it to another class?) Locations: 0 1 2
end end private def sanitize_postcode @postcode = PostcodeSanitizer.call(params[:postcode]) end def postcode? @postcode.present? end def find_by_postcode @constituency = Constituency.find_by_postcode(@postcode) end def find_by_slug @constituency = Constituency.find_by_slug!(params[:id]) end def constituency? @constituency.present? end def find_petitions @petitions = Petition.popular_in_constituency(@constituency.external_id, 50) end def find_all_petitions @petitions = Petition.all_popular_in_constituency(@constituency.external_id, 50) end def redirect_to_constituency if Parliament.dissolved? redirect_to all_local_petition_url(@constituency.slug)
  1. LocalPetitionsController#redirect_to_constituency calls '@constituency.slug' 2 times Locations: 0 1
else redirect_to local_petition_url(@constituency.slug)
  1. LocalPetitionsController#redirect_to_constituency calls '@constituency.slug' 2 times Locations: 0 1
end end def csv_filename if action_name == 'all' "all-popular-petitions-in-#{@constituency.slug}.csv"
  1. LocalPetitionsController#csv_filename calls '@constituency.slug' 2 times Locations: 0 1
else "open-popular-petitions-in-#{@constituency.slug}.csv"
  1. LocalPetitionsController#csv_filename calls '@constituency.slug' 2 times Locations: 0 1
end end def set_content_disposition response.headers['Content-Disposition'] = "attachment; filename=#{csv_filename}" end end