sentient_model
sentient_model is simple piece of code trying to solve a common problem.
Imagine you are writing an authentication system for Ruby on Rails. Usually you would define a method called current_user
in your ApplicationController
and expose it to your views and so on (which is fine in most cases).
However, if you need current_user
in locations where you can't easily expose the method, sentient_model is the perfect solution. Instead of manually exposing current_user
, you would just rely on User.current
. sentient_model's methods are globally accessible - therefore you don't have to worry about anything - it just works.
This Gem has been inspired by David Bock's sentient_user.
Usage
Define your models as usual and include the module SentientModel
:
class YourModel
include SentientModel
# more code such as validations or associations
end
By including SentientModel
, the following methods are added to your model:
YourModel#make_current # instance method - makes the callee object the sentient object
YourModel#current? # instance method - true if the callee is the current sentient object or false otherwise
YourModel.current # class method - returns the sentient object or nil
YourModel.current=(m) # class method - sets the sentient object to the supplied argument (must be an instance of YourModel)
YourModel.has_current? # class method - true if a sentient object defined or false otherwise
Compatibility
- MRI Ruby 1.8.7
- MRI Ruby 1.9.2
- JRuby 1.6
How to contribute?
- Fork on GitHub.
- Make sure, that all specs are still passing (run
rake spec
). - Send Pull Request.
Copyright
Copyright ©2011 Christoph Schiessl. See LICENSE for details.