我目前有一个应用程序表单的控制器和模型,我需要帮助重构。它是一个多视图应用程序表单(因此控制器中有一堆重定向等)。有这么多的参数我不知道该怎么做(或者我还有更多的事情要做)。我应该进一步拆分模型吗?我试图利用价值对象(即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来隐藏/显示表单的某些部分以缩小/简化控制器,尽管这不会解决大量的参数...
答案 0 :(得分:0)
作为更新,我已经将应用程序表单拆分为不同的模型,并使用多态关联作为地址。