inject things into views #55

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
2 participants
Collaborator

tcrayford commented Apr 30, 2012

This enables users to write things like

<% if inject :user %>
Logout
<% end %>

in their views (assuming user is an injectable that returns nil when no user is available).

Owner

garybernhardt commented May 3, 2012

Mixing the inject into the access strikes me as a weird mixing of concerns. What about either declaring (<% inject :user %> makes user available, but doesn't return it), or a method_missing hack?

This makes me pine for a view/template distinction, but I don't think pursing that desire would lead to happiness... ;)

Collaborator

tcrayford commented May 3, 2012

I like the first of those. Will put in a pull req in a bit.

Collaborator

tcrayford commented May 17, 2012

So I thought about this a bit more (finally). The thing I dislike about the first approach (making things available) is that it forces mutable state into raptor.

I guess method_missing is kinda better than that (assuming you mean the template just calls user, and the context brings that out of the injector). Kinda makes the injector feel even more magic though.

Owner

garybernhardt commented May 17, 2012

It definitely does make it feel more magical. Are you happy enough to add it and try it out? If so, rebase onto master and I'll merge?

Collaborator

tcrayford commented Jun 9, 2012

Thinking about fallbacks for when the injectable isn't available here. What about the method_missing hack taking a block, and only calling the block if the user is available:

<% current_user do |current_user| %>
  <a href="/logout">Logout</a>
<% end %>

This has a downside of not being able to display other text when the user isn't available.

Other options:

  • Pass a fallback to method_missing: current_user(:or => NilUser.new)
  • Pass a fallback to the inject method that makes the user available: inject :current_user, NilUser.new.

@tcrayford tcrayford closed this Jul 11, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment