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

current_user not available in "visible do" block #549

Closed
dannybtran opened this issue Jul 13, 2011 · 20 comments
Closed

current_user not available in "visible do" block #549

dannybtran opened this issue Jul 13, 2011 · 20 comments
Assignees
Milestone

Comments

@dannybtran
Copy link

Not sure if this is the right place to post this but figure I'd try anyway.

I'm trying to implement something similar to this:

RailsAdmin.config do |config|
  config.model Team do
    list do
      field :name
      field :created_at
      field :revenue do
        visible do
          current_user.roles.include?(:accounting) # metacode
        end
      end
    end
  end
end

I know in the README it says the example is theoretical, but I keep getting that current_user is not found within that block. I'm authorizing with CanCan:

RailsAdmin.authorize_with :cancan

At the top of rails_admin.rb in config/initializers

Can anyone tell me how to get current_user available within the visible block? I'd like to only show certain fields in the "List" view if a user is an admin.

@bbenezech
Copy link
Collaborator

Try bindings[:view].controller.current_user or just bindings[:view].current_user

@dannybtran
Copy link
Author

bbenezech, thanks for the suggestion. Unfortunately it did not work. I YAML::dump'd the bindings variable in the visible block and it was null, as well as bindings[:view] ... I guess that's only available in the config.edit block?

@ghost ghost assigned kaapa Jul 14, 2011
@sferik
Copy link
Collaborator

sferik commented Jul 14, 2011

Assigning @kaapa, since config is his "zone".

@crosebrugh
Copy link
Contributor

Are you sure that 'visible' is the right thing to use? I use 'show' in both the list and edit blocks and it works fine:

  field :active do
    show do
      o = bindings[:object]
      User.current.super? && !o.super?
    end
  end

@bbenezech
Copy link
Collaborator

Fixed meanwhile

@svoynow
Copy link

svoynow commented Nov 11, 2011

I'm still having this exact problem
My devise model is called Admin so I've got:

  config.current_user_method do
    current_admin
  end
  config.model Admin do
    show do
      field :id
      field :email
      field :last_sign_in_ip do
        visible do
          current_user.email == "foo@bar.com"
        end
      end
    end
  end

Which gives me:

undefined local variable or method `current_user' for #RailsAdmin::Config::Fields::Types::String:0x79a0ce4c

Anybody have any ideas?

@bbenezech
Copy link
Collaborator

You can call your current_admin or rails_admin's _current_user.

@svoynow
Copy link

svoynow commented Nov 13, 2011

Both of those result in a similar undefined local variable error ( for _current_user or current_admin, respectively)

Sent from my iPad

On Nov 12, 2011, at 5:29 AM, Benoit Bénézechreply@reply.github.com wrote:

You can call your current_admin or rails_admin's _current_user.


Reply to this email directly or view it on GitHub:
#549 (comment)

@bbenezech
Copy link
Collaborator

bindings[:view]._current_user

@svoynow
Copy link

svoynow commented Nov 14, 2011

Great, that seems to work. Thanks!
Just out of curiosity, what's actually going on here? Are the other forms
supposed to work? And if so, what should I investigate to figure out why
they're not?

Cheers,

Sasha

On Sun, Nov 13, 2011 at 11:52 PM, Benoit Bnzech <
reply@reply.github.com

wrote:

bindings[:view]._current_user


Reply to this email directly or view it on GitHub:
#549 (comment)

Sasha Voynow
Lab Zero
Pier 38, The Embarcadero
San Francisco CA 94107
510 859 3206
skype: sasha.voynow
www.labzero.com

@bbenezech
Copy link
Collaborator

_current_user is the RailsAdmin mapping of your current_admin method.
Both are accessible from the view, that you can access in the DSL with bindings[:view]

Cheers!

@versilov
Copy link
Contributor

Does not work in my case:

    list do
      include_fields :id, :title, :body
      field :admin_user do
        visible do
          bindings[:view]._current_user.super_admin?
        end
      end

It gives me
NoMethodError in RailsAdmin::MainController#index
private method `_current_user' called for #RailsAdmin::MainController:0xbeceb94

For show and edit configs this constructions works fine.

@versilov
Copy link
Contributor

But it worked when I used

bindings[:view].current_admin_user.super_admin?

admin_user is my user model name.

@bbenezech bbenezech reopened this Dec 31, 2011
@bbenezech
Copy link
Collaborator

Shouldn't be private. Use send meanwhile.

@bbenezech
Copy link
Collaborator

There is a helper method for _current_user:

https://github.com/sferik/rails_admin/blob/master/app/controllers/rails_admin/application_controller.rb#L16

And there is a test case that passes:

https://github.com/sferik/rails_admin/blob/master/spec/requests/rails_admin_spec.rb#L51

You say it only happens with list, I'm gonna have a look.

@RedXVII
Copy link

RedXVII commented Apr 29, 2013

Hello, The method _current_user is still not public.

config.model 'Model' do
    visible do 
      # this does not work, _current_user is private
      bindings[:controller]._current_user.admin 
    end
end

binding[:view] cannot be used in that scope either.

Thank you

@gokure
Copy link
Contributor

gokure commented May 29, 2013

@RedXVII +1

@earnold
Copy link

earnold commented Oct 28, 2013

@RedXVII +1

@StasKoval
Copy link

RedXVII +1

@Loremaster
Copy link

@RedXVII +10

@mshibuya mshibuya reopened this Aug 5, 2014
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