From a6eea61f2a7cfbf67175583dd7dcb49a4c09da75 Mon Sep 17 00:00:00 2001 From: Aaron Peckham Date: Mon, 20 May 2013 12:19:35 -0700 Subject: allow widget views to be written in any Tilt-supported template language --- lib/dashing.rb | 24 +++++++++++++++--------- test/app_test.rb | 23 +++++++++++++++-------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/lib/dashing.rb b/lib/dashing.rb index 391095d..04921fe 100644 --- a/lib/dashing.rb +++ b/lib/dashing.rb @@ -50,21 +50,20 @@ end get '/:dashboard' do protected! - view_engine = Tilt.mappings.keys.find do |ext| - File.exist? File.join(settings.views, "#{params[:dashboard]}.#{ext}") + tilt_html_engines.each do |suffix, _| + file = File.join(settings.views, "#{params[:dashboard]}.#{suffix}") + return render(suffix.to_sym, params[:dashboard].to_sym) if File.exist? file end - if view_engine - render view_engine.to_sym, params[:dashboard].to_sym - else - halt 404 - end + halt 404 end get '/views/:widget?.html' do protected! - widget = params[:widget] - send_file File.join(settings.root, 'widgets', widget, "#{widget}.html") + tilt_html_engines.each do |suffix, engines| + file = File.join(settings.root, "widgets", params[:widget], "#{params[:widget]}.#{suffix}") + return engines.first.new(file).render if File.exist? file + end end post '/widgets/:id' do @@ -116,6 +115,13 @@ def first_dashboard files.first end +def tilt_html_engines + Tilt.mappings.select do |_, engines| + default_mime_type = engines.first.default_mime_type + default_mime_type.nil? || default_mime_type == 'text/html' + end +end + Dir[File.join(settings.root, 'lib', '**', '*.rb')].each {|file| require file } {}.to_json # Forces your json codec to initialize (in the event that it is lazily loaded). Does this before job threads start. diff --git a/test/app_test.rb b/test/app_test.rb index 83bdb74..97aad3a 100644 --- a/test/app_test.rb +++ b/test/app_test.rb @@ -72,26 +72,33 @@ class AppTest < Dashing::Test get '/sampletv' assert_equal 200, last_response.status assert_include last_response.body, 'class="gridster"' + assert_include last_response.body, "DOCTYPE" end end begin require 'haml' - def test_get_haml + + def test_get_haml_dashboard with_generated_project do |dir| - File.write(File.join(dir, "dashboards/hamltest.haml"), <<-HAML) -.gridster - %ul - %li{data: {col: 1, row: 1, sizex: 1, sizey: 1}} - %div{data: {view: "Clock"}} - %i.icon-time.icon-background -HAML + File.write(File.join(dir, 'dashboards/hamltest.haml'), '.gridster') get '/hamltest' assert_equal 200, last_response.status assert_include last_response.body, "class='gridster'" end end + + def test_get_haml_widget + with_generated_project do |dir| + File.write(File.join(dir, 'widgets/clock/clock.haml'), '%h1 haml') + File.unlink(File.join(dir, 'widgets/clock/clock.html')) + get '/views/clock.html' + assert_equal 200, last_response.status + assert_include last_response.body, '

haml

' + end + end rescue LoadError + puts "[skipping haml tests because haml isn't installed]" end def test_get_nonexistent_dashboard -- cgit v1.2.3