Module: Zena::Use::ZafuTemplates::ControllerMethods

Defined in:
lib/zena/use/zafu_templates.rb

Overview

Common

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



236
237
238
239
240
241
242
243
244
245
# File 'lib/zena/use/zafu_templates.rb', line 236

def self.included(base)
#          base.send(:helper_attr, :asset_cache)
  if base.respond_to?(:helper_method)
    base.send(:helper_method, :dev_mode?, :lang_path, :rebuild_template, :get_template_text, :template_url, :template_url_for_asset, :zafu_helper, :template_path_from_template_url, :save_erb_to_url, :default_template_url, :fullpath_from_template_url)
  end

  base.send(:include, ::Zafu::ControllerMethods)
  # Needs to be inserted after Zafu::ControllerMethods since we overwrite get_template_text and such
  base.send(:include, Common)
end

Instance Method Details

#asset_cacheObject



401
402
403
# File 'lib/zena/use/zafu_templates.rb', line 401

def asset_cache
  @asset_cache ||= AssetCache.new
end

#dev_box?(mode, format) ⇒ Boolean

Return true if we should display the dev box

Returns:

  • (Boolean)


374
375
376
# File 'lib/zena/use/zafu_templates.rb', line 374

def dev_box?(mode, format)
  (format == 'html' && !['+popupLayout', '+user'].include?(mode)) && dev_mode?
end

#dev_mode?Boolean

Return true if the current rendering should include a dev box.

Returns:

  • (Boolean)


369
370
371
# File 'lib/zena/use/zafu_templates.rb', line 369

def dev_mode?
  visitor.dev_mode?
end

#get_best_template(kpaths, format, mode, skin, template = nil) ⇒ Object

Find the best template to render the object. The ‘template’ option is used when static calls the native method to mangle the zafu_url



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/zena/use/zafu_templates.rb', line 306

def get_best_template(kpaths, format, mode, skin, template = nil)
  if template ||= secure(Template) { Template.find(:first,
      :conditions => ["tkpath IN (?) AND format = ? AND mode #{mode ? '=' : 'IS'} ? AND idx_templates.node_id = nodes.id AND idx_templates.skin_id = ?", kpaths, format, mode, skin.id],
      :from       => "nodes, idx_templates",
      :select     => "nodes.*, tkpath",
      :order      => "length(tkpath) DESC"
    )}

    # Path as seen from zafu:
    path_in_skin = template.fullpath.gsub(/^#{@skin.fullpath}\//, '')

    if path_in_skin == template.zip.to_s
      zafu_url = [@skin.title, template.title]
    else
      zafu_url = [@skin.title] + Node.fullpath_map(path_in_skin, :title)
    end
    [zafu_url.map(&:to_filename).join('/'), template]
  else
    nil
  end
end

#get_skin(skin_name = nil) ⇒ Object

Return the skin to use depending on the current node and dev mode of the visitor.



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/zena/use/zafu_templates.rb', line 383

def get_skin(skin_name = nil)
  @skins ||= {}

  if skin_name.blank?
    skin = visitor.get_skin(@node)
  elsif skin = @skins[skin_name]
    return skin
  else
    skin =secure(Skin) { Skin.find_by_title(skin_name) }
  end

  if skin
    @skins[skin.title] = skin
  end

  skin
end

#lang_pathObject



378
379
380
# File 'lib/zena/use/zafu_templates.rb', line 378

def lang_path
  dev_mode? ? "dev_#{lang}" : lang
end

#template_path_from_template_url(suffix = '', template_url = , build = true) ⇒ Object

Return the template path from a template’s url. The expected url is of the form ‘/skin/Klass-mode/partial’



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/zena/use/zafu_templates.rb', line 330

def template_path_from_template_url(suffix='', template_url=params[:t_url], build=true)
  raise "Missing template_url (t_url parameter)" unless template_url
  if template_url =~ /\A\.|[^ #{String::ALLOWED_CHARS_IN_FILEPATH}]/
    raise Zena::AccessViolation.new("'template_url' contains illegal characters : #{template_url.inspect}")
  end

  template_url = template_url.split('/')
  base_p = [current_site.zafu_path] + template_url[0..-2]
  lang_p = [dev_mode? ? "dev_#{lang}" : lang]
  part_p = template_url[-1]
  
  main_fullpath = SITES_ROOT + (base_p + lang_p + ['_main.erb']).join('/')
  if !File.exist?(main_fullpath) && build
    skin = template_url[0]
    template_name = template_url[-2]
    
    if template_name =~ ::Template::MODE_FORMAT_FROM_TITLE
      klass  = $1
      mode   = $4 || ''
      format = $6 || 'html'
      
      template_url(:mode => mode, :format => format, :vclass => VirtualClass[klass], :skin => skin)
    end
  end
  
  (base_p + lang_p + ["#{part_p}#{suffix}.erb"]).join('/')
end

#template_url(opts = {}) ⇒ Object

Return the path of a template for the given skin, mode and format. Compiles the zafu template if needed.



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/zena/use/zafu_templates.rb', line 248

def template_url(opts={})
  # opts[:skin] option removed
  if vclass = opts[:vclass]
    # We know the skin title, skip 'get_skin'
    @skins ||= {}
    
    if opts[:skin].to_s =~ /^\$(.*)/
      # The skin passed through the url is $brick-bar if using fs_skin and this does not work.
      @skin = @skins[opts[:skin]] ||= secure(Skin) { visitor.site.root_node.find("skin where z_fs_skin = '#{$1}' in site") }
    else
      @skin = @skins[opts[:skin]] ||= secure(Skin) { Skin.find_by_title(opts[:skin]) }
    end
    
    # possible classes for the master template :
    kpaths = [vclass.kpath]
  else
    @skin  = get_skin
    vclass = @node.vclass
    
    # possible classes for the master template :
    kpaths = []
    vclass.kpath.split(//).each_index { |i| kpaths << vclass.kpath[0..i] }
  end
  
  mode   = opts[:mode]
  mode   = nil if mode.blank?
  format = opts[:format] || 'html'

  if @skin
    zafu_url, template = get_best_template(kpaths, format, mode, @skin)
    return default_template_url(opts) unless zafu_url
    
    rel_path  = current_site.zafu_path + "/#{zafu_url}/#{lang_path}/_main.erb"
    
    path      = SITES_ROOT + rel_path
    if !File.exists?(path) || params[:rebuild]
      # Use the template's target vclass as starting context
      zafu_node('@node', VirtualClass.find_by_kpath(template.tkpath))

      # template is a new record when template is returned for
      # a static file.
      unless rebuild_template(template.new_record? ? nil : template, opts.merge(:zafu_url => zafu_url, :rel_path => rel_path, :dev_mode => (dev_box?(mode, format))))
        return default_template_url(opts)
      end
    end

    rel_path
  else
    # No template found, use a default

    # $default/Node
    default_template_url(opts)
  end
end

#zafu_helperObject



358
359
360
361
362
363
364
365
366
# File 'lib/zena/use/zafu_templates.rb', line 358

def zafu_helper
  @zafu_helper ||= begin
    # FIXME rails 3.0.pre: zafu_helper = ActionView::Base.for_controller(self)
    helper = ActionView::Base.new([], {}, self)
    helper.send(:_evaluate_assigns_and_ivars)
    helper.helpers.send :include, self.class.master_helper_module
    helper
  end
end