Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

generate theme archieve file. #149.

  • Loading branch information...
commit f9dfb488b0275a2dee6d7def02d540f34a8128e5 1 parent 02a178c
@saberma saberma authored
View
4 Rakefile
@@ -6,6 +6,8 @@ require 'rake'
require 'thinking_sphinx/deltas/resque_delta/tasks'
require 'resque/tasks'
-task "resque:setup" => :environment
+task "resque:setup" => :environment do
+ Resque.before_fork = Proc.new { ActiveRecord::Base.establish_connection } # 第二次执行resque任务时失败 PGError: ERROR: prepared statement "a3" already exists 参考 http://j.mp/rpVqhc http://j.mp/rqj9CQ ;Rails已经打了补丁,未发布版本 http://j.mp/ntPQMr
+end
Shopqi::Application.load_tasks
View
6 app/assets/javascripts/backbone/views/themes/show.js.coffee
@@ -7,6 +7,7 @@ App.Views.Theme.Show = Backbone.View.extend
'click .publish-theme-link a' : 'publish'
'click .duplicate-theme a' : 'duplicate'
'click .delete-theme a' : 'destroy'
+ 'click .export-theme a' : 'export'
'click .publish-theme-dropdown a' : 'cancel'
'submit form' : 'save'
@@ -49,6 +50,11 @@ App.Views.Theme.Show = Backbone.View.extend
msg '删除成功!'
false
+ export: ->
+ $.post "/admin/themes/#{@model.id}/export", ->
+ msg "正将模板、附件等打包成一个压缩文件,此过程需要几分钟的时间,完成后我们会将文件发送至 ", 5000
+ false
+
save: ->
$.post "/admin/themes/#{@model.id}", 'theme[role]': @$("select[name='theme[role]']").val(), _method: 'put', ->
window.location = window.location # 刷新页面
View
16 app/assets/javascripts/plugins.js.coffee
@@ -7,20 +7,20 @@ window.log = ->
((b) ->
c = ->
d = "assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(",")
-
+
while a = d.pop()
b[a] = b[a] or c
) window.console = window.console or {}
-window.msg = (text) ->
- flash_message text, 'notice'
+window.msg = (text, delay = null) ->
+ flash_message text, 'notice', delay
-window.error_msg = (text) ->
- flash_message text, 'errors'
+window.error_msg = (text, delay = null) ->
+ flash_message text, 'errors', delay
-window.flash_message = (text, type) ->
+window.flash_message = (text, type, delay = 2000) ->
if $("#sticky-progress")[0] # 指引
$("#sticky-progress").children('.flash').remove()
- $("<div/>").addClass("flash #{type}").html(text).prependTo("#sticky-progress").fadeIn(100).delay(2000).fadeOut()
+ $("<div/>").addClass("flash #{type}").html(text).prependTo("#sticky-progress").fadeIn(100).delay(delay).fadeOut()
else
- $("#flash#{type}").html(text).fadeIn(100).delay(2000).fadeOut 500
+ $("#flash#{type}").html(text).fadeIn(100).delay(delay).fadeOut 500
View
5 app/controllers/themes_controller.rb
@@ -45,6 +45,11 @@ def destroy # 删除
render json: theme.to_json(methods: :name, except: [:created_at, :updated_at])
end
+ def export # 导出(后台任务型)
+ Resque.enqueue(ThemeExporter, shop.id, params[:id].to_i)
+ render nothing: true
+ end
+
end
end
View
17 app/jobs/theme_exporter.rb
@@ -0,0 +1,17 @@
+# encoding: utf-8
+module ThemeExporter
+
+ @queue = "theme_exporter"
+
+ def self.perform(shop_id, theme_id) # 导出tmp/theme_exporter/shopqi/qiao-mu-ling-di-057ae9ce55.tar.gz
+ shop = Shop.find(shop_id)
+ theme = shop.themes.find(theme_id)
+ theme_exporter_path = Rails.root.join('tmp', 'theme_exporter', test_dir, shop.domains.myshopqi.subdomain)
+ FileUtils.mkdir_p theme_exporter_path
+ repo = Grit::Repo.new theme.public_path
+ sha = repo.commits(nil, 1).first.sha[0, 10]
+ file_path = File.join theme_exporter_path, "#{Pinyin.t(theme.name, '-')}-#{sha}.tar.gz"
+ repo.archive_to_file('master', nil, file_path) unless File.exists?(file_path)
+ end
+
+end
View
3  app/models/shop_theme.rb
@@ -159,8 +159,7 @@ def duplicate # 复制主题
begin #相对路径
def files_relative_path # s/files/1/theme/1
- test = %w(test travis).include?(Rails.env) ? Rails.env : '' #测试目录与其他环境分开,不干扰
- File.join 's', 'files', test, self.shop_id.to_s, 'theme', self.id.to_s
+ File.join 's', 'files', test_dir, self.shop_id.to_s, 'theme', self.id.to_s
end
def asset_relative_path(asset) # s/files/1/theme/1/assets/theme.liquid
View
2  app/views/themes/index.html.haml
@@ -74,7 +74,7 @@
%li.theme-editor
%a(href="/admin/themes/{{id}}/assets") 模板编辑器
%li.export-theme
- %a(href="/admin/themes/{{id}}") 导出主题
+ %a(href="#") 导出主题
%ul.theme-actions.extra.clearfix
{{#if is_published}}
%span.main-theme 已经发布为{{#if is_main}}普通{{else}}手机{{/if}}主题
View
28 config/initializers/0_load_first.rb
@@ -0,0 +1,28 @@
+
+# Methods in this file need to be loaded first.
+# This is because other initializers may depend on them.
+# The funky filename? Rails loads the initializers in alphabetical order.
+
+def development?
+ env_is('development')
+end
+
+def staging?
+ env_is('staging')
+end
+
+def production?
+ env_is('production')
+end
+
+def test?
+ env_is('test')
+end
+
+def env_is(env)
+ Rails.env == env
+end
+
+def test_dir #测试目录与其他环境分开,不干扰,用于保存主题文件
+ %w(test travis).include?(Rails.env) ? Rails.env : ''
+end
View
1  config/routes.rb
@@ -241,6 +241,7 @@
resources :themes, only: [:index, :update, :destroy] do
member do
post :duplicate # 复制主题
+ post :export # 导出主题
begin 'settings' # 外观设置
get :settings , to: 'shop_theme_settings#show'
put :settings , to: 'shop_theme_settings#update'
View
18 spec/jobs/theme_exporter_spec.rb
@@ -0,0 +1,18 @@
+require 'spec_helper'
+
+describe ThemeExporter do
+
+ let(:shop) { Factory(:user).shop }
+
+ let(:theme) { shop.theme }
+
+ it 'should be export' do
+ Grit.debug = true
+ with_resque do
+ Resque.enqueue(ThemeExporter, shop.id, theme.id)
+ end
+ File.exist?("tmp/theme_exporter/test/#{shop.domains.myshopqi.subdomain}").should be_true
+ FileUtils.rm_rf("tmp/theme_exporter/test")
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.