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

Pass in variable into presenter #45

Open
vinnyglennon opened this issue Jan 24, 2015 · 6 comments
Open

Pass in variable into presenter #45

vinnyglennon opened this issue Jan 24, 2015 · 6 comments

Comments

@vinnyglennon
Copy link

Url: /api/v1/videos?auth_token=11112323432432

In controllors/api/v1/videos
I can easily get the user from the auth_token. I want to then make it available in the presenter, ideally via render :json => present("videos") { videos, :extra => user }

Is it possible to pass variables to presenter?
render :json => present("videos") { Widget.visible_to(current_user) } does not work in this case, as in the presenter, I want to show different information, depending on the user

I am currently storing the user_id in a thread in a controller:
Thread.current[:user_id] = 1

and accessing that in the presenter, but this is far less than ideal.

@cantino
Copy link
Contributor

cantino commented Jan 25, 2015

Hey @vinnyglennon. We expose current_user in our presenters with

module Api
  module V1
    module Helper
      def current_user
        ActiveRecord::Base.current_user
        # works in our user model, but you may need to put it on Thread.current.
      end
    end
  end
end

Brainstem::Presenter.helper(Api::V1::Helper)

@cantino
Copy link
Contributor

cantino commented Jan 25, 2015

I'm very open to discussion better methods.

@vinnyglennon
Copy link
Author

Hi,
That's fine for getting current user. I don't want to do a devise type login and hence don't have access to current_user.

Instead, my idea is that a url has an auth_token. In the controller I find the correct user, and somehow pass it to the presenter. Exposing it to presenter is my issue. The helper will never have access to it, as its only in the controller.

Currently, is there any way to pass an object to presenter directly?
Vinny
PS delighted to hear about V2 is in shunkworks, great piece of work!

@cantino
Copy link
Contributor

cantino commented Jan 25, 2015

That's a good point. I'll take a look at letting you pass objects through to presenters, unless you'd like to take a crack at it.

@vinnyglennon
Copy link
Author

Hmmm, just noticed using Thread.current is causing rspec to fail, as rspec keeps referring to an older Thread.current. It does not look like a viable solution for this :(

@cantino
Copy link
Contributor

cantino commented Feb 13, 2015

I will work on getting some sort of solution for this into the next version of Brainstem.

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

No branches or pull requests

2 participants