Sha256: dd92ed119d70fc8e5b2f4e0bd63333e45d3cf53efb5d9d19a9de2cd9476a0107

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

class Member < ActiveRecord::Base

  include Ext::DeviseConfiguration
  include Ext::Integrations::Member

  has_many    :memberships
  belongs_to  :organization
  belongs_to  :person

  validates :organization,  :presence => true
  validates :member_number,  :presence => true

  before_create :set_member_number

  def self.generate_password
    Devise.friendly_token
  end

  def active_memberships
    memberships.select{ |m| m.starts_at < DateTime.now && m.ends_at > DateTime.now }
  end

  def set_member_number
    self.member_number = MemberNumberGenerator.next_number_for(self.organization)
  end

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

  def active_for_authentication?
    super && !suspended?
  end

  def self.find_by_membership(membership)
    Member.find_by_email_and_organization_id(membership.item.order.person.email, membership.organization)    #TODO: Biggest join ever
  end

  #
  # * Creates (and invites) a member for this membership if one does not already exist
  # * Attaches that member to the applicable person
  #
  def self.for(membership)
    member = Member.find_by_membership(membership)
    if member.nil?
      member = Member.new({:email => membership.item.order.person.email})
      member.organization = membership.item.order.organization
      member.person = membership.item.order.person
      member.invite!
    end

    membership.member = member
    membership.save

    member
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
artfully_ose-1.2.0.pre.2 app/models/member.rb
artfully_ose-1.2.0.pre.1 app/models/member.rb
artfully_ose-1.2.0.pre app/models/member.rb