Acts as user handles multiple user roles on a rails app. It uses polymorphic associations to relate other models and behave like a user.
ActsAsUser 1.2.1 works with rails 3 onwards. You can add it to your Gemfile with:
gem 'acts_as_user'
Then run the bundle command to install it.
After you install ActsAsUser you need to run the generator:
rails g acts_as_user:install
The generator will install in initializer which describes all the ActsAsUser configuration options, so we recommend you take a look at it. When you are done you are ready to start your user model:
rails g acts_as_user User <attributes>
Next you'll probably want to run the migrations "rake db:migrate", as the generator will create a migration file (open it modify if you need to).
##Configuration
For the models you want to inherit to you just have to add this line of code into them:
class Member
acts_as_user
end
A little note on the User model...just in case!
class User
is_user
end
###Ignore attributes to delegate from the user
If you want to ignore some attributes from your user model to the childs, you can do it on the acts_as_user.rb
initializer like so:
ActsAsUser.setup do |config|
config.ignored_attributes = ["name", "bio"]
end
By default it ignores the following attributes:
["created_at", "updated_at", "id", "userable_type", "userable_id"]
##Devise support
Yes we do!
Acts as a user plays well with Devise as it ignores and adds the corresponding attributes to delegate to.
When using devise, ActsAsUser will also ignore the encrypted_password
attribute from the user. No further configuration needs to be done.
##Getting to know the user
ActsAsUser gem now adds some handy instance user methods that returns true or false wheter the current user is a specific type or not, for example:
A simple configuration may look something similar to:
class User < ActiveRecord::Base
is_user
end
class Customer < ActiveRecord::Base
acts_as_user
end
class Admin < ActiveRecord::Base
acts_as_user
end
Just a little configuration is needed, you can do it on the acts_as_user.rb
initializer like so:
ActsAsUser.setup do |config|
config.models_acting_as_users = [:admin, :customer]
end
Now we will instantiate a Customer object:
customer = Customer.find(1)
current_user = customer.user
You now should be able to detect in this case if the current_user is wheter an admin or a customer by simply calling:
current_user.customer?
=> true
current_user.admin?
=> false
Enjoy!
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
###Psst! Here is a live example in rails
- Abraham Kuri (https://github.com/kurenn)
- Patricio Beltrán (https://github.com/patobeltran)
- Support for Mongoid
- Add wiki
Icalia Labs - [email protected]
MIT License. Copyright 2012-2013 IcaliaLabs. http://icalialabs.com