engine/app/models/membership.rb

59 lines
1.1 KiB
Ruby
Raw Normal View History

class Membership
include Locomotive::Mongoid::Document
## fields ##
field :role, :default => 'author'
## associations ##
2011-05-16 21:06:46 +00:00
referenced_in :account, :validate => false
embedded_in :site, :inverse_of => :memberships
## validations ##
validates_presence_of :account
## callbacks ##
before_save :define_role
## methods ##
Ability::ROLES.each do |_role|
define_method("#{_role}?") do
self.role == _role
end
end
def email; @email; end
def email=(email)
@email = email
self.account = Account.where(:email => email).first
end
def process!
if @email.blank?
self.errors.add_on_blank(:email)
:error
2010-07-29 23:21:48 +00:00
elsif self.account.blank?
:create_account
2011-07-05 16:30:12 +00:00
elsif self.site.memberships.any? { |m| m.account_id == self.account_id && m._id != self._id }
self.errors.add(:base, 'Already created')
2011-07-05 16:30:12 +00:00
:already_created
else
self.save
:save_it
end
end
def ability
@ability ||= Ability.new(self.account, self.site)
end
protected
def define_role
self.role = Ability::ROLES.include?(role.downcase) ? role.downcase : Ability::ROLES.first
end
end