Class: Ecm::CmsCore::Template::Resolver

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

Instance Method Summary collapse

Instance Method Details

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/ecm/cms_core/template.rb', line 83

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  
  

  # ActiveRecord::Base.logger.debug("prefix: #{prefix}")
  ActiveRecord::Base.logger.debug("pathname: #{pathname}")
  ActiveRecord::Base.logger.debug("basename: #{name}")          

  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  = Ecm::CmsCore::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.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/models/ecm/cms_core/template.rb', line 132

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.



127
128
129
# File 'app/models/ecm/cms_core/template.rb', line 127

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”.



122
123
124
# File 'app/models/ecm/cms_core/template.rb', line 122

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.



153
154
155
156
157
158
159
160
# File 'app/models/ecm/cms_core/template.rb', line 153

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