Sha256: 8af8e904c725cc241775a04b24c906aa2c65f697b79dc9dbdeb047945480fa91

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

module Effective
  class MentorshipGroupUser < ActiveRecord::Base
    # Effective scoped
    belongs_to :mentorship_cycle, class_name: 'Effective::MentorshipCycle'

    # App scoped
    belongs_to :mentorship_group
    belongs_to :user, polymorphic: true

    effective_resource do
      mentorship_role   :string

      # Copied from user
      name    :string
      email   :string

      position  :integer

      timestamps
    end

    scope :deep, -> { includes(:mentorship_cycle, :mentorship_group, :user) }
    scope :sorted, -> { order(:mentorship_cycle_id).order(:position) }

    # Assign position and cycle
    before_validation(if: -> { mentorship_group.present? }) do
      self.position ||= (mentorship_group.mentorship_group_users.map(&:position).compact.max || -1) + 1
      self.mentorship_cycle = mentorship_group.mentorship_cycle
    end

    # Denormalized data for searching
    before_validation(if: -> { user.present? }) do
      assign_attributes(name: user.to_s, email: user.email)
    end

    validates :user_id, uniqueness: { scope: [:mentorship_group_id] }

    def to_s
      user.to_s.presence || model_name.human
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
effective_mentorships-0.2.1 app/models/effective/mentorship_group_user.rb
effective_mentorships-0.2.0 app/models/effective/mentorship_group_user.rb
effective_mentorships-0.1.0 app/models/effective/mentorship_group_user.rb