From f78400c27443a0fda17e806509da6bad875772a2 Mon Sep 17 00:00:00 2001 From: chris2tof Date: Thu, 1 Sep 2011 19:06:49 +0800 Subject: [PATCH] Clean code Moved the unzip_design method as protected --- app/controllers/admin/designs_controller.rb | 101 ++++++++++---------- 1 file changed, 52 insertions(+), 49 deletions(-) diff --git a/app/controllers/admin/designs_controller.rb b/app/controllers/admin/designs_controller.rb index 1b258600..84da2334 100644 --- a/app/controllers/admin/designs_controller.rb +++ b/app/controllers/admin/designs_controller.rb @@ -22,55 +22,6 @@ class Admin::DesignsController < ApplicationController temp_file.close 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 @designs = Design.all.entries @@ -132,5 +83,57 @@ class Admin::DesignsController < ApplicationController render :action => 'new' 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