Ruby on rails Rails重构参数过多的控制器和模型
我目前有一个应用程序表单的控制器和模型,需要重构帮助。这是一个多视图应用程序表单(因此控制器中有大量重定向等)。有这么多的情妇,我只是不知道该做什么(或者如果有更多的事情要我做)。我应该把模型进一步分开吗?我尝试使用值对象(即rails“composited_of”函数),但不确定是否正确使用了它。任何帮助和想法都将不胜感激!代码如下: 应用程序控制器Ruby on rails Rails重构参数过多的控制器和模型,ruby-on-rails,controller,refactoring,value-objects,Ruby On Rails,Controller,Refactoring,Value Objects,我目前有一个应用程序表单的控制器和模型,需要重构帮助。这是一个多视图应用程序表单(因此控制器中有大量重定向等)。有这么多的情妇,我只是不知道该做什么(或者如果有更多的事情要我做)。我应该把模型进一步分开吗?我尝试使用值对象(即rails“composited_of”函数),但不确定是否正确使用了它。任何帮助和想法都将不胜感激!代码如下: 应用程序控制器 class Leads::ApplicationsController < ApplicationController helper_
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 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
引用值对象
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
学生价值观对象
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隐藏/显示表单的某些部分,以压缩/简化控制器,尽管这并不能解决大量参数的问题……作为更新,我已经将应用程序表单拆分为不同的模型,并对地址使用多态关联