class LiquidMarkdown::Resolver

Public Class Methods

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

Instantiate Resolver by passing a model (decoupled from ORMs)

# File lib/liquid_markdown/resolver.rb, line 38
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/liquid_markdown/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
  }

  @records = []
  @@model.find_model_templates(conditions).map do |record|
    @records << record
    if record.format == 'html'
      rec = OpenStruct.new(record.attributes)
      rec.format = 'text'
      @records << rec
    else
      rec = OpenStruct.new(record.attributes)
      rec.format = 'html'
      @records << rec
    end
  end

  @records.map do |record|
    initialize_template(record, record.format)
  end
end

Private Instance Methods

build_path(name, prefix) click to toggle source

Build path with eventual prefix

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

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

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

  details = {
      :format => Mime[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/liquid_markdown/resolver.rb, line 67
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/liquid_markdown/resolver.rb, line 72
def virtual_path(path, partial)
  return path unless partial
  if index = path.rindex("/")
    path.insert(index + 1, "_")
  else
    "_#{path}"
  end
end