Sha256: 0a0bafcfdd4aba3b127cd22407253571210e14f0438c5040fe8d7dbafe22dcd7

Contents?: true

Size: 1.98 KB

Versions: 6

Compression:

Stored size: 1.98 KB

Contents

module Kuztuscms
  class Resolver < ActionView::Resolver
    require "singleton"
    include Singleton

    def find_templates(name, prefix, partial, details)
      conditions = {
        :path    => normalize_path(name, prefix),
        :locale  => normalize_array(details[:locale]).first,
        :format  => normalize_array(details[:formats]).first,
        :handler => normalize_array(details[:handlers]),
        :partial => partial || false
      }

      if prefix == 'layouts'
        Layout.where(conditions).map do |record|
          initialize_template(record)
        end
      else
        Template.where(conditions).map do |record|
          initialize_template(record)
        end
      end
    end

    # Normalize name and prefix, so the tuple ["index", "users"] becomes
    # "users/index" and the tuple ["template", nil] becomes "template".
    def normalize_path(name, prefix)
      return name if prefix == "kuztuscms/pages"
      return name.gsub(/^#{Kuztuscms.layout_prefix}/, '') if prefix == "layouts"

      prefix.present? ? "#{prefix}/#{name}" : name
    end

    # Normalize arrays by converting all symbols to strings.
    def normalize_array(array)
      array.map(&:to_s)
    end

    # Initialize an ActionView::Template object based on the record found.
    def initialize_template(record)
      source = record.body
      identifier = "Template - #{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

    # Make paths as "users/user" become "users/_user" for partials.
    def virtual_path(path, partial)
      return path unless partial
      if index = path.rindex("/")
        path.insert(index + 1, "_")
      else
        "_#{path}"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
kuztuscms-0.0.10 config/initializers/resolver.rb
kuztuscms-0.0.9 config/initializers/resolver.rb
kuztuscms-0.0.7 config/initializers/resolver.rb
kuztuscms-0.0.6 config/initializers/resolver.rb
kuztuscms-0.0.5 config/initializers/resolver.rb
kuztuscms-0.0.4 config/initializers/resolver.rb