Created
February 24, 2016 12:36
-
-
Save tombeynon/f2c9d8bc1c2902f4596d to your computer and use it in GitHub Desktop.
Facade pattern
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
<% if facade.logged_in? %> | |
<ul> | |
<% facade.active_users.each do |user| %> | |
<li><%= user.name %></li> | |
<% end %> | |
</ul> | |
<% else %> | |
<p>You are not logged in!</p> | |
<% 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 UserListingFacade | |
attr_reader :user | |
def initialize(user) | |
@user = user | |
end | |
def active_users | |
@active_users ||= User.published.ordered | |
end | |
def logged_in? | |
!user.nil? | |
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 UsersController < ApplicationController | |
def index | |
@facade = UserListingFacade.new(current_user) | |
end | |
private | |
def facade | |
@facade ||= ApplicationFacade.new | |
end | |
helper_method :facade | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment