Skip to content

Commit

Permalink
Polishing up template importing a bit. Now handled in the model, with…
Browse files Browse the repository at this point in the history
… a simple test. Could use some more work, but I'm marking this complete for now. [#110 state:resolved]
  • Loading branch information
bamnet committed Jun 14, 2011
1 parent 3cd77e0 commit 9a1d76f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 28 deletions.
47 changes: 22 additions & 25 deletions app/controllers/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,37 +138,34 @@ def preview
end

# PUT /templates/import
# Import a template from an XML description and convert it
# Import a template from an XML description and convert it to an actual
# template model.
#
# TODO - This should be cleaned up, we should throw smarter errors too.
def import
xml_file = params[:xml]
image_file = params[:image]

data = Hash::from_xml(xml_file.read)

@template = Template.new(:name => data['template']['name'],
:author => data['template']['author'])
@template.media.build({:key=>"original", :file => image_file})

data['template']['field'].each do |field|
position = @template.positions.build
field.each_pair do |key, value|
position.send("#{key}=".to_sym, value) if position.respond_to?("#{key}=".to_sym)
end
if field.has_key?('name')
position.field = Field.where(:name => field['name']).first
end
if !position.valid?
position.destroy
end
end
respond_to do |format|
if @template.save
format.html { redirect_to(@template, :notice => 'Template was successfully created.') }
format.xml { render :xml => @template, :status => :created, :location => @template }
else
@template = Template.new
if xml_file.nil? || image_file.nil?
respond_to do |format|
format.html { render :action => "new" }
format.xml { render :xml => @template.errors, :status => :unprocessable_entity }
end
else
xml_data = xml_file.read
if !xml_data.blank? && @template.import_xml(xml_data)
@template.media.build({:key=>"original", :file => image_file})
end

respond_to do |format|
if @template.save
format.html { redirect_to(@template, :notice => 'Template was successfully created.') }
format.xml { render :xml => @template, :status => :created, :location => @template }
else
format.html { render :action => "new" }
format.xml { render :xml => @template.errors, :status => :unprocessable_entity }
end
end
end
end
end
35 changes: 35 additions & 0 deletions app/models/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,39 @@ class Template < ActiveRecord::Base

#Validations
validates :name, :presence => true

# Given a string from an XML descriptor, build the template
# to try and match the description. Each position will be
# constructed from the descriptor. If a position can't be
# validated, usually because there was no matching field,
# that position will be rejected but the other sucessful
# positions will be saved.
#
# This means that many templates may only import a few of
# their fields(v1 term) / positions(v2 term). We'll either
# need to strengthn up the matching approach or clean the
# templates we make publicly available.
def import_xml(xml)
data = Hash::from_xml(xml)

self.name = data['template']['name']
self.author = data['template']['author']

data['template']['field'] = [data['template']['field']] unless data['template']['field'].kind_of?(Array)

This comment has been minimized.

Copy link
@bamnet

bamnet Jun 14, 2011

Author Member

If you're curious (and for my own memory later) this line is important. If the XML just contains one it won't get turned into an array, just another hash. This fixes that problem and ensures we have an array.

data['template']['field'].each do |field|
position = self.positions.build
field.each_pair do |key, value|
position.send("#{key}=".to_sym, value) if position.respond_to?("#{key}=".to_sym)
end
if field.has_key?('name')
position.field = Field.where(:name => field['name']).first
end
if !position.valid?
# This position might not actually be deleted,
# instead it will be marked for deletion (aka not creating it)
position.destroy
end
end
return self.valid?
end
end
16 changes: 16 additions & 0 deletions test/fixtures/files/simple_template.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<template>
<name>Sample Template</name>
<width>1920</width>
<height>1080</height>
<author>Testing Author</author>
<field>
<name>Graphics</name>
<type>Graphics</type>
<style>border:solid 2px #ccc;</style>
<left>0.025</left>
<top>0.026</top>
<width>0.567</width>
<height>0.77</height>
</field>
</template>
8 changes: 5 additions & 3 deletions test/unit/template_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require 'test_helper'

class TemplateTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
# Test the ability to import a simple xml descriptor
test "importing a simple template" do
t = Template.new
file = fixture_file_upload("/files/simple_template.xml", 'text/xml')
assert t.import_xml(file.read)
end
end

0 comments on commit 9a1d76f

Please sign in to comment.