class Ecs::SaveGraduationCourseSupportForm
  include ActiveModel::Model

  attr_accessor :course_supports

  validates :course_supports, presence: true

  validate :check_course_support_unique
  def check_course_support_unique
    uniq_size = course_supports.map { |support| support[:ec_course_id].to_i }.uniq.size

    if uniq_size != course_supports.size
      errors.add(:course_supports, :not_unique)
    end
  end

  validate :check_weight_total
  def check_weight_total
    total = course_supports.sum { |item| item[:weights].to_f }

    if total > 1
      errors.add(:course_supports, :weights_too_big)
    end
  end
end
