Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

returns in predicates no longer supported #2

Closed
coldnebo opened this issue Jun 14, 2012 · 1 comment
Closed

returns in predicates no longer supported #2

coldnebo opened this issue Jun 14, 2012 · 1 comment
Assignees

Comments

@coldnebo
Copy link
Owner

A side effect of the fix for issue 1 is that blocks that use return generate a LocalJumpError now. Since predicates are conceptually like lambdas, it's much more flexible to support return within predicate blocks -- especially when transplanting business logic from legacy sources that may be hard to refactor into a non-return format.

For example:

# in a controller action...
predicate(:show_secret?) { 
  if my_terrible_secret
    return true
  else
    return false
  end 
}

# then, when used in the view...
Started GET "/main/index" for 127.0.0.1 at 2012-06-14 18:13:07 -0400
Connecting to database specified by database.yml
Processing by MainController#index as HTML
  Rendered main/index.html.erb within layouts/application (9.0ms)
Completed 500 Internal Server Error in 74ms

ActionView::Template::Error (unexpected return):
    1: <h1>Main#index</h1>
    2: <p>Find me in app/views/main/index.html.erb</p>
    3: <% if show_secret? %>
    4: <p>THIS IS THE SECRET BLOCK</p>
    5: <% end %>
  app/controllers/main_controller.rb:8:in `block in index'
  app/views/main/index.html.erb:3:in `_app_views_main_index_html_erb___2806522044999392377_25110420'

  Rendered /local/rvm/gems/ruby-1.9.3-p194@conditest/gems/actionpack-3.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.erb (19.2ms)
  Rendered /local/rvm/gems/ruby-1.9.3-p194@conditest/gems/actionpack-3.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.9ms)
  Rendered /local/rvm/gems/ruby-1.9.3-p194@conditest/gems/actionpack-3.2.6/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (30.5ms)
[2012-06-14 18:13:08] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
@ghost ghost assigned coldnebo Jun 14, 2012
@coldnebo
Copy link
Owner Author

This fix was a little harder to get right. It turns out that define_method(name,&block) has special magic that keeps block as a method/lambda if it is so inclined, while define_method(name) {} always creates a new Proc. (This is why condi-0.0.6 worked and 0.0.7 didn't.) So I had to switch strategies and define an impl the old way, while wrapping it the new way with the request id check logic. Phew!

Anyway, cooking with gas now:

Started GET "/main/index" for 127.0.0.1 at 2012-06-14 19:46:02 -0400
Connecting to database specified by database.yml
Processing by MainController#index as HTML
  Rendered main/index.html.erb within layouts/application (2.9ms)
Completed 200 OK in 164ms (Views: 160.8ms | ActiveRecord: 0.0ms)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant