2010-04-09 09:23:41 +00:00
|
|
|
class Account
|
2010-06-01 00:06:46 +00:00
|
|
|
|
|
|
|
include Locomotive::Mongoid::Document
|
2010-04-09 09:23:41 +00:00
|
|
|
|
2010-04-13 13:24:12 +00:00
|
|
|
# devise modules
|
2010-05-10 22:39:52 +00:00
|
|
|
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable
|
2010-04-13 13:24:12 +00:00
|
|
|
|
|
|
|
# attr_accessible :email, :password, :password_confirmation # TODO
|
2010-04-09 09:23:41 +00:00
|
|
|
|
2010-04-13 13:24:12 +00:00
|
|
|
## attributes ##
|
|
|
|
field :name
|
|
|
|
field :locale, :default => 'en'
|
|
|
|
|
|
|
|
## validations ##
|
|
|
|
validates_presence_of :name
|
|
|
|
|
|
|
|
## associations ##
|
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
## callbacks ##
|
|
|
|
before_destroy :remove_memberships!
|
|
|
|
|
|
|
|
## methods ##
|
|
|
|
|
2010-04-13 13:24:12 +00:00
|
|
|
def sites
|
2010-05-10 22:39:52 +00:00
|
|
|
Site.where({ 'memberships.account_id' => self._id })
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def password_required?
|
|
|
|
!persisted? || !password.blank? || !password_confirmation.blank?
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_memberships!
|
|
|
|
self.sites.each do |site|
|
|
|
|
site.memberships.delete_if { |m| m.account_id == self._id }
|
|
|
|
|
|
|
|
if site.admin_memberships.empty?
|
|
|
|
raise I18n.t('errors.messages.needs_admin_account')
|
|
|
|
else
|
|
|
|
site.save
|
|
|
|
end
|
|
|
|
end
|
2010-04-13 13:24:12 +00:00
|
|
|
end
|
2010-04-09 09:23:41 +00:00
|
|
|
|
|
|
|
end
|