Question

I'm working on a Padrino application that has a main App and an Admin app. When a user logins through session, I just run the following two lines to log him in.

account = Account.authenticate(params[:email], params[:password])
set_current_account(account)

With debugger in any controllers in Admin

current_account
#<Account @id=1 @name="John" @surname="Davies" @email="john.davies@gmail.com" @crypted_password="3456789" @role="admin">

With debugger in any models

current_account
*** NameError Exception: undefined local variable or method `current_account' for #<Post @id=1 @question="Why is this not working?" @answer="I have no idea">

I am able to access current_account to find out which user is logged in in the Admin app, but this variable is not accessible in the main App.

What I am trying to do is that I am creating an Activity Feed, or Audit Trail, for all my models, so when a record is created/updated/destroyed in any model, a new record is created in model Activity. That being said, I need to access the current_account variable in my models.

I have searched for solutions, and came across a suggestion:

In admin/app.rb

enable  :sessions
set :session_id, "my-global-session"

In app/app.rb

register Padrino::Admin::AccessControl
register Padrino::Admin::Helpers

enable :sessions
set :session_id, "my-global-session"

It did not work for me. Is there a way I can access current_account in my models?

Thank you for any guide or suggestion.

Was it helpful?

Solution

I'm not exactly sure what are you trying to do, but I solved a similar problem like this:

class Account
  # ...
  class << self
    attr_accessor :current
  end
  # ...
end

class Admin < Padrino::Application
  #...
  before do
    Account.current = current_account
  end
  # ...
end

And then use Account.current to access it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top