Updated

app/controllers/admin / petition_emails_controller.rb

C
98 lines of codes
14 methods
4.2 complexity/method
5 churn
59.33 complexity
62 duplications
class Admin::PetitionEmailsController < Admin::AdminController
  1. Admin::PetitionEmailsController assumes too much for instance variable '@email'
  2. Admin::PetitionEmailsController assumes too much for instance variable '@petition'
  3. Admin::PetitionEmailsController has no descriptive comment
before_action :fetch_petition before_action :build_email, only: [:new, :create] before_action :fetch_email, only: [:edit, :update, :destroy] def new render 'admin/petitions/show' end def create
  1. Similar code found in 2 nodes Locations: 0 1
  2. Admin::PetitionEmailsController#create has approx 8 statements
if @email.update(email_params) if send_email_to_petitioners? schedule_email_petitioners_job send_preview_email message = :email_sent_overnight elsif send_preview_email? send_preview_email message = :preview_email_sent else message = :petition_email_created end redirect_to [:admin, @petition], notice: message else render 'admin/petitions/show' end end def edit end def update
  1. Similar code found in 2 nodes Locations: 0 1
  2. Admin::PetitionEmailsController#update has approx 8 statements
if @email.update(email_params) if send_email_to_petitioners? schedule_email_petitioners_job send_preview_email message = :email_sent_overnight elsif send_preview_email? send_preview_email message = :preview_email_sent else message = :petition_email_updated end redirect_to [:admin, @petition], notice: message else render 'admin/petitions/show' end end def destroy if @email.destroy message = :petition_email_deleted else message = :petition_email_not_deleted end redirect_to [:admin, @petition], notice: message end private def fetch_petition @petition = Petition.moderated.find(params[:petition_id]) end def build_email @email = @petition.emails.build end def fetch_email @email = @petition.emails.find(params[:id]) end def email_params params.require(:petition_email).permit(:subject, :body).merge(sent_by: current_user.pretty_name) end def feedback_signature FeedbackSignature.new(@petition) end def send_email_to_petitioners? params.key?(:save_and_email) end def send_preview_email? params.key?(:save_and_preview) end def schedule_email_petitioners_job EmailPetitionersJob.run_later_tonight(petition: @petition, email: @email) end def send_preview_email PetitionMailer.email_signer(@petition, feedback_signature, @email).deliver_now end end