Class: Template::Resolver

Inherits:
ActionView::Resolver
  • Object
show all
Includes:
Singleton
Defined in:
lib/generators/ecm/cms_core/models/templates/template.rb

Instance Method Summary collapse

Instance Method Details

#find_templates(name, prefix, partial, details) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/generators/ecm/cms_core/models/templates/template.rb', line 53

def find_templates(name, prefix, partial, details)
  if prefix.length > 0
    pathname = "#{normalize_array(details[:locale]).first}/#{prefix}/"
  else
    pathname = "#{normalize_array(details[:locale]).first}/"
  end  
  conditions = {
    # :pathname    => normalize_path(name, prefix),
    :pathname    => pathname,
    :basename    => name,
    :locale      => normalize_array(details[:locale]).first,
    :format      => normalize_array(details[:formats]).first,
    :handler     => normalize_array(details[:handlers]),
    :partial     => partial || false
  }

  format = conditions.delete(:format)
  query  = ::Template.where(conditions)

  # 2) Check for templates with the given format or format is nil
  query = query.where(["format = ? OR format = ''", format])

  # 3) Ensure templates with format come first
  query = query.order("format DESC")

  # 4) Now trigger the query passing on conditions to initialization
  query.map do |record|
    initialize_template(record, details)
  end
end

#initialize_template(record, details) ⇒ Object

Initialize an ActionView::Template object based on the record found.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/generators/ecm/cms_core/models/templates/template.rb', line 96

def initialize_template(record, details)
  source     = record.body
  identifier = "Template - #{record.id} - #{record.pathname.inspect}"
  handler    = ActionView::Template.registered_template_handler(record.handler)

  # 5) Check for the record.format, if none is given, try the template
  # handler format and fallback to the one given on conditions
  format   = record.format && Mime[record.format]
  format ||= handler.default_format if handler.respond_to?(:default_format)
  format ||= details[:formats]

  details = {
    :format => format,
    :updated_at => record.updated_at,
    :virtual_path => virtual_path(record.pathname, record.partial)
  }

  ActionView::Template.new(source, identifier, handler, details)
end

#normalize_array(array) ⇒ Object

Normalize arrays by converting all symbols to strings.



91
92
93
# File 'lib/generators/ecm/cms_core/models/templates/template.rb', line 91

def normalize_array(array)
  array.map(&:to_s)
end

#normalize_path(name, prefix) ⇒ Object

Normalize name and prefix, so the tuple [“index”, “users”] becomes “users/index” and the tuple [“template”, nil] becomes “template”.



86
87
88
# File 'lib/generators/ecm/cms_core/models/templates/template.rb', line 86

def normalize_path(name, prefix)
  prefix.present? ? "#{prefix}/#{name}" : name
end

#virtual_path(path, partial) ⇒ Object

Make paths as “users/user” become “users/_user” for partials.



117
118
119
120
121
122
123
124
# File 'lib/generators/ecm/cms_core/models/templates/template.rb', line 117

def virtual_path(path, partial)
  return path unless partial
  if index = path.rindex("/")
    path.insert(index + 1, "_")
  else
    "_#{path}"
  end
end