Sha256: 74df1a558f481c9beb51682cac76956795f57fa55b41da4fc98f34410e2069e9

Contents?: true

Size: 1.93 KB

Versions: 30

Compression:

Stored size: 1.93 KB

Contents

class Invitation < ActiveRecord::Base
  belongs_to :account
  validates_presence_of :account_id
  validates_presence_of :email
  has_and_belongs_to_many :projects

  after_create :deliver_invitation

  attr_accessor :new_user_name, :new_user_password,
    :new_user_password_confirmation, :existing_user_password
  attr_writer :new_user_email, :existing_user_email
  attr_reader :user
  attr_protected :account_id

  validate :validate_accepting_user, :on => :update

  def account_name
    account.name
  end

  def accept(attributes)
    self.attributes = attributes
    @user = existing_user || new_user
    if valid?
      @user.save!
      @user.memberships.create!(:account  => account,
                                :admin    => admin,
                                :projects => projects)
    end
  end

  def new_user_email
    @new_user_email ||= email
  end

  def existing_user_email
    @existing_user_email ||= email
  end

  private

  def deliver_invitation
    InvitationMailer.invitation(self).deliver
  end

  def existing_user
    if existing_user?
      User.find_by_email(existing_user_email)
    end
  end

  def existing_user?
    existing_user_password.present?
  end

  def new_user
    User.new(
      :email                 => new_user_email,
      :password              => new_user_password,
      :password_confirmation => new_user_password_confirmation,
      :name                  => new_user_name
    )
  end

  def validate_accepting_user
    if existing_user?
      validate_existing_user
    else
      validate_new_user
    end
  end

  def validate_new_user
    user.valid?
    user.errors.each do |field, message|
      errors.add("new_user_#{field}", message)
    end
  end

  def validate_existing_user
    if existing_user.nil?
      errors.add(:existing_user_email, "isn't signed up")
    elsif !existing_user.authenticated?(existing_user_password)
      errors.add(:existing_user_password, "is incorrect")
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
saucy-0.2.18 app/models/invitation.rb
saucy-0.2.17 app/models/invitation.rb
saucy-0.2.16 app/models/invitation.rb
saucy-0.2.15 app/models/invitation.rb
saucy-0.2.14 app/models/invitation.rb
saucy-0.2.13 app/models/invitation.rb
saucy-0.2.12 app/models/invitation.rb
saucy-0.2.11 app/models/invitation.rb
saucy-0.2.10 app/models/invitation.rb
saucy-0.2.9 app/models/invitation.rb
saucy-0.2.8.1 app/models/invitation.rb
saucy-0.2.8 app/models/invitation.rb
saucy-0.2.7 app/models/invitation.rb
saucy-0.2.6.1 app/models/invitation.rb
saucy-0.2.6 app/models/invitation.rb
saucy-0.2.5 app/models/invitation.rb
saucy-0.2.4.1 app/models/invitation.rb
saucy-0.2.4 app/models/invitation.rb
saucy-0.2.3 app/models/invitation.rb
saucy-0.2.2 app/models/invitation.rb