我在一个大型 Rails 应用程序中有 2 个模型,room
并且inquiry
. 他们都共享一个属性/列cancellation_policy
。
在进行查询(别名:预订)时,从到cancellation_policy
复制。room.cancellation_policy
inquiry.cancellation_policy
我目前有一个RoomPresenter
用这样的对象初始化的room
:
def initialize(room, current_user = nil)
@room = room
@current_user = current_user
end
这位演示者会做各种事情来“呈现”一个room
. 然而,我最近添加了一堆方法,例如:
def cancellation_policy_type
def get_cancellation_policy_text_with_formatting
etc.
例如,在各种 RoomControllers(跨不同的命名空间)中,我可以实例化@room_presenter = RoomPresenter.new(@room)
并调用相关视图中的方法,如预期的那样@room_presenter. def cancellation_policy_type
。
我觉得我可以采取以下方法
class RoomPresenter
# gives me access to RoomPresenter#cancellation_policy (see below)
include RoomPresenters::CancellationPolicy
def initialize(room)
@room = room
end
end
# app/presenters/room_presenters/cancellation_policy.rb
module RoomPresenters
module CancellationPolicy
def cancellation_policy
###
end
end
end
这将以一种合乎逻辑的方式将room
演示者方法与room.cancellation_policy
Room
Inquiry
然而,当涉及到在查询模型和房间模型中结合这一点时,我的主要问题/无知出现了。以下对我来说似乎都非常错误:
class InquiryPresenter (would be initialized with an inquiry).
include RoomPresenters::CancellationPolicy
同样:
class InquiryPresenter
#lots of duplicated code doing the same thing/same methods.
我试图了解如何最好地组织这种类型的逻辑,但不确定最好的方法。
底层输出非常简单——每种方法都只是输出一些纯文本或 html,但随着应用程序的进一步发展,我认为需要确保Presenters
遵守 SRP。
如果需要进一步解释,请告诉我示例。