On Friday, June 27, 2014 5:01:36 AM UTC-7, Nick Sutterer wrote:
>
>
> You mean instead of having to override ::view_context_class in a 
> controller you could just extend your ApplicationView class directly?
>
> Nick
>

 Didn't hear of ::view_context_class before, but yes, I'd like to be able 
to subclass and use the ApplicationView class outside of regular 
controllers, while still using all the tools normally only available in the 
controller, like helper loading:

class MyRenderer < ApplicationView::Base
end

MyRenderer.render('/users/index')

The above should be as smart as regular view rendering is vis-a-vis which 
helpers/contex to load, for example:

   - All app route helper methods (_url and _path) should be available 
   without having to custom-require them. 
   - The same view paths should be able as those available from regular 
   controller-base view renders.
   - It should load the same helpers that would be available had an action 
   been rendered from UsersController#index. And custom helpers should be 
   loadable in same way as in controller:

class MyRenderer < ApplicationView::Base
  helper :all
end

Assigns and methods should be settable directly on the renderer:

class MyRenderer < ApplicationView::Base
  def initialize(user_id)
    super # if needed

    @user_id = user_id
    @organizations = current_user.organizations
  end

  def current_user
    User.find(@user_id)
  end
end

Locals should be passed to render method as usual, and possible to set 
defaults from object:

class MyRenderer < ApplicationView::Base
  def render(options={})
    super.merge(some_local: 'some_value')
  end
end

Then, the above renderer can be used anywhere, as well as be serialized and 
reused later in places like Delayed Job which doesn't have request context.

In short, bring some reusable, object-oriented niceness to views, without 
having to reconfigure everything my app should _already_ know about 
(because it's already configured in regular controller-based views)

If there is existing way to do this kind of thing, please do tell.

Thanks.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to