-
-
Save activefx/446326 to your computer and use it in GitHub Desktop.
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 Mongoid::Document | |
include Mongoid::Timestamps | |
field :subdomain, :type => String | |
embeds_many :users | |
accepts_nested_attributes_for :users | |
validates_presence_of :name, :subdomain | |
validates_uniqueness_of :subdomain, :case_sensitive => false | |
validates_exclusion_of :subdomain, :in => %w(www support media admin help), :message => "%s is reserved" | |
validates_each :users do |document, attribute, value| | |
users = value.to_a | |
unless users.any? && users.all?(&:valid?) | |
document.errors.add(attribute, :invalid, :value => value) | |
end | |
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 User | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
embedded_in :account, :inverse_of => :users | |
devise :database_authenticatable, :recoverable, :rememberable, | |
:authentication_keys => [ :email, :subdomain ] | |
def self.find(*args) | |
options = args.extract_options! | |
user_options = Hash[*(options[:conditions] || {}).map { |k, v| [ :"users.#{k == :id ? :_id : k}", v ] }.flatten] | |
if account = Account.find(*(args + [options.merge(:conditions => user_options)])) | |
account.users.detect do |u| | |
options[:conditions].all? { |k, v| u.send(k) == v } | |
end | |
else | |
super | |
end | |
end | |
def self.find_for_authentication(conditions={}) | |
if account = Account.where(:subdomain => conditions.delete(:subdomain)).first | |
account.users.detect { |u| u.email == conditions[:email] } | |
else | |
nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What does routes.rb look like here? What would an example of seeds.rb look like when creating a single account with a single dummy user? Thanks!