Skip to content

Commit

Permalink
Refactor screen template rendering to the frontend namespace.
Browse files Browse the repository at this point in the history
* Move the template rendering code for the frontend, update the caching slightly.
* Pass the template path as part of the screen setup information instead of constructing it on the fly.
  • Loading branch information
bamnet committed Mar 18, 2012
1 parent 1b5b50c commit 6e21c06
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 35 deletions.
3 changes: 3 additions & 0 deletions app/controllers/frontend/screens_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def setup
rescue ActiveRecord::ActiveRecordError
render :json => {}, :status => 404
else
# Inject the path into an fake attribute so it gets sent with the setup info.
@screen.template.path = frontend_screen_template_path(@screen, @screen.template)
respond_to do |format|
format.json {
render :json => @screen.to_json(
Expand All @@ -28,6 +30,7 @@ def setup
},
},
:only => [:id],
:methods => [:path]
}
}
)
Expand Down
34 changes: 34 additions & 0 deletions app/controllers/frontend/templates_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Frontend::TemplatesController < ApplicationController

# GET /frontend/1/template/1
# Render the template for display on a screen.
def show
template = Template.find(params[:id])
if stale?(:last_modified => template.last_modified.utc, :etag => template, :public => true)
require'image_utility'
media = template.media.original.first
image = Magick::Image.from_blob(media.file_contents).first

# Resize the image to a height and width if they are both being set.
# Round these numbers up to ensure the image will at least fill
# the requested space.
height = params[:height].nil? ? nil : params[:height].to_f.ceil
width = params[:width].nil? ? nil : params[:width].to_f.ceil

image = ImageUtility.resize(image, width, height, false)
case request.format
when Mime::Type.lookup_by_extension(:jpg)
image.format = "JPG"
when Mime::PNG
image.format = "PNG"
else
render :status => 406, :text => "Unacceptable image type." and return
end

send_data image.to_blob,
:filename => "#{template.name.underscore}.#{image.format.downcase}",
:type => image.mime_type, :disposition => 'inline'
end
end

end
33 changes: 0 additions & 33 deletions app/controllers/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,39 +93,6 @@ def destroy
format.xml { head :ok }
end
end

# GET /template/1/display
# Render the template for display on a screen.
def display
require'image_utility'
@template = Template.find(params[:id])
@media = @template.media.original.first
image = Magick::Image.from_blob(@media.file_contents).first

# Resize the image to a height and width if they are both being set.
# Round these numbers up to ensure the image will at least fill
# the requested space.
width = nil
height = nil
height = params[:height].to_f.ceil unless params[:height].nil?
width = params[:width].to_f.ceil unless params[:width].nil?

image = ImageUtility.resize(image, width, height, false)
case request.format
when Mime::Type.lookup_by_extension(:jpg)
image.format = "JPG"
when Mime::PNG
image.format = "PNG"
end

# Set some reasonable cache headers
response.headers["Last-Modified"] = CGI.rfc1123_date(@template.last_modified)
expires_in 36.hours, :public => true

send_data image.to_blob,
:filename => "#{@template.name.underscore}.#{image.format.downcase}",
:type => image.mime_type, :disposition => 'inline'
end

# GET /template/1/preview
# Generate a preview of the template based on the request format.
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/frontend/templates_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Frontend::TemplatesHelper
end
3 changes: 3 additions & 0 deletions app/models/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class Template < ActiveRecord::Base

#Validations
validates :name, :presence => true

#Placeholder attributes
attr_accessor :path

# Given a string from an XML descriptor, build the template
# to try and match the description. Each position will be
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
get :contents, :path => 'content'
end
end
resources :templates, :only => [:show]
end
end
# End really dangerous routes.
Expand All @@ -31,7 +32,6 @@
resources :positions
member do
get :preview
get :display
end
collection do
post :import
Expand Down
10 changes: 9 additions & 1 deletion public/frontend_js/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ concerto.frontend.Template = function(screen, opt_div) {
/**
* The template ID number.
* @type {?number}
* @private
*/
this.id = null;

/**
* The URL to the template.
* @type {?string}
*/
this.path = null;

/**
* Positions being shown on this template.
* @type {?Array.<concerto.frontend.Position>}
Expand Down Expand Up @@ -81,6 +88,7 @@ concerto.frontend.Template.prototype.createDiv_ = function() {
*/
concerto.frontend.Template.prototype.load = function(data) {
this.id = data.id;
this.path_ = data.path;
goog.dom.setProperties(this.div_, {'id': 'template_' + this.id});

this.render_();
Expand All @@ -104,7 +112,7 @@ concerto.frontend.Template.prototype.load = function(data) {
concerto.frontend.Template.prototype.render_ = function() {
var size = goog.style.getSize(this.div_);

var background_url = new goog.Uri('/templates/' + this.id + '/display');
var background_url = new goog.Uri(this.path_);
background_url.setParameterValue('height', size.height);
background_url.setParameterValue('width', size.width);

Expand Down
1 change: 1 addition & 0 deletions test/functional/frontend/screens_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Frontend::ScreensControllerTest < ActionController::TestCase
assert_equal data['name'], screens(:one).name
assert_equal data['template']['positions'].length,
screens(:one).template.positions.length
assert data['template']['path'].length > 0
end

test "cannot setup missing screen" do
Expand Down
7 changes: 7 additions & 0 deletions test/functional/frontend/templates_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class Frontend::TemplatesControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
4 changes: 4 additions & 0 deletions test/unit/helpers/frontend/templates_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class Frontend::TemplatesHelperTest < ActionView::TestCase
end

0 comments on commit 6e21c06

Please sign in to comment.