簡體   English   中英

參數過多的Rails重構控制器和模型

[英]Rails Refactoring Controller and Model with too many params

我目前有一個需要重構的應用程序表單的控制器和模型。 這是一種多視圖應用程序形式(因此,為什么控制器中會有大量重定向等)。 我有太多的參數我不確定該怎么做(或者是否還有更多要做的事情)。 我應該進一步拆分模型嗎? 我嘗試利用值對象(即rails“ composed_of”函數),但不確定我是否在正確使用它。 任何幫助和想法將不勝感激! 代碼如下:

應用控制器

class Leads::ApplicationsController < ApplicationController
  helper_method :resource_name, :resource, :devise_mapping
  before_action :authenticate_lead!

  def resource_name
    :lead
  end

  def resource
    @resource ||= Lead.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:lead]
  end

  def new
    @new_rental_application = current_lead.create_rental_application
  end

  def address
    RentalApplication.create_address
    is_currently_student?
  end

  def occupation_post
    build_application_occupation
    save_application
    redirect_to rental_applications_references_path
  end

  def occupation_get
    render '/leads/rental_applications/occupation'
  end

  def student_post
    build_application_student
    save_application
    redirect_to rental_applications_references_path
  end

  def student_get
    render 'leads/rental_applications/student'
  end

  def references_post
    build_application_references
    save_application
    redirect_to action: "confirm_booking"
  end

  def references_get
    render 'leads/rental_applications/references'
  end

  def confirm_booking
    flash[:notice] = "Rental Application Succesfully Created."
    redirect_to profile_rental_application_path
  end

  def create
    current_lead.create_rental_application(rental_application_params)
    redirect_to profile_rental_application_path
  end

  def update
    current_lead.rental_application.update(rental_application_params)
    flash[:notice] = "Rental Application Succesfully Updated."
    redirect_to profile_rental_application_path
  end

  private

  def rental_application_params
    params.require(:rental_application).permit(lead_current_address<<lead_occupation<<lead_student<<lead_references)
  end

  def lead_occupation
    [:current_occupation,
    :current_occupation_company_name,
    :current_occupation_company_city,
    :current_occupation_company_address,
    :current_occupation_company_country,
    :current_occupation_company_province,
    :current_occupation_company_postal_code,
    :current_occupation_range]
  end

  def lead_current_address
    [:current_unit_number,
    :current_unit_street,
    :current_unit_city,
    :current_unit_province,
    :current_unit_postal_code,
    :current_unit_country,
    :is_currently_student]
  end

  def lead_references
    [:reference1_name,
    :reference1_phone_number,
    :reference1_relationship,
    :reference2_name,
    :reference2_phone_number,
    :reference2_relationship]
  end

  def lead_student
    [:current_student_degree,
    :current_student_university,
    :current_student_university_city,
    :current_student_university_country,
    :current_student_graduation_year]
  end

  def load_application
    current_lead.rental_application
  end

  def save_application
    load_application.save
  end

  def build_application_current_address
    load_application.current_address = Address.new(params[:current_unit_number], params[:current_unit_street], params[:current_unit_postal_code],
      params[:current_unit_city], params[:current_unit_province], params[:current_unit_country])
  end

  def build_application_occupation
    load_application.occupation = Occupation.new(params[:current_occupation], params[:current_occupation_company_name], params[:current_occupation_company_address], params[:current_occupation_company_city],
      params[:current_occupation_company_province], params[:current_occupation_company_postal_code], params[:current_occupation_company_country], params[:current_occupation_range])
  end

  def build_application_student
    load_application.student = Student.new(params[:current_student_degree], params[:current_student_university], params[:current_student_university_city], params[:current_student_university_country],
      params[:current_student_graduation_year])
  end

  def build_application_references
    load_application.references = References.new(params[:reference1_name], params[:reference1_phone_number], params[:reference1_relationship], params[:reference2_name], params[:reference2_phone_number],
      params[:reference2_relationship])
  end

  def is_currently_student?
    if params[:is_currently_student] == 'true'
      current_lead.rental_application.update(:is_currently_student => true)
      redirect_to rental_applications_student_path
    else
      current_lead.rental_application.update(:is_currently_student => false)
      redirect_to rental_applications_occupation_path
    end
  end

end

應用模式

class RentalApplication < ActiveRecord::Base
  belongs_to :lead

  composed_of :current_address, mapping: [ %w(current_unit_number number),
    %w(current_unit_street street),
    %w(current_unit_postal_code postal_code),
    %w(current_unit_city city),
    %w(current_unit_province province),
    %w(current_unit_country country) ]

  composed_of :occupation, mapping: [ %w(current_occupation occupation),
    %w(current_occupation_company_name company_name),
    %w(current_occupation_company_address company_address),
    %w(current_occupation_company_city company_city),
    %w(current_occupation_company_province company_province),
    %w(current_occupation_company_postal_code company_postal_code),
    %w(current_occupation_company_country company_country),
    %w(current_occupation_range salary_range) ]

  composed_of :student, mapping: [ %w(current_student_degree degree),
    %w(current_student_university university),
    %w(current_student_university_city university_city),
    %w(current_student_university_country university_country),
    %w(current_student_graduation_year graduation_year) ]

  composed_of :references, mapping: [ %w(reference1_name reference1_name),
    %w(reference1_phone_number reference1_phone_number),
    %w(reference1_relationship reference1_relationship),
    %w(reference2_name reference2_name),
    %w(reference2_phone_number reference2_phone_number),
    %w(reference2_relationship reference2_relationship) ]

end

值對象:

當前地址值對象

class CurrentAddress
  attr_reader :number, :street, :postal_code, :city, :province, :country

    def initialize(number, street, postal_code, city, province, country)
      @number, @street, @postal_code, @city, @province, @country = number, street, postal_code, city, province, country
    end

    def == (other_address)
      city == other_address.city && province == other_address.province &&
        country == other_address.country && postal_code == other_address.postal_code &&
        street == other_address.street && number == other_address.number
    end

end

職業價值對象

class Occupation
  attr_reader :occupation, :company_name, :company_address, :company_city, :company_province, :company_postal_code, :company_country, :salary_range

    def initialize(occupation, company_name, company_address, company_city, company_province, company_postal_code, company_country, salary_range)
      @occupation, @company_name, @company_address, @company_city, @company_province, @company_postal_code, @company_country, @salary_range = occupation, company_name, company_address, company_city, company_province,
        company_postal_code, company_country, salary_range
    end

    def ==(other_occupation)
      company_city == other_occupation.company_city && company_province == other_occupation.company_province &&
        company_country == other_occupation.company_country && company_postal_code == other_occupation.company_postal_code &&
        copmany_address == other_occupation.company_address && occupation == other_occupation.occupation &&
        company_name == other_occupation.company_name && salary_range == other_occupation.salary_range
    end

end

引用值對象

class References
  attr_reader :reference1_name, :reference1_phone_number, :reference1_relationship, :reference2_name, :reference2_phone_number, :reference2_relationship

    def initialize(reference1_name, reference1_phone_number, reference1_relationship, reference2_name, reference2_phone_number, reference2_relationship)
      @reference1_name, @reference1_phone_number, @reference1_relationship, @reference2_name, @reference2_phone_number, @reference2_relationship = reference1_name, reference1_phone_number,
        reference1_relationship, reference2_name, reference2_phone_number, reference2_relationship

    end

end

學生價值對象

class Student
  attr_reader :degree, :university, :university_city, :university_country, :graduation_year

    def initialize(degree, university, university_city, university_country, graduation_year)
      @degree, @university, @university_city, @university_country, @graduation_year = degree, university, university_city, university_country, graduation_year
    end

    def ==(other_student)
      degree == other_student.degree && university == other_student.university &&
        university_country == other_student.university_country && university_city == other_student.university_city &&
        graduation_year == other_student.graduation_year
    end

end

編輯:我也已經使用CSS / Javascript來隱藏/顯示表單的某些部分以壓縮/簡化控制器,盡管這並不能解決大量的參數...

作為更新,我繼續將申請表拆分為不同的模型,並對地址使用了多態關聯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM