Sha256: 02813ccc6a1888e2e9ba0341e506561f450cb7fad1b15616391b91fbeaf7ff4d

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

class ProjectMember
  include ActiveModel::Model

  attr_accessor :email, :name, :role, :project
  attr_reader :user, :project_user

  validates :email,    presence: true
  validates :name,     presence: true
  validates :role,     presence: true
  validates :project,  presence: true

  def save
    return false unless valid? && find_or_create_user

    @project_user = ProjectUserCreator.create user: user, project: project,
      role: role

    if project_user.persisted?
      true
    else
      merge_project_user_errors
      false
    end
  end

  private

  def find_or_create_user
    @user = User.find_by_email(email) ||
      UserCreator.create(name: name, email: email)

    if user.persisted?
      true
    else
      merge_user_errors
      false
    end
  end

  def merge_user_errors
    user.errors[:email].each { |error| errors.add :email, error }
    user.errors[:name].each  { |error| errors.add :name, error }
  end

  def merge_project_user_errors
    if project.users.include? user
      errors.add :email, "appears to already be associated to project."
    end

    project_user.errors[:role].each do |error|
      errors.add :role, error
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
self_systeem-0.1.0 test/dummy_app/app/models/project_member.rb