Sha256: a2948ad05c0231172fd6dc1221555852f43ab6b9bda095b8d44229222eecb5c5

Contents?: true

Size: 890 Bytes

Versions: 10

Compression:

Stored size: 890 Bytes

Contents

module Gaku
  class GradingMethodSetItem < ActiveRecord::Base
    belongs_to :grading_method
    belongs_to :grading_method_set

    validates :grading_method_set_id, :grading_method_id, presence: true

    validates :grading_method_id,
              uniqueness: {
                            scope: :grading_method_set_id,
                            message: I18n.t(:'grading_method_set_item.already')
                          }

    before_create :proper_position
    after_destroy :refresh_positions

    private

    def proper_position
      return unless grading_method_set
      self.position = grading_method_set.grading_method_set_items.count
    end

    def refresh_positions
      items = grading_method_set.grading_method_set_items
      items.pluck(:id).each_with_index do |id, index|
        items.update_all( { position: index }, { id: id } )
      end
    end

  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
gaku-0.1.1 core/app/models/gaku/grading_method_set_item.rb
gaku_core-0.1.1 app/models/gaku/grading_method_set_item.rb
gaku-0.1.0 core/app/models/gaku/grading_method_set_item.rb
gaku_core-0.1.0 app/models/gaku/grading_method_set_item.rb
gaku_core-0.0.3 app/models/gaku/grading_method_set_item.rb
gaku-0.0.3 core/app/models/gaku/grading_method_set_item.rb
gaku-0.0.2 core/app/models/gaku/grading_method_set_item.rb
gaku_core-0.0.2 app/models/gaku/grading_method_set_item.rb
gaku-0.0.1 core/app/models/gaku/grading_method_set_item.rb
gaku_core-0.0.1 app/models/gaku/grading_method_set_item.rb