Sha256: 30c95d74028884ed485840540bed96de365283b822bf0444c905f5a6427850bb

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

require 'mongoid-sortable/version'
require 'active_support/concern'

module Mongoid
  module Sortable
    extend ActiveSupport::Concern
    
    included do
      cattr_accessor :mongoid_sortable_options
      self.mongoid_sortable_options = {}

      field :position, type: Integer, default: 1
      
      before_create :set_position
      after_destroy :set_sibling_positions
    end

    module ClassMethods
      def sortable(options = {})
        options = {
          scope: []
        }.merge(options)

        options[:scope].map!(&:to_sym)

        self.mongoid_sortable_options = options
      end
    end
    
    def previous
      position_scope.lt(position: position).order_by('position desc').first
    end
    
    def next
      position_scope.gt(position: position).order_by('position asc').first
    end
    
    def reorder(ids)
      ids.map!(&:to_s) # ensure entities are strings
      ids.each_with_index do |id, index|
        position_scope.find(id).set(position: index + 1)
      end
    end
    
    def position_at(new_position)
      position_scope.gt(position: position).each{|d| d.inc(position: -1) }
      set(position: nil)
      position_scope.gte(position: new_position).each{|d| d.inc(position: 1) }
      set(position: new_position)
    end
        
    def set_position
      self.position = position_scope.count + (embedded? ? 0 : 1)
    end
    
    def set_sibling_positions
      position_scope.gt(position: position).each{|d| d.inc(position: -1) }
    end

    def relation
      (embedded? ? send(self._association.inverse).send(self._association.name) : self.class).unscoped.scoped
    end

    def position_scope(options = {})
      scopes = Array(mongoid_sortable_options[:scope])
      scopes.inject(relation) do |criteria, scope_field|
        criteria.where(scope_field => self.send(scope_field))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-sortable-1.0.0 lib/mongoid-sortable.rb