Clean code

Moved the unzip_design method as protected
This commit is contained in:
chris2tof 2011-09-01 19:06:49 +08:00
parent a570be1811
commit f78400c274
1 changed files with 52 additions and 49 deletions

View File

@ -22,55 +22,6 @@ class Admin::DesignsController < ApplicationController
temp_file.close temp_file.close
end end
end end
def unzip_design(file, zip_name)
Zip::ZipFile.open(file) { |zip_file|
design = Design.new.from_json(zip_file.read("#{zip_name}/info.json"))
Dir.mktmpdir('f_path') { |dir|
design.build_layout
themes_entries = []
javascripts_entries = []
images_entries = []
#dir/zip_name
#dir/zip_name
zip_file.entries.each do |entry|
case (path = entry.to_s)
when /\A(#{zip_name})\/[^\/]*(\.css)\z/ #for structure css
filename = File.basename(entry.to_s)
temp_file4structure = File.new(dir + '/' + filename, 'w+')
temp_file4structure.write (zip_file.read entry ).force_encoding('UTF-8')
design.structure_css = temp_file4structure
when /\A(#{zip_name})\/[^\/]*(\.html)\z/ #for layout html
filename = File.basename(entry.to_s)
temp_file4layout = File.new(dir + '/' + filename, 'w+')
temp_file4layout.write (zip_file.read entry ).force_encoding('UTF-8')
design.layout.file = temp_file4layout
design.layout.to_save=true
when /\A(#{zip_name})\/(themes)\/.*(\.css)\z/
#for themes css
themes_entries << entry
when /\A(#{zip_name})\/(javascripts)\/.*(\.js)\z/
#for js
javascripts_entries << entry
when /\A(#{zip_name})\/(images)\/.*((\.jpg)|(\.png)|(\.gif))\z/ #for img
images_entries << entry
end
end
['themes', 'javascripts', 'images'].each do |type|
eval("#{type}_entries").each do |entry|
filename = File.basename entry.to_s
temp_file = File.new(dir + '/' + filename, 'w+')
temp_file.write (zip_file.read entry).force_encoding('UTF-8')
build_and_store = eval("design.#{type}").build
build_and_store.file = temp_file
build_and_store.file_filename = filename
build_and_store.to_save = true
end
end
}
design.save
}
end
def index def index
@designs = Design.all.entries @designs = Design.all.entries
@ -132,5 +83,57 @@ class Admin::DesignsController < ApplicationController
render :action => 'new' render :action => 'new'
end end
end end
protected
def unzip_design(file, zip_name)
Zip::ZipFile.open(file) { |zip_file|
design = Design.new.from_json(zip_file.read("#{zip_name}/info.json"))
Dir.mktmpdir('f_path') { |dir|
design.build_layout
themes_entries = []
javascripts_entries = []
images_entries = []
#dir/zip_name
#dir/zip_name
zip_file.entries.each do |entry|
case (path = entry.to_s)
when /\A(#{zip_name})\/[^\/]*(\.css)\z/ #for structure css
filename = File.basename(entry.to_s)
temp_file4structure = File.new(dir + '/' + filename, 'w+')
temp_file4structure.write (zip_file.read entry ).force_encoding('UTF-8')
design.structure_css = temp_file4structure
when /\A(#{zip_name})\/[^\/]*(\.html)\z/ #for layout html
filename = File.basename(entry.to_s)
temp_file4layout = File.new(dir + '/' + filename, 'w+')
temp_file4layout.write (zip_file.read entry ).force_encoding('UTF-8')
design.layout.file = temp_file4layout
design.layout.to_save=true
when /\A(#{zip_name})\/(themes)\/.*(\.css)\z/
#for themes css
themes_entries << entry
when /\A(#{zip_name})\/(javascripts)\/.*(\.js)\z/
#for js
javascripts_entries << entry
when /\A(#{zip_name})\/(images)\/.*((\.jpg)|(\.png)|(\.gif))\z/ #for img
images_entries << entry
end
end
['themes', 'javascripts', 'images'].each do |type|
eval("#{type}_entries").each do |entry|
filename = File.basename entry.to_s
temp_file = File.new(dir + '/' + filename, 'w+')
temp_file.write (zip_file.read entry).force_encoding('UTF-8')
build_and_store = eval("design.#{type}").build
build_and_store.file = temp_file
build_and_store.file_filename = filename
build_and_store.to_save = true
end
end
}
design.save
}
end
end end