Class: Orange::SitemapResource

Inherits:
ModelResource
  • Object
show all
Defined in:
lib/orange-more/sitemap/resources/sitemap_resource.rb

Instance Method Summary collapse

Instance Method Details



209
210
211
212
213
214
215
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 209

def add_link_for(packet)
  linky = ['add_route']
  linky << (packet['subsite'].blank? ? (packet['site'].blank? ? '0' : packet['site'].id) : packet['subsite'].id)
  linky << (packet['route.resource'].blank? ? '0' : packet['route.resource'])
  linky << (packet['route.resource_id'].blank? ? '0' : packet['route.resource_id'])
  packet.route_to(:sitemap, linky.join('/') )
end

#add_route(packet, opts = {}) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 239

def add_route(packet, opts = {})
  args = packet['route.resource_path'].split('/')
  args.shift
  args = [:orange_site_id, :resource, :resource_id, :slug].inject_hash{|results, key|
    results[key] = args.shift
  }
  me = model_class.new(args)
  me.save
  me.move(:into => home(packet))
  packet.reroute(@my_orange_name, :orange,  me.id, 'edit')
  do_view(packet, :add_route, {})
end

#add_route_for(packet, opts = {}) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 217

def add_route_for(packet, opts = {})
  unless opts.blank?
    parent = opts.delete(:parent) || home(packet, opts)
    unless parent.is_a? model_class
      parent = model_class.get(parent) # Get parent object if parent was an id number
    end
    me = model_class.new(opts)
    me.save
    me.move(:into => parent)
  end
end

#afterNew(packet, obj, params = {}) ⇒ Object



98
99
100
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 98

def afterNew(packet, obj, params = {})
  obj.move(:into => home(packet))
end


185
186
187
188
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 185

def breadcrumb(packet, opts = {})
  opts.with_defaults!(:model => packet['route.route'])
  do_view(packet, :breadcrumb, opts)
end

#create_home_for_site(packet, site_id) ⇒ Object



164
165
166
167
168
169
170
171
172
173
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 164

def create_home_for_site(packet, site_id)
  page = orange[:pages, true].new(packet, :title => 'Homepage', :body => 'this is an orange webpage', :no_reroute => true, :published => true)
  if page
    ret = model_class.create_home_for_site(site_id, :resource => 'pages', :resource_id => page.id)
  else
    ret = model_class.create_home_for_site(site_id)
  end
  orange.fire(:sitemap_created, {:packet => packet, :site_id => site_id})
  ret
end

#find_list(packet, mode, *args) ⇒ Object



279
280
281
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 279

def find_list(packet, mode, *args)
  home(packet, :subsite => true).self_and_descendants
end

#find_route(packet, opts = {}) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 200

def find_route(packet, opts = {})
  site_id = packet['site'].id
  subsite_id = packet['subsite'].blank? ? nil : packet['subsite'].id
  # First try subsite, if necessary
  m = model_class.first(opts.merge(:orange_site_id => subsite_id)) if subsite_id
  m = model_class.first(opts.merge(:orange_site_id => site_id)) unless m
  m
end

#find_route_info(packet, path) ⇒ Object

Path should be an array of path parts



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 58

def find_route_info(packet, path)
  parts = path.split('/')
  pad = parts.shift
  matched = home(packet, :subsite => true)
  extras = ''
  while (!parts.empty?)
    next_part = parts.shift
    matches = matched.children.first(:slug => next_part)
    if(matches) 
      matched = matches
    else
      extras = parts.unshift(next_part).unshift(pad).join('/')
      parts = []
    end
  end
  [extras, matched]
end

#higher(packet, opts = {}) ⇒ Object



133
134
135
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 133

def higher(packet, opts = {})
  move(packet, false, :direction => :higher)
end

#home(packet, opts = {}) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 155

def home(packet, opts = {})
  if(opts[:subsite])
    site_id = opts[:orange_site_id] || packet['subsite'].blank? ? packet['site'].id : packet['subsite'].id
  else
    site_id = opts[:orange_site_id] || packet['site'].id 
  end
  model_class.home_for_site(site_id) || create_home_for_site(packet, site_id)
end

#indent(packet, opts = {}) ⇒ Object



151
152
153
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 151

def indent(packet, opts = {})
  move(packet, false, :direction => :indent)
end

#into(packet, opts = {}) ⇒ Object



137
138
139
140
141
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 137

def into(packet, opts = {})
  opts[:parent_id] ||= packet['route.resource_path'].split('/').last
  opts[:parent] = find_one(packet, :move, (opts[:parent_id])) unless opts[:parent]
  move(packet, false, :direction => :into, :parent => opts[:parent])
end


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 259

def link_text_for(route)
  return route.link_text unless route.resource && route.resource_id
  if match = route.link_text.match(/^\{([-a-z0-9A-Z_]+)\}$/)
    resource = route.resource.to_sym
    return route.link_text unless orange.loaded?(resource)
    return route.link_text if route.resource_id.blank?
    if my_resource = orange[resource].model_class.get(route.resource_id)
      my_resource.__send__(match[1])
    else
      route.link_text
    end
  else
    route.link_text
  end
end

#lower(packet, opts = {}) ⇒ Object



143
144
145
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 143

def lower(packet, opts = {})
  move(packet, false, :direction => :lower)
end

#move(packet, obj, opts = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 102

def move(packet, obj, opts = {})
  no_reroute = opts.delete(:no_reroute)
  if packet.request.post? || !opts.blank?
    dir = opts[:direction]
    dir = {:into => opts[:parent]} if (dir == :into)
    obj ||= find_one(packet, :move, (opts[:id] || packet['route.resource_id']))
    obj.move(dir) if obj
  end
  packet.reroute(@my_orange_name, :orange) unless (packet.request.xhr? || no_reroute)
end

#one_level(packet, opts = {}) ⇒ Object



175
176
177
178
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 175

def one_level(packet, opts = {})
  opts.with_defaults!(:model => home(packet))
  do_view(packet, :one_level, opts)
end

#onNew(packet, params = {}) ⇒ Object

Creates a new model object and saves it (if a post), then reroutes to the main page

Parameters:

  • packet (Orange::Packet)

    the packet being routed



93
94
95
96
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 93

def onNew(packet, params = {})
    params.with_defaults!(:orange_site_id => (packet['subsite'].blank? ? packet['site'].id : packet['subsite'].id))
    model_class.new(params)
end

#outdent(packet, opts = {}) ⇒ Object



147
148
149
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 147

def outdent(packet, opts = {})
  move(packet, false, :direction => :outdent)
end

#route(packet) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 46

def route(packet)
  return packet.reroute(packet['route.reroute']) if packet['route.reroute']
  resource = packet['route.resource']
  raise 'resource not found' unless orange.loaded? resource
  unless (packet['route.resource_action'])
    packet['route.resource_action'] = (packet['route.resource_id'] ? :show : :index)
  end
  
  packet[:content] = (orange[resource].view packet)
end

#route?(packet, path) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 76

def route?(packet, path)
  extras, matched = find_route_info(packet, path)
  return false if(extras.length > 0 && !matched.accept_args)
  return false if matched.resource.blank? && matched.reroute_to.blank?
  packet['route.reroute'] = matched.reroute_to unless matched.reroute_to.blank?
  packet['route.path'] = path
  packet['route.route'] = matched
  packet['route.resource'] = matched.resource.to_sym unless matched.resource.blank?
  packet['route.resource_id'] = matched.resource_id.to_i unless matched.resource_id.blank?
  packet['route.resource_action'] = matched.resource_action.to_sym unless  matched.resource_action.blank?
  # allow "resource_paths" - extra arguments added as path parts
  packet['route.resource_path'] = extras
  return true
end

#route_actions(packet, opts = {}) ⇒ Object



42
43
44
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 42

def route_actions(packet, opts = {})
  do_view(packet, :route_actions, opts)
end

#routes_for(packet, opts = {}) ⇒ Object



190
191
192
193
194
195
196
197
198
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 190

def routes_for(packet, opts = {})
  keys = {}
  keys[:resource] = opts[:resource] || packet['route.resource'] 
  keys[:resource_id] = opts[:resource_id] || packet['route.resource_id'] 
  keys[:orange_site_id] = opts[:orange_site_id] || (packet['subsite'].blank? ? packet['site'].id : packet['subsite'].id)
  keys[:slug] = opts[:slug]
  keys.delete_if{|k,v| v.blank? }
  model_class.all(keys)
end

#show_in_nav(packet, opts = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 113

def show_in_nav(packet, opts = {})
  no_reroute = opts.delete(:no_reroute)
  if packet.request.post? || !opts.blank?
    obj = find_one(packet, :show_in_nav, (opts[:id] || opts[:resource_id] || packet['route.resource_id']))
    obj.show_in_nav = true
    obj.save
  end
  packet.reroute(@my_orange_name, :orange) unless (packet.request.xhr? || no_reroute)
end


295
296
297
298
299
300
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 295

def sitemap_links(packet, opts = {})
  packet.add_js('sitemap.js', :module => '_sitemap_')
  opts.with_defaults!({:list => routes_for(packet) })
  opts.merge!({:add_route_link => add_link_for(packet)})
  do_list_view(packet, :sitemap_links, opts)
end

#slug(str) ⇒ Object



275
276
277
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 275

def slug(str)
  str.downcase.gsub(/[']+/, "").gsub(/[^a-z0-9]+/, "-")
end

#slug_for(model, props) ⇒ Object



252
253
254
255
256
257
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 252

def slug_for(model, props)
  hash = model.attributes
  # return slug(model.title) if hash.has_key?(:title)
  # return slug(model.name) if hash.has_key?(:name)
  return 'page-'+model.id.to_s
end

#stack_initObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 8

def stack_init
  orange[:admin, true].add_link('Content', :resource => @my_orange_name, :text => 'Sitemap')
  orange[:radius, true].define_tag "link" do |tag|
    packet = tag.locals.packet
    slug = tag.expand
    id = tag.attr['id'] if tag.attr['id']
    route = orange[:sitemap].find_route(packet, {:id => id}) if id
    route = orange[:sitemap].find_route(packet, {:slug => slug}) unless route
    route = orange[:sitemap].find_route(packet, {:link_text => slug}) unless route
    
    orange[:sitemap].to_html(packet, route, tag.attr["text"])
  end
  orange.register(:model_resource_deleted) do |packet, opts|
    model_class.all(opts).destroy!
  end
end

#table_row(packet, opts = {}) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 283

def table_row(packet, opts ={})
  opts[:route] = opts[:model] || find_one(packet, :table_row, opts[:id])
  resource = opts[:route].resource
  resource = resource.to_sym unless resource.blank?
  if resource && orange[resource].respond_to?(:sitemap_row)
    opts.delete(:model)
    orange[resource].sitemap_row(packet, opts.merge(:resource_name => resource, :id => opts[:route].resource_id))
  else
    do_view(packet, :table_row, opts)
  end
end

#to_href(packet, route) ⇒ Object



25
26
27
28
29
30
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 25

def to_href(packet, route)
  unless route.is_a? model_class
    route = model_class.get(route)
  end
  full_path = route ? route.full_path : "#not-found"
end

#to_html(packet, route, text = false) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 32

def to_html(packet, route, text = false)
  unless route.is_a? model_class
    route = model_class.get(route)
  end
  full_path = route ? route.full_path : "#not-found"
  link_text = route ? link_text_for(route) : "(Broken link tag)"
  link_text = text if text
  "<a href='#{full_path}'>#{link_text}</a>"
end

#two_level(packet, opts = {}) ⇒ Object



180
181
182
183
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 180

def two_level(packet, opts = {})
  opts.with_defaults!(:model => home(packet))
  do_view(packet, :two_level, opts)
end

#unshow_in_nav(packet, opts = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 123

def unshow_in_nav(packet, opts = {})
  no_reroute = opts.delete(:no_reroute)
  if packet.request.post? || !opts.blank?
    obj = find_one(packet, :unshow_in_nav, (opts[:id] || opts[:resource_id] || packet['route.resource_id']))
    obj.show_in_nav = false
    obj.save
  end
  packet.reroute(@my_orange_name, :orange) unless (packet.request.xhr? || no_reroute)
end

#url_for(packet, opts = {}) ⇒ Object



229
230
231
232
233
234
235
236
237
# File 'lib/orange-more/sitemap/resources/sitemap_resource.rb', line 229

def url_for(packet, opts = {})
  include_subsite = opts.delete(:include_subsite) || false
  m = model_class.first(opts)
  if !packet['subsite'].blank? && include_subsite
    return orange[:subsites].url_for(packet).gsub(/\/$/, '') + (m ? packet.route_to(m) : '#not_found')
  else
    return (m ? packet.route_to(m) : '#not_found')
  end
end