class ApplicationDraft < ActiveRecord::Base

  include HasSeason

  belongs_to :team

  validates :team, presence: true
  validates :coaches_hours_per_week, :coaches_why_team_successful, presence: true, on: :apply

  before_validation :set_current_season

  attr_accessor :current_user

  Role::TEAM_ROLES.each do |role|
    define_method "as_#{role}?" do                       # def as_student?
      team.send(role.pluralize).include? current_user    #   team.students.include? current_user
    end                                                  # end
  end

  def students
    if as_student?
      [ current_student, current_pair ].compact
    else
      team.students.order(:id)
    end.map { |user| Student.new(user) }
  end

  def current_student
    @current_student ||= team.students.detect{ |student| student == current_user }
  end

  def current_pair
    @current_pair ||= (team.students - [current_student]).first
  end

  def role_for(user)
    draft = dup.tap { |d| d.current_user = user }
    if draft.as_student?
      'Student'
    elsif draft.as_coach?
      'Coach'
    elsif draft.as_mentor?
      'Mentor'
    end
  end

  def ready?
    false # valid?(:apply)
  end

  private

  def set_current_season
    self.season ||= Season.current
  end
end

class ApplicationDraft < ActiveRecord::Base

  include HasSeason

  belongs_to :team

  validates :team, presence: true
  validates :coaches_hours_per_week, :coaches_why_team_successful, presence: true, on: :apply

  before_validation :set_current_season

  attr_accessor :current_user

  Role::TEAM_ROLES.each do |role|
    define_method "as_#{role}?" do                       # def as_student?
      team.send(role.pluralize).include? current_user    #   team.students.include? current_user
    end                                                  # end
  end

  def method_missing(method, *args, &block)
    if match = /^student([01])_(.*)/.match(method.to_s) and index = match[1].to_i and field = match[2]
      students[index].send(field) if students[index]
    else
      super
    end
  end

  def students
    if as_student?
      [ current_student, current_pair ].compact
    else
      team.students.order(:id)
    end.map { |user| Student.new(user) }
  end

  def current_student
    @current_student ||= team.students.detect{ |student| student == current_user }
  end

  def current_pair
    @current_pair ||= (team.students - [current_student]).first
  end

  def role_for(user)
    draft = dup.tap { |d| d.current_user = user }
    if draft.as_student?
      'Student'
    elsif draft.as_coach?
      'Coach'
    elsif draft.as_mentor?
      'Mentor'
    end
  end

  def ready?
    false # valid?(:apply)
  end

  private

  def set_current_season
    self.season ||= Season.current
  end
end

Add proxy methods for students' attributes
Add proxy methods for students' attributes
