Sha256: 0a1f9af40cdb2a30e3779c69b25ed8ddf61a5f86deb2fa694d9fa327afb5807a

Contents?: true

Size: 1.99 KB

Versions: 10

Compression:

Stored size: 1.99 KB

Contents

# encoding: utf-8
require 'enum_field'

module Sunrise
  module Models
    module Structure
      extend ActiveSupport::Concern
      
      included do
        extend EnumField::EnumeratedAttribute

        enumerated_attribute :structure_type
        enumerated_attribute :position_type
        
        validates_presence_of :title
        validates_numericality_of :position_type_id, only_integer: true
        validates_numericality_of :structure_type_id, only_integer: true
        
        acts_as_nested_set

        before_validation :generate_slug, if: :should_generate_new_slug?
        
        scope :visible, lambda { where(is_visible: true) }
        scope :with_type, lambda {|structure_type| where(structure_type_id: structure_type.id) }
        scope :with_depth, lambda {|level| where(depth: level.to_i) }
        scope :with_position, lambda {|position_type| where(position_type_id: position_type.id) }
      end
      
      module ClassMethods
        def nested_set_options(mover = nil)
          result = []
          
          roots.each do |root|
            result += root.self_and_descendants.map do |i|
              if mover.nil? || mover.new_record? || mover.move_possible?(i)
                [yield(i), i.id]
              end
            end.compact
          end
          result
        end
      end
      
      def moveable?
        new_record? || !root?
      end
      
      def descendants_count
        (right - left - 1) / 2
      end

      protected

        def generate_slug
          self.slug = Sunrise::Utils.normalize_friendly_id(title)
        end

        def should_generate_new_slug?
          base = self.title
          slug_value = self.slug

          # If the slug base is nil, and the slug field is nil, then we're going to leave the slug column NULL.
          return false if base.nil? && slug_value.nil?
          
          # Otherwise, if this is a new record, we're definitely going to try to create a new slug.
          slug_value.blank?
        end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
sunrise-cms-1.0.6 lib/sunrise/models/structure.rb
sunrise-cms-1.0.5 lib/sunrise/models/structure.rb
sunrise-cms-1.0.4 lib/sunrise/models/structure.rb
sunrise-cms-1.0.3 lib/sunrise/models/structure.rb
sunrise-cms-1.0.2 lib/sunrise/models/structure.rb
sunrise-cms-1.0.1 lib/sunrise/models/structure.rb
sunrise-cms-1.0.0 lib/sunrise/models/structure.rb
sunrise-cms-1.0.0.rc3 lib/sunrise/models/structure.rb
sunrise-cms-1.0.0.rc2 lib/sunrise/models/structure.rb
sunrise-cms-1.0.0.rc1 lib/sunrise/models/structure.rb