Skip to content

Instantly share code, notes, and snippets.

@brandonhilkert
Created April 21, 2017 14:19
Show Gist options
  • Save brandonhilkert/fe3f25c71def44803ee4a77001988063 to your computer and use it in GitHub Desktop.
Save brandonhilkert/fe3f25c71def44803ee4a77001988063 to your computer and use it in GitHub Desktop.
Basic presenters without gem
class ApplicationPresenter
def self.presents(name)
define_method(name) do
@object
end
end
def initialize(object, template = nil)
@object, @template = object, template
end
private
def h
@template
end
end
class ConnectionPresenter < ApplicationPresenter
presents :connection
def connection_link
if likely_private_messages_connection?
["#", { data: { toggle: "modal", target: "#expectations-modal" } }]
else
h.child_path(connection.child)
end
end
private
def likely_private_messages_connection?
true
end
end
module PresentersHelper
def present(object, klass = nil)
klass ||= "#{object.class}Presenter".constantize
presenter = klass.new(object, self)
if block_given?
yield presenter
nil
else
presenter
end
end
end
<%= present connection do |connection_presenter| %>
<%= connection_presenter.connection_link %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment