0

I've notice the new features of concern in rails4, and I read the document of it in http://edgeapi.rubyonrails.org/classes/ActiveSupport/Concern.html. But it seems doesn't work as I expect. Here is my code of my /models/concerns/current_user.rb.

require 'active_support/concern'

module CurrentUser
  extend ActiveSupport::Concern

  module ClassMethods
    def accessor_current_user
      attr_accessor :current_user
    end
  end

end

class ActiveRecord::Base
  include CurrentUser
end

You probably notice that last 3 lines of my code, that's because I want all my models can call the method and I think it may be a good way to achieve that. But when i start rails server, it just cannot call the accessor_current_user method. So I am confusing about this. I really don't know the reason. Hope someone can help me. :)


Update! Finally, i found it's maybe a good way to create a ActiveRecord::Base class in the initializers fold, and then include the CurrentUser in the class.

2 Answers 2

1

Try:

require 'active_support/concern'

module CurrentUser
  extend ActiveSupport::Concern

  included do
    attr_accessor :current_user
  end

  module ClassMethods
  end

end

class ActiveRecord::Base
  include CurrentUser
end

Then you should be able to do something like this:

foo = Foo.new
foo.current_user

Where Foo is:

class Foo < ActiveRecord:Base
  include CurrentUser
end
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer, can you explain this? I tried to add the last 2 lines but still can't work.
The last 2 lines are only here to illustrate. You should not add them. The basic ideas is to add an attr_accessor on any class that include your CurrentUser module. This is done with the included block.
Well, I know if I add include CurrentUser in the Foo class can work well, but that isn't what i want. Suppose you have 8 models need to call the accessor_current_user method, so in your way you need to add include CurrentUser in all the 8 models, i don't think it's best way. So i am wondering if i can write some code to achieve that goal.
Including your CurrentUser module for every ActiveRecord::Base sub classes is not a good idea.
0

The way you are calling accessor_current_user is wrong. This way you can call ActiveRecord::Base.accessor_current_user method and it will execute the code without any error. And then you can call

ar = ActiveRecord::Base.new
ar.current_user = "foo"
ar.current_user # this will return 'foo'

But the correct way to implement the attr_accessor using ActiveSupport::Concern is

module CurrentUser
  extend ActiveSupport::Concern

  included do
    attr_accessor :current_user
  end

end

If you are writing your code in rails, then you don't need require 'active_support/concern'. Please refer http://blog.neerajk.com/articles/2014-11-26-is-active-support-concern-really-another-concern/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.