Skip to content

Instantly share code, notes, and snippets.

@benhawker
Created February 2, 2016 08:37
Show Gist options
  • Save benhawker/3620235c41c0bdda0d44 to your computer and use it in GitHub Desktop.
Save benhawker/3620235c41c0bdda0d44 to your computer and use it in GitHub Desktop.
Rails Presenters example
# 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