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



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

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



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

def asset_cache
  @asset_cache ||= AssetCache.new
end

#dev_box?(mode, format) ⇒ Boolean

Return true if we should display the dev box

Returns:

  • (Boolean)


343
344
345
# File 'lib/zena/use/zafu_templates.rb', line 343

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)


338
339
340
# File 'lib/zena/use/zafu_templates.rb', line 338

def dev_mode?
  !visitor.dev_skin_id.blank?
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



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

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.



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/zena/use/zafu_templates.rb', line 352

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



347
348
349
# File 'lib/zena/use/zafu_templates.rb', line 347

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

#template_path_from_template_url(template_url = ) ⇒ Object

Return the template path without ‘.erb’ extension in case we need to append ‘_form’ from a template’s url. The expected url is of the form ‘/skin/Klass-mode/partial’



314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/zena/use/zafu_templates.rb', line 314

def template_path_from_template_url(template_url=params[:t_url])
  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('/')
  template_url.insert(-2, dev_mode? ? "dev_#{lang}" : lang)
  path = template_url.join('/')

  "/#{current_site.host}/zafu/#{path}"
end

#template_url(opts = {}) ⇒ Object

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



246
247
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
# File 'lib/zena/use/zafu_templates.rb', line 246

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



327
328
329
330
331
332
333
334
335
# File 'lib/zena/use/zafu_templates.rb', line 327

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