https://github.com/icalialabs/acts_as_user
A gem which handles multiple types of users on a rails app
https://github.com/icalialabs/acts_as_user
devise gem polymorphic-relationships rails ruby user-handling
Last synced: 3 months ago
JSON representation
A gem which handles multiple types of users on a rails app
- Host: GitHub
- URL: https://github.com/icalialabs/acts_as_user
- Owner: IcaliaLabs
- License: mit
- Created: 2013-12-11T22:14:51.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2020-06-13T01:59:36.000Z (over 5 years ago)
- Last Synced: 2025-10-06T23:14:55.835Z (3 months ago)
- Topics: devise, gem, polymorphic-relationships, rails, ruby, user-handling
- Language: Ruby
- Size: 38.1 KB
- Stars: 24
- Watchers: 6
- Forks: 6
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Acts as user
[](https://travis-ci.org/IcaliaLabs/acts_as_user)
[](https://codeclimate.com/github/IcaliaLabs/acts_as_user)
[](https://gemnasium.com/IcaliaLabs/acts_as_user)
[](http://badge.fury.io/rb/acts_as_user)
Acts as user handles multiple user roles on a rails app. It uses polymorphic associations to relate other models and behave like a user.
## Getting started
ActsAsUser 1.2.1 works with rails 3 onwards. You can add it to your Gemfile with:
```ruby
gem 'acts_as_user'
```
Then run the bundle command to install it.
After you install ActsAsUser you need to run the generator:
```console
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:
```console
rails g acts_as_user User
```
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:
```ruby
class Member
acts_as_user
end
```
A little note on the User model...just in case!
```ruby
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:
```ruby
ActsAsUser.setup do |config|
config.ignored_attributes = ["name", "bio"]
end
```
By default it ignores the following attributes:
```ruby
["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:
```ruby
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:
```ruby
ActsAsUser.setup do |config|
config.models_acting_as_users = [:admin, :customer]
end
```
Now we will instantiate a Customer object:
```console
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:
```console
current_user.customer?
=> true
current_user.admin?
=> false
```
Enjoy!
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
### Psst! Here is a live example in rails
[Rails acts as user example](https://github.com/IcaliaLabs/acts_as_user_example)
### Devs
* Abraham Kuri (https://github.com/kurenn)
* Patricio Beltrán (https://github.com/patobeltran)
### Future
* Support for Mongoid
* Add wiki
## Credits
Icalia Labs - weare@icalialabs.com
[Follow us](http://twitter.com/icalialabs "Follow us")
[Like us on Facebook](https://www.facebook.com/icalialab "Like us on Facebook")
### License
MIT License. Copyright 2012-2013 IcaliaLabs. http://icalialabs.com