Class: Sitemap

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/sitemap.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_path(*path_args) ⇒ Object

Private Methods



116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/sitemap.rb', line 116

def self.find_by_path(*path_args)
  path_args.flatten!
  path = path_args.reverse
  path.delete(nil)
  obj = Sitemap.main_menu
  while (path_piece=path.pop)
    obj = obj.children.select{|t| t.menu_text.downcase==path_piece.downcase.gsub(/_/, ' ')}[0]
    break unless obj
  end
  return obj
end

.from_request_params(params) ⇒ Object

Class Methods



83
84
85
86
# File 'app/models/sitemap.rb', line 83

def self.from_request_params(params)
  puts params.slice(:level1, :level2, :level3, :level4).values.inspect + "\n"*30
  find_by_path params.slice(:level1, :level2, :level3, :level4).values
end

.lost_and_foundObject



88
89
90
# File 'app/models/sitemap.rb', line 88

def self.lost_and_found
  root.children.select{|t| t.menu_text=='Lost + Found'}[0]
end


92
93
94
# File 'app/models/sitemap.rb', line 92

def self.main_menu
  root.children.select{|t| t.menu_text=='Main Menu'}[0]
end

.new_main_menu_page(name) ⇒ Object



100
101
102
103
104
105
# File 'app/models/sitemap.rb', line 100

def self.new_main_menu_page(name)
  Sitemap.create( :parent=>self.main_menu,
                  :menu_text=>name,
                  :resource=>Page.create(:title=>name),
                  :position=>Sitemap.main_menu.children.count)
end

.reload_rootObject

Callbacks



58
59
60
# File 'app/models/sitemap.rb', line 58

def self.reload_root
  Sitemap.instance_variable_set('@_root', nil)
end

.rootObject



96
97
98
# File 'app/models/sitemap.rb', line 96

def self.root
  @_root ||= Sitemap.find_by_menu_text_and_parent_id('root', nil)
end

.settingsObject



107
108
109
# File 'app/models/sitemap.rb', line 107

def self.settings
  root.children.select{|t| t.menu_text=='Settings'}[0]
end

Instance Method Details

#as_json(*a) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/sitemap.rb', line 20

def as_json(*a)
  {
    :position=>self.position,
    :data=>{
      :title=>self.menu_text,
      :icon=>icon_name,
    },
    :attr=>{
      :id=>"node_#{id}",
      :rel=>resource_type.underscore,
      :data_menu_text => self.menu_text,
      :data_resource_type => self.resource_type,
      :data_resource_id => self.resource_id,
      :data_id => self.id
    },
    :state=>treeview_state,
    :children=> children.sorted
  }.as_json(*a)
end

#on_before_destroyObject



66
67
68
# File 'app/models/sitemap.rb', line 66

def on_before_destroy
  resource.destroy if resource && resource.sitemaps.size==1
end

#reload_rootObject



62
63
64
# File 'app/models/sitemap.rb', line 62

def reload_root
  Sitemap.reload_root
end

#resource_attributes=(attrib = {}) ⇒ Object



40
41
42
43
44
45
# File 'app/models/sitemap.rb', line 40

def resource_attributes=(attrib={})
  raise "Cant build resource without resource_type" if resource_type.empty?
  resource = create_resource
  resource.attributes = attrib
  resource.save
end

#urlObject



47
48
49
50
51
52
53
54
55
# File 'app/models/sitemap.rb', line 47

def url
  if self == Sitemap.main_menu
    '/'
  elsif resource.is_a? ExternalLink
    resource.url
  else
    %(#{(a=parent.url)=='/'?'':a}/#{menu_text.gsub(/ /, '_')})
  end
end