class Panoramic::Resolver

Public Class Methods

using(model, options={}) click to toggle source

Instantiate Resolver by passing a model (decoupled from ORMs)

# File lib/panoramic/resolver.rb, line 24
def self.using(model, options={})
  @@model = model
  @@resolver_options = options
  self.instance
end

Public Instance Methods

find_templates(name, prefix, partial, details, key=nil, locals=[]) click to toggle source

this method is mandatory to implement a Resolver

# File lib/panoramic/resolver.rb, line 7
def find_templates(name, prefix, partial, details, key=nil, locals=[])
  return [] if @@resolver_options[:only] && !@@resolver_options[:only].include?(prefix)

  conditions = {
    :path    => build_path(name, prefix),
    :locale  => [normalize_array(details[:locale]).first, nil],
    :format  => normalize_array(details[:formats]),
    :handler => normalize_array(details[:handlers]),
    :partial => partial || false
  }.merge(details[:additional_criteria].presence || {})

  @@model.find_model_templates(conditions).map do |record|
    initialize_template(record)
  end
end

Private Instance Methods

build_path(name, prefix) click to toggle source

Build path with eventual prefix

# File lib/panoramic/resolver.rb, line 48
def build_path(name, prefix)
  prefix.present? ? "#{prefix}/#{name}" : name
end
initialize_template(record) click to toggle source

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

# File lib/panoramic/resolver.rb, line 33
def initialize_template(record)
  source = record.body
  identifier = "#{record.class} - #{record.id} - #{record.path.inspect}"
  handler = ActionView::Template.registered_template_handler(record.handler)

  details = {
    :format => Mime[record.format],
    :updated_at => record.updated_at,
    :virtual_path => virtual_path(record.path, record.partial)
  }

  ActionView::Template.new(source, identifier, handler, details)
end
normalize_array(array) click to toggle source

Normalize array by converting all symbols to strings.

# File lib/panoramic/resolver.rb, line 53
def normalize_array(array)
  array.map(&:to_s)
end
virtual_path(path, partial) click to toggle source

returns a path depending if its a partial or template

# File lib/panoramic/resolver.rb, line 58
def virtual_path(path, partial)
  return path unless partial
  if index = path.rindex("/")
    path.insert(index + 1, "_")
  else
    "_#{path}"
  end
end