Skip to content

Commit

Permalink
A rough first pass at template importing. Only works some of the time…
Browse files Browse the repository at this point in the history
…, has no error handling, but will partially import as much as possible. [#110]
  • Loading branch information
bamnet committed Jun 14, 2011
1 parent e74c72a commit 67023f1
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 8 deletions.
35 changes: 35 additions & 0 deletions app/controllers/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,39 @@ def preview
end
end
end

# PUT /templates/import
# Import a template from an XML description and convert it
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
format.html { render :action => "new" }
format.xml { render :xml => @template.errors, :status => :unprocessable_entity }
end
end
end
end
12 changes: 8 additions & 4 deletions app/models/position.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ class Position < ActiveRecord::Base
belongs_to :template

#Validations
validates_uniqueness_of :field_id, :scope => :template_id

validates :field, :presence => true, :associated => true
validates :right, :numericality => {:greater_than_or_equal_to => -1, :less_than_or_equal_to => 1}
validates :left, :numericality => {:greater_than_or_equal_to => -1, :less_than_or_equal_to => 1}
validates :top, :numericality => {:greater_than_or_equal_to => -1, :less_than_or_equal_to => 1}
validates :bottom, :numericality => {:greater_than_or_equal_to => -1, :less_than_or_equal_to => 1}

# Compute the width of the position block.
# A Concerto-1 style attribute, figuring out
# the total width of the element.
Expand All @@ -17,15 +21,15 @@ def width
# A Concerto-1 style accessor, mainly used
# for importing templates.
def width=(size)
self.right = left + size
self.right = left + size.to_f
end

# Enabling the height to be set for a position.
# The bottom is adjusted relative to the top.
# A Concerto-1 style accessor, mainly used
# for importing templates.
def height=(size)
self.bottom = top + size
self.bottom = top + size.to_f
end

# Compute the height of the position block.
Expand Down
4 changes: 2 additions & 2 deletions app/models/template.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Template < ActiveRecord::Base
has_many :screens
has_many :media, :as => :attachable
has_many :positions
has_many :media, :as => :attachable, :dependent => :destroy
has_many :positions, :dependent => :destroy

accepts_nested_attributes_for :media

Expand Down
13 changes: 13 additions & 0 deletions app/views/templates/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,16 @@
<%= render 'form' %>
<%= link_to 'Back', templates_path %>

<h1>Import template</h1>
<%= form_tag(import_templates_path, {:multipart => true}) do %>
<div class="field">
<%= label_tag :xml, 'XML' %><br />
<%= file_field_tag :xml %>
</div>
<div class="field">
<%= label_tag :image %><br />
<%= file_field_tag :image %>
</div>
<%= submit_tag "Import Template" %>
<% end %>
4 changes: 2 additions & 2 deletions app/views/templates/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<b>Is hidden:</b>
<%= @template.is_hidden %>
</p>

<%= link_to "Media", @template.media.original.first %>
<%= link_to "Preview", preview_template_path(@template) %> |
<%= link_to "Media", @template.media.original.first %> |

<%= link_to 'Edit', edit_template_path(@template) %> |
<%= link_to 'Back', templates_path %>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
member do
get :preview
end
collection do
post :import
end
end

resources :screens do
Expand Down

0 comments on commit 67023f1

Please sign in to comment.