Module: Classiccms::Routing

Included in:
AdminController, ApplicationController, CMSController
Defined in:
lib/classiccms/lib/routing.rb

Instance Method Summary collapse

Instance Method Details

#get_first_itemObject

in charge of get the first item of a given model (for menu items etc.)



5
6
7
8
9
10
11
12
# File 'lib/classiccms/lib/routing.rb', line 5

def get_first_item
  Base.where(:_type => CONFIG[:model]).each do |item|
    if item.connections.where(:parent_id => nil, :section => CONFIG[:section], :order_id.lte => 1).count > 0
      return item
    end
  end
  return nil
end

#get_route(current, routes = []) ⇒ Object

This method will get you the most awesome route through a tree! (OMG!)



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/classiccms/lib/routing.rb', line 15

def get_route(current, routes = [])
  if current.kind_of? Base
    routes << current.id
    branches = Array.new
    current.connections.each_with_index do |connection, i|
      if connection.parent_id != nil and Base.where(:_id => connection.parent_id).count > 0
        branches[i] = get_route(Base.find(connection.parent_id))
      end
    end
    new = Array.new
    branches.each do |branch|
      if branch != nil and (new.count == 0 or new.count > branches.count)
        new = branch
      end
    end
    routes += new
  end
  routes
end