Created
June 21, 2017 17:21
-
-
Save bryanp/b77f41614c227ef81e424e8467534772 to your computer and use it in GitHub Desktop.
Pakyow Presenter State
This file contains hidden or 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
# Demonstration of how state will be passed from the route context to presenter. | |
# Presentation will not occur in the routing context, so only what is exposed is available. | |
module Helpers | |
presentable :current_user, :current_resource | |
def current_user | |
@current_user ||= data.user.find(session[:user]) | |
end | |
def current_resource | |
# does the right thing based on the route; optional to use | |
end | |
end | |
resource :post do | |
include Helpers | |
new do | |
handle 403 unless current_user.can?(:create_post) | |
# view is auto-rendered based on the url | |
end | |
create do | |
current_resource.save | |
handle ValidationError do | |
# calls the `new` route, which presents the form | |
# for the resource we just tried to save | |
reroute :post_new | |
end | |
end | |
end | |
view "/posts/new" do | |
# not sure about this syntax, but this sets up the | |
# form for the resource we are presenting | |
form_for(current_resource).setup | |
# we also have access to `current_user` here | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment