命名空间和继承的表单对象的编码模式
Coding Pattern for Namespaced & inherited Form Object
当前表单对象
app/form_models/registration/step1.rb
module Registration
class Step1
include ActiveModel::Model
#Common code
#Unique code
private
#Common code
#Unique code
end
end
app/form_models/registration/step2.rb
module Registration
class Step2
include ActiveModel::Model
#Common code
#Unique code
private
#Common code
#Unique code
end
end
重构它以避免 #Common Code
被重复的首选方法是什么?
我应该创建另一个 class 并继承它吗?如果是这样,这是正确的模式吗?
## 可能的新表单对象模式
app/form_models/registration/base.rb
module Registration
class Base
include ActiveModel::Model
#Common code
private
#Common code
end
end
app/form_models/registration/step1.rb
这是继承命名间隔窗体对象的正确方法吗?
module Registration
class Step1<Registration::Base
#Unique code
private
#Unique code
end
end
我认为正确的方法是:
module Registration
class Step1 < Base
#Unique code
private
#Unique code
end
end
当前表单对象
app/form_models/registration/step1.rb
module Registration
class Step1
include ActiveModel::Model
#Common code
#Unique code
private
#Common code
#Unique code
end
end
app/form_models/registration/step2.rb
module Registration
class Step2
include ActiveModel::Model
#Common code
#Unique code
private
#Common code
#Unique code
end
end
重构它以避免 #Common Code
被重复的首选方法是什么?
我应该创建另一个 class 并继承它吗?如果是这样,这是正确的模式吗?
## 可能的新表单对象模式
app/form_models/registration/base.rb
module Registration
class Base
include ActiveModel::Model
#Common code
private
#Common code
end
end
app/form_models/registration/step1.rb
这是继承命名间隔窗体对象的正确方法吗?
module Registration
class Step1<Registration::Base
#Unique code
private
#Unique code
end
end
我认为正确的方法是:
module Registration
class Step1 < Base
#Unique code
private
#Unique code
end
end