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



394
395
396
# File 'lib/zena/use/zafu_templates.rb', line 394

def asset_cache
  @asset_cache ||= AssetCache.new
end

#dev_box?(mode, format) ⇒ Boolean

Return true if we should display the dev box

Returns:

  • (Boolean)


367
368
369
# File 'lib/zena/use/zafu_templates.rb', line 367

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)


362
363
364
# File 'lib/zena/use/zafu_templates.rb', line 362

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



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/zena/use/zafu_templates.rb', line 291

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.



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/zena/use/zafu_templates.rb', line 376

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



371
372
373
# File 'lib/zena/use/zafu_templates.rb', line 371

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’



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/zena/use/zafu_templates.rb', line 315

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.host, 'zafu'] + 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
      # title changed force  update
      klass  = $1
      mode   = $4 || ''
      format = $6 || 'html'
      # Template rendering node
      node_bak = @node
        # Find first node matching klass
        vclass = VirtualClass[klass]
        @node = Node.sfind("#{klass.underscore} in site", :first)
        if @node.skin.title != skin
          @node.skin = secure(Skin) { Skin.find_by_title(skin) }
        end
        template_url(:mode => mode, :format => format)
      @node = node_bak
    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
# File 'lib/zena/use/zafu_templates.rb', line 248

def template_url(opts={})
  # opts[:skin] option removed
  @skin     = get_skin
  mode      = opts[:mode]
  format    = opts[:format] || 'html'
  klass     = @node.vclass

  # possible classes for the master template :
  kpaths = []
  klass.kpath.split(//).each_index { |i| kpaths << klass.kpath[0..i] }

  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]
      if @node && klass = VirtualClass.find_by_kpath(template.tkpath)
        zafu_node('@node', klass)
      else
        nil
      end

      # 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



351
352
353
354
355
356
357
358
359
# File 'lib/zena/use/zafu_templates.rb', line 351

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