Skip to content

Instantly share code, notes, and snippets.

@pzol
Last active December 11, 2015 07:29
Show Gist options
  • Select an option

  • Save pzol/4566825 to your computer and use it in GitHub Desktop.

Select an option

Save pzol/4566825 to your computer and use it in GitHub Desktop.
Hexagonal controller example
module Bmsapp
module Booking
class ActionController
def initialize(ui, deps=Dependencies.new(:current_user => ui.current_user))
@ui, @deps, @bms, @logger = ui, deps, deps.bms, deps.logger
end
def action(action_name, id, params)
@booking = @bms.booking_by_id(id)
@params = params
action_class = constantize(action_name)
if action_class
action = action_class.new(@booking, params, @deps)
result = run_and_log(action)
@ui.redirect(@ui.url_for(:booking, :index, :id => @booking._id), result)
else
@ui.error(501, "action #{action_name.classify} not implemented")
end
end
private
def constantize(action_name)
"Bmsapp::Booking::Actions::#{action_name.classify}".safe_constantize
end
def run_and_log(action)
begin
result = action.call
@logger.info(:user => @ui.current_user.name, :ref => @booking.ref_anixe,
:short => "#{action.class.name.demodulize} #{result}", :full => @params.inspect)
result
rescue => e
@logger.error(:user => @ui.current_user.name, :ref => @booking.ref_anixe,
:short => "#{action.class.name} failed #{e.message}", :full => @params.inspect)
{:error => "#{action.class.name.demodulize} failed (#{e.message})" }
end
end
end
end
end
module Bmsapp::Booking::Actions
class Cancel
def initialize(booking, params, deps)
@booking, @params, @queue = booking, params.symbolize_keys, deps.queue
end
def call
options = {}
requested_cancellation_fee_amount = @params[:requested_cancellation_fee_amount]
if @params[:specify_cancellation_fee] == "1" && requested_cancellation_fee_amount.present?
options[:cancellation_fee_amount] = requested_cancellation_fee_amount
options[:cancellation_fee_currency] = @booking.price.currency
end
@queue.push(@booking, 'Cancel', options)
{ notice: 'Booking queued for cancellation. Reload at will.' }
end
end
end
module Bmsapp
module Booking
class Controller
def initialize(ui, deps=Dependencies.new)
@ui, @deps, @bms = ui, deps, deps.bms
end
def index(id, format=:html)
@booking = @bms.booking_by_id(id)
render(format)
end
def archive(id, format=:html)
@booking = @bms.archived_booking_by_id(id)
render(format)
end
private
def render(format)
if @booking
@ability = Bmsapp::Ability.new(@booking)
self.send(format)
else
@ui.error(404, "Booking not found")
end
end
def can?(action)
@ability.can? @ui.current_user.roles, action
end
def json
if can? :view_booking_raw_data
@booking.to_json
else
@ui.error(403, "Not Authorized, 'anixe' role required")
end
end
def html
begin
@booking.extend(DetailsDecorator)
@booking.deps = @deps
@ui.flash(:notice => Flash.new(@booking).notice)
@ui.render('bookings/details', :booking => @booking, :ability => @ability)
rescue => e
@ui.flash(:error => "This booking contains an error and cannot be displayed")
@ui.render('bookings/error', :booking => @booking, :ability => @ability, :error => e, :backtrace => Bmsapp::CleanBacktrace.new.to_html(e.backtrace))
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment