Created
April 21, 2017 14:19
-
-
Save brandonhilkert/fe3f25c71def44803ee4a77001988063 to your computer and use it in GitHub Desktop.
Basic presenters without gem
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
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 |
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
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 |
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
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 |
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
<%= 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