2016-02-23 99 views
2

我目前有一個控制器和模型,用於需要幫助重構的應用程序表單。這是一個多視圖應用程序的形式(因此爲什麼在控制器中有一堆重定向等)。有太多的參數,我只是不知道該怎麼做(或者如果有更多的我做)。我應該將模型進一步分割嗎?我試圖利用值對象(即rails「composed_of」函數),但不知道我是否正確使用它。任何幫助和想法將不勝感激!下面的代碼:Rails重構控制器和包含太多參數的模型

應用控制器

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來隱藏/顯示凝結/簡化控制器,儘管這不會解決大量PARAMS形式的部分也contimplated ...

回答

0

作爲更新,我已經完成並將應用程序表單拆分爲不同的模型,並使用地址的多態關聯。