【发布时间】:2016-05-31 15:45:53
【问题描述】:
我在大型 Rails 应用程序 room 和 inquiry 中有 2 个模型。他们都共享一个属性/列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.
在各种 RoomController(跨不同的命名空间)中,我可以使用 @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。
如果需要进一步解释,请告诉我。
【问题讨论】:
标签: ruby-on-rails encapsulation single-responsibility-principle presenter