Sha256: 0ac4a100795ddae3ea78a3c78d0bb0d0814165cdccabf97c90877cbefd98297a

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

module DragOrder
  module Models
    module Page
      
      if defined?(Page::NONDRAFT_FIELDS)
        Page::NONDRAFT_FIELDS << 'position'
      end
      
      def self.included(base)
        base.class_eval do
          
          before_validation_on_create :set_initial_position
          
          self.reflections[:children].options[:order] = "position ASC"
          
          class << self
            
            def children_of_after_position(parent, position)
              find_all_by_parent_id(parent, :conditions => [ 'position >= ?', position ])
            end
            
            def children_of_with_slug_like(parent, slug)
              find_all_by_parent_id(parent, :conditions => [ 'slug LIKE ?', slug ])
            end
            
          end
            
          def newly_created_siblings?
            self.class.find_all_by_parent_id(parent_id, :conditions => ["position is null"] ).size > 0
          end
          
          def siblings_and_self
            self.class.find_all_by_parent_id(parent_id, :order => ["position ASC"] )
          end
          
          def following_siblings
            self.class.find_all_by_parent_id(parent_id, :conditions => [ 'position > ?', position ] )
          end
          
        private
          def set_initial_position
            self.position ||= begin
              if last_sibling = Page.find_by_parent_id(parent_id, :order => [ "position DESC" ])
                last_sibling.position + 1
              else
                0
              end
            end
          end
          
        end
      end
      
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
radiant-drag_order-extension-0.3.9 lib/drag_order/models/page.rb
radiant-drag_order-extension-0.3.8 lib/drag_order/models/page.rb
radiant-drag_order-extension-0.3.7 lib/drag_order/models/page.rb