← Back to Upcase

Refactoring: making it as concise as possible help


(Brian Dear) #1

Here’s the code:

def calculate_discount_price
    self.course = Course.find(session[:course_id])
    if discount_code = DiscountCode.find_by(discount_code: params[:discount_code])
      if discount_code.not_expired_or_cancelled?
        apply_discount(discount_code, course)
      else
        expired_code(course)
      end
    else
      code_not_found(course)
    end
  end

  def apply_discount(discount_code, course)
    session[:discount_code_id] = discount_code.id
    flash[:success] = 'Discount applied!'
    redirect_to new_charge_path(course_id: course.id)
  end

  def expired_code(course)
    flash[:error] = 'That discount code has expired!'
    redirect_to new_charge_path(course_id: course.id)
  end

  def code_not_found(course)
    flash[:error] = 'Sorry, but that code was not found.'
    redirect_to new_charge_path(course_id: course.id)
  end

Any ideas on how to make this even more concise and DRYed up as possible?