toptal / granite

Business Actions architecture for Rails apps

Home Page:https://toptal.github.io/granite/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Projector with `Granite.view_context` is always `nil`

jonatas opened this issue · comments

I'm trying to create the first projector for the example_granite_application and it fails when I try to inject the current_user as the performer.

In a simple version, I'm creating an InlineProjector and trying to use it:

class InlineProjector < Granite::Projector

  post :perform, as: '' do
    if action.perform!
      redirect_to projector.success_redirect, notice: t('.notice')
    else
      messages = projector.action.errors.full_messages.to_sentence
      redirect_to projector.failure_redirect, alert:  t('.error', messages)
    end
  end

  def final_success_response
    { success: true }
  end
  private
  def build_action(*args)
      action_class.as(self.class.proxy_performer || h.current_user).new(*args)
  end
end

it fails to try to call h.current_user because h is nil.
Any recommendation?

Very cool! I figured out what was wrong.
I forget to setup the granite view context in the application controller:

  around_action :setup_granite_view_context
  before_action { view_context }

  protected
  def setup_granite_view_context(&block)
    Granite.with_view_context(view_context, &block)
  end

I also documented it in our tutorial.