Created
December 15, 2014 13:51
-
-
Save bismark64/c9bb4bc734f8a5b1e4de to your computer and use it in GitHub Desktop.
Devise + MongoDB embedded users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Account | |
include Regexs | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
#has_many :users | |
embeds_many :users, store_as: :credentials | |
scope :for, ->(current_user_id){ where('credentials._id' => current_user_id) } | |
scope :via_oauth, ->(auth){ where('credentials.provider' => auth.provider, 'credentials.uid' => auth.uid) } | |
def find_user(user_id) | |
users.find(user_id) unless user_id.nil? | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
def current_user_id | |
session["warden.user.user.key"].first.first if session["warden.user.user.key"].present? | |
end | |
def current_user | |
return @current_user unless @current_user.nil? | |
account = @current_account || Account.for(current_user_id).first | |
@current_user ||= account.find_user(current_user_id) if account.present? | |
end | |
def current_account | |
@current_account ||= @current_user.account if @current_user.present? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment