Sha256: f620f56f76ab35bc373e3aed1a4c824b3a7eebec84452ac6cd51e32197a58859

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

# encoding: utf-8
module Sunrise
  module Models
    module Structure
      def self.included(base)
        base.send :include, InstanceMethods
        base.send :extend,  ClassMethods
      end
      
      module ClassMethods
        def self.extended(base)
          base.send(:include, Utils::Header)
          base.class_eval do
            enumerated_attribute :structure_type, :id_attribute => :kind
            enumerated_attribute :position_type, :id_attribute => :position
            
            validates_presence_of :title
            validates_numericality_of :position, :only_integer => true
            
            has_one :page, :dependent => :destroy
            
            scope :visible, where(:is_visible => true)
            scope :with_kind, proc {|structure_type| where(:kind => structure_type.id) }
            scope :with_depth, proc {|level| where(:depth => level.to_i) }
            scope :with_position, proc {|position_type| where(:position => position_type.id) }
          end
        end
        
        def find_by_permalink(value)
          value.to_s.is_int? ? find(value) : where(:slug => value.to_s).first
        end
      end
      
      module InstanceMethods
        def moveable?
          return true if new_record?
          !root?
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sunrise-core-0.1.3 lib/sunrise/models/structure.rb