Skip to content

Instantly share code, notes, and snippets.

@bismark64
Created December 15, 2014 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bismark64/c9bb4bc734f8a5b1e4de to your computer and use it in GitHub Desktop.
Save bismark64/c9bb4bc734f8a5b1e4de to your computer and use it in GitHub Desktop.
Devise + MongoDB embedded users
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
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