Permalink
Browse files

Replace templates rake task with a controller/view

  • Loading branch information...
1 parent 8c033e4 commit a2c13f5dda03e4ce9a664013aa89d2ee0a7822c6 @tye committed Nov 30, 2012

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,12 @@
+class Rubyception::TemplatesController < ApplicationController
+ layout false
+
+ helper :all
+
+ def index
+ @templates = {}
+ respond_to do |f|
+ f.js
+ end
+ end
+end
@@ -1,22 +0,0 @@
-require 'jenny'
-class Rubyception::TemplatingController < AbstractController::Base
- include AbstractController::Logger
- include AbstractController::Rendering
- include AbstractController::Layouts
- include AbstractController::Helpers
- include AbstractController::Translation
- include AbstractController::AssetPaths
- include Rails.application.routes.url_helpers
-
- self.view_paths = 'app/views'
- self.assets_dir = 'app/public'
-
- helper Rubyception::ApplicationHelper,
- ::JennyHelper
-
- public
- def r partial
- render :partial => "rubyception/#{partial}"
- end
-
-end
@@ -5,5 +5,27 @@ def display_rails_backtrace_lines_link
class: 'show_rails',
onclick: 'return false'
end
+ def parse_tree tree, parents=[], result={}
+ Rails.logger.info "Start #{tree.inspect}"
+ if tree.is_a?(String)
+ Rails.logger.info "Val is a string"
+ item = result
+ parents.each do |parent|
+ item[parent] ||= {}
+ item = item[parent]
+ end
+ Rails.logger.info "Render #{parents.join('/')}/_#{tree}.haml"
+ item[tree] = render(partial: "rubyception/#{parents.join('/')}/#{tree}")
+ elsif tree.is_a?(Hash)
+ tree.each do |key, val|
+ parse_tree val, parents + [key], result
+ end
+ elsif tree.is_a?(Array)
+ tree.each do |val|
+ parse_tree val, parents, result
+ end
+ end
+ result
+ end
end
end
@@ -3,6 +3,7 @@
%head
%title Rubyception
= stylesheet_link_tag 'rubyception/application', :media => 'all'
+ = javascript_include_tag '/rubyception/templates'
= javascript_include_tag 'rubyception/application'
= csrf_meta_tags
%body
@@ -0,0 +1,4 @@
+<%=
+ parse_tree Rubyception::TEMPLATES, [], @templates
+ "window.Template = #{@templates.to_json};".html_safe
+%>
@@ -0,0 +1,5 @@
+module Rubyception
+ filename = Rubyception::Engine.root.join 'config', 'templates.yml'
+ yaml = File.read filename
+ TEMPLATES = YAML.load yaml
+end
View
@@ -1,3 +1,4 @@
Rubyception::Engine.routes.draw do
+ get '/templates' => 'templates#index'
root to: 'application#index'
end
@@ -8,8 +8,8 @@ shared:
- filter_controller
- filter_action
lines:
- index:
- action_controller:
+ - index
+ - action_controller:
- write_fragment
- read_fragment
- expire_fragment
@@ -22,18 +22,18 @@ lines:
- send_data
- redirect_to
- halted_callback
- action_view:
+ - action_view:
- render_template
- render_partial
- active_record:
+ - active_record:
- sql
- identity
- action_mailer:
+ - action_mailer:
- receive
- deliver
- active_resource:
+ - active_resource:
- request
- active_support:
+ - active_support:
- cache_read
- cache_generate
- cache_fetch_hit
@@ -1,48 +0,0 @@
-# This compiles haml templates into a json object and writes it to
-# the file app/javascripts/template.js. Its so we can render html
-# via javascript.
-
-class Rubyception::Templating
- def self.r partial
- t = Rubyception::TemplatingController.new
- t.r partial
- end
-
- def self.append_to_tree template, current
- keys = current.split('/').collect{|k|"['#{k}']"}.join
- partials = {}
- template.each_index do |i|
- partials[template[i]] = self.r("#{current}/#{template[i]}")
- end
- eval "@@templates#{keys} = partials"
- end
-
- def self.process_tree template, key=nil, current=''
- path = "#{current}/#{key}"
- path = key.to_s if current == ''
- kind = template.class.to_s
- case kind
- when 'Hash'
- template.each do |k,v|
- if v.nil?
- self.append_to_tree [k], path
- else
- self.process_tree v, k, path
- end
- end
- when 'Array'
- self.append_to_tree template, path
- end
- end
-
- def self.compile
- yml = File.join(File.dirname(__FILE__),'..','..','config','templating.yml')
- file = File.open yml
- templates = YAML::load file
- @@templates = templates
- self.process_tree templates
- js = "Template = #{@@templates.to_json}"
- path = File.join(File.dirname(__FILE__),'..','..','app','assets','javascripts','rubyception','template.js')
- File.open(path,'w'){|f|f.write(js)}
- end
-end

0 comments on commit a2c13f5

Please sign in to comment.