Sha256: 47371444246ea28f23ae30a367c5d9732fe1dcdb8358a263fb734b9733f4bee1

Contents?: true

Size: 912 Bytes

Versions: 10

Compression:

Stored size: 912 Bytes

Contents

module Gaku
  class GradingMethodConnector < ActiveRecord::Base
    belongs_to :grading_method
    belongs_to :gradable, polymorphic: true

    validates :grading_method_id, :gradable_id, :gradable_type, presence: true

    validates(
      :gradable_type,
      inclusion: {
        in: %w(Gaku::Exam Gaku::Course),
        message: '%value is not a valid'
      }
    )

    validates :grading_method_id, uniqueness: { scope: [:gradable_type, :gradable_id] }

    default_scope { order('position ASC') }

    before_create :proper_position
    after_destroy :refresh_positions

    private

    def proper_position
      self.position = gradable.grading_method_connectors.count if gradable
    end

    def refresh_positions
      gradable.grading_method_connectors.each_with_index do |id, index|
        gradable.grading_method_connectors.where(id: id).update_all(position: index)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
gaku-0.2.4 core/app/models/gaku/grading_method_connector.rb
gaku_core-0.2.4 app/models/gaku/grading_method_connector.rb
gaku-0.2.3 core/app/models/gaku/grading_method_connector.rb
gaku_core-0.2.3 app/models/gaku/grading_method_connector.rb
gaku-0.2.2 core/app/models/gaku/grading_method_connector.rb
gaku_core-0.2.2 app/models/gaku/grading_method_connector.rb
gaku-0.2.1 core/app/models/gaku/grading_method_connector.rb
gaku_core-0.2.1 app/models/gaku/grading_method_connector.rb
gaku-0.2.0 core/app/models/gaku/grading_method_connector.rb
gaku_core-0.2.0 app/models/gaku/grading_method_connector.rb