Created
February 2, 2016 08:37
-
-
Save benhawker/3620235c41c0bdda0d44 to your computer and use it in GitHub Desktop.
Rails Presenters example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Assume you also have the following "general case" presenters | |
# app/presenters/cancellation_policy_presenter.rb | |
# app/presenters/daily_price_presenter.rb | |
# app/presenters/weekly_price_presenter.rb | |
# Now for some presenter fun.. | |
# app/presenters/room_presenter.rb | |
class RoomPresenter | |
# gives you access to RoomPresenter#cancellation_policy (see below) | |
include RoomPresenters::CancellationPolicy | |
# gives you access to RoomPresenter#daily_price and RoomPresenter#weekly_price (see below) | |
include RoomPresenters::Prices | |
def initialize(room) | |
@room = room | |
# + your awesome initialization codes here | |
end | |
end | |
# app/presenters/room_presenters/cancellation_policy.rb | |
module RoomPresenters | |
module CancellationPolicy | |
def cancellation_policy | |
@cancellation_policy ||= CancellationPolicyPresenter.new(@room).show | |
# ..or modify the result of CancellationPolicyPresenter#show that applies only to rooms | |
end | |
end | |
end | |
# app/presenters/room_presenters/prices.rb | |
module RoomPresenters | |
module Prices | |
def daily_price | |
@daily_price ||= DailyPricePresenter.new(@room).show | |
# ..or modify the result of DailyPricePresenter#show that applies only to rooms | |
end | |
def weekly_price | |
@weekly_price ||= WeeklyPricePresenter.new(@room).show | |
# ..or modify the result of WeeklyPricePresenter#show that applies only to rooms | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment