https://github.com/sixarm/sixarm_ruby_current_user
SixArm.com » Ruby » Get the current user of a Ruby on Rails app
https://github.com/sixarm/sixarm_ruby_current_user
current-user gem rails ruby
Last synced: 7 months ago
JSON representation
SixArm.com » Ruby » Get the current user of a Ruby on Rails app
- Host: GitHub
- URL: https://github.com/sixarm/sixarm_ruby_current_user
- Owner: SixArm
- License: other
- Created: 2010-10-18T00:20:23.000Z (over 15 years ago)
- Default Branch: main
- Last Pushed: 2023-09-15T19:27:52.000Z (over 2 years ago)
- Last Synced: 2025-02-06T00:25:46.243Z (about 1 year ago)
- Topics: current-user, gem, rails, ruby
- Language: Ruby
- Homepage: http://sixarm.com
- Size: 326 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
# SixArm.com → Ruby →
CurrentUser module with current_user methods
* Doc:
* Gem:
* Repo:
## Introduction
Get and set the current user in the Rails session array.
When you set the current user, this does:
- @current_user = user
- @current_user_id = user.id
- session[:current_user_id] = user.id
For docs go to
Want to help? We're happy to get pull requests.
## Install
### Gem
To install this gem in your shell or terminal:
gem install sixarm_ruby_current_user
### Gemfile
To add this gem to your Gemfile:
gem 'sixarm_ruby_current_user'
### Require
To require the gem in your code:
require 'sixarm_ruby_current_user'
## Example code
joe = User.find(123)
self.current_user = joe
=>
@current_user == joe
@current_user_id == 123
session[:current_user_id] == 123
## Example controller
class MyController < ApplicationController
def sign_in(user)
self.current_user = user
end
def sign_out
self.current_user = nil
end
def is_anyone_using_this?
current_user?
end
end
## Example of reloading
For fast speed, we memoize current_user and current_user_id:
we use fast instance variables @current_user and @current_user_id
rather than reading the slower session[:current_user_id] each time.
To reload @current_user and @current_user_id from session[:current_user_id],
we use the :reload parameter like this:
current_user(:reload => true)
## Why use the self prefix?
When we set variables, we must use the "self" prefix because Ruby uses this to do method dispatch.
Right:
self.current_user = joe
Wrong:
current_user = joe