Module: Sunrise::Models::Structure::ClassMethods

Defined in:
lib/sunrise/models/structure.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sunrise/models/structure.rb', line 11

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
    
    acts_as_nested_set
    set_callback :move, :after, :update_depth
    
    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

Instance Method Details



32
33
34
35
# File 'lib/sunrise/models/structure.rb', line 32

def find_by_permalink(value)
  return if value.blank?
  value.to_s.is_int? ? where(:id => value.to_i).first : where(:slug => value.to_s).first
end

#find_by_permalink!(value) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


37
38
39
40
41
# File 'lib/sunrise/models/structure.rb', line 37

def find_by_permalink!(value)
  record = find_by_permalink(value)
  raise ActiveRecord::RecordNotFound, "Couldn't find structure by #{value}" if record.nil?
  return record
end