2010-05-10 22:39:52 +00:00
|
|
|
class Membership
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-01 00:06:46 +00:00
|
|
|
include Locomotive::Mongoid::Document
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
## fields ##
|
|
|
|
field :admin, :type => Boolean, :default => false
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
## associations ##
|
2011-05-16 21:06:46 +00:00
|
|
|
referenced_in :account, :validate => false
|
2010-05-10 22:39:52 +00:00
|
|
|
embedded_in :site, :inverse_of => :memberships
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
## validations ##
|
|
|
|
validates_presence_of :account
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
## methods ##
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
def email; @email; end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
def email=(email)
|
|
|
|
@email = email
|
|
|
|
self.account = Account.where(:email => email).first
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-13 00:46:17 +00:00
|
|
|
def process!
|
2010-05-10 22:39:52 +00:00
|
|
|
if @email.blank?
|
2010-07-13 00:46:17 +00:00
|
|
|
self.errors.add_on_blank(:email)
|
2010-05-10 22:39:52 +00:00
|
|
|
:error
|
2010-07-29 23:21:48 +00:00
|
|
|
elsif self.account.blank?
|
2010-05-10 22:39:52 +00:00
|
|
|
:create_account
|
|
|
|
elsif self.site.memberships.find_all { |m| m.account_id == self.account_id }.size > 1
|
2010-07-13 00:46:17 +00:00
|
|
|
self.errors.add(:base, 'Already created')
|
2010-05-10 22:39:52 +00:00
|
|
|
:nothing
|
|
|
|
else
|
2010-07-13 00:46:17 +00:00
|
|
|
self.save
|
2010-05-10 22:39:52 +00:00
|
|
|
:save_it
|
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
|
|
|
end
|