kristianmandrup / tecla-cms

Tecla CMS
Other
0 stars 0 forks source link

Add Templating #22

Open kristianmandrup opened 8 years ago

kristianmandrup commented 8 years ago

Add templating for API: blocks, pages and menu.

Use the following code as a base "template" to work from

def block opts = {}
  Block.find(opts);
end

# http://apidock.com/rails/ActionController/Base/render
Cms::RenderTemplate.new.render {
  template: 'blog',
  layout: 'blog_post',
  :locals => block(blog: 'latest').to_json
}

# See spec
# https://github.com/yappbox/render_anywhere/blob/master/spec/lib/render_anywhere_spec.rb

class Cms::RenderTemplate
  require 'render_anywhere'
  include RenderAnywhere

  prepend_view_path Cms::Template::Resolver.instance

  attr_reader :template, :layout

  # Include an additional helper
  # If being used in a rake task, you may need to require the file(s)
  # Ex: require Rails.root.join('app', 'helpers', 'blog_pages_helper')
  def include_helper(helper_name)
    set_render_anywhere_helpers(helper_name)
  end

  # Apply an instance variable to the controller
  # If you need to use instance variables instead of locals, just call this method as many times as you need.
  def set_instance_variable(var, value)
    set_instance_variable(var, value)
  end

 class RenderingController < RenderAnywhere::RenderingController
    # include custom modules here, define accessors, etc. For example:
    # attr_accessor :current_user
    # helper_method :current_user
  end

  # If you define custom RenderingController, don't forget to override this method
  def rendering_controller
    @rendering_controller ||= self.class.const_get("RenderingController").new
  end
end

class TemplateFinder
  require 'singleton'
  include Singleton

  def find_template(name)
    find(name, 'template')
  end

  def find_layout(name)
    find(name, 'layout')
  end

  # or via type: 'partial'
  def find_partial(name)
    find_template("_#{name}")
  end

  def find(name, type)
    Template.find(name: name, type: type)
  end
end

class Cms::Template
  ...
  field format: String

  def identifier
    "#{record.type} - #{record.id} - #{record.path}"
  end
end

# Singleton Pattern
# http://eftimov.net/singleton-pattern/

class Cms::Template::Resolver < ActionView::Resolver
  require 'singleton'
  include Singleton

  def initialize
    @finder = TemplateFinder.instance
  end

  def type prefix
    prefix.empty? ? 'template' : 'layout'
  end

  def find_templates name, prefix, partial, details
    create_template record(name, type(prefix))
  end

  def record name, type
    finder.find name, type
  end

  def create_template record
    Cms::Template::Creator.new(record).create
  end
end

class Cms::Template::Creator
  attr_reader :record

  extend Forwardable
  def_delegators :record, :path, :partial, :format, :name, :identifier, :body, :handler, :updated_at

  def initialize record
    @record = record
  end

  # Initialize an ActionView::Template object based on the record found.
  def create
    ActionView::Template.new body, identifier, template_handler, options
  end

  def template_handler
    ActionView::Template.registered_template_handler handler
  end

  def options
    {
      :format => Mime[format],
      :updated_at => updated_at,
      :virtual_path => virtual_path(path, partial)
    }
  end

  # Make paths as "users/user" become "users/_user" for partials.
  def virtual_path
    return path unless partial
    index
  end

  def index
    path.rindex('/') ? path.insert(index + 1, '_') : "_#{path}"
  end
end