Sha256: f1021bb45379cb0d6c99230fd00eccbadbb27a5fe9b9e97b79384a87c1f1424b

Contents?: true

Size: 1016 Bytes

Versions: 3

Compression:

Stored size: 1016 Bytes

Contents

require File.dirname(__FILE__)+'/ranked-model/ranker'
require File.dirname(__FILE__)+'/ranked-model/railtie' if defined?(Rails::Railtie)

module RankedModel

  # Signed MEDIUMINT in MySQL
  #
  MAX_RANK_VALUE = 8388607
  MIN_RANK_VALUE = -8388607

  def self.included base

    base.class_eval do
      class_attribute :rankers

      extend RankedModel::ClassMethods

      before_save :handle_ranking

      scope :rank, lambda { |name|
        order ranker(name.to_sym).column
      }
    end

  end

  private

  def handle_ranking
    self.class.rankers.each do |ranker|
      ranker.with(self).handle_ranking
    end
  end

  module ClassMethods

    def ranker name
      rankers.find do |ranker|
        ranker.name == name
      end
    end

  private

    def ranks *args
      self.rankers ||= []
      ranker = RankedModel::Ranker.new(*args)
      self.rankers << ranker
      attr_accessor "#{ranker.name}_position"
      public "#{ranker.name}_position", "#{ranker.name}_position="
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ranked-model-0.3 lib/ranked-model.rb
ranked-model-0.2.1 lib/ranked-model.rb
ranked-model-0.2.0 lib/ranked-model.rb