diff --git a/.gitignore b/.gitignore index 6289755b..ef69cfb0 100644 --- a/.gitignore +++ b/.gitignore @@ -56,3 +56,5 @@ data #redis dump.rdb +resque.pid +resque_scheduler.pid diff --git a/app/jobs/theme_extracter.rb b/app/jobs/theme_extracter.rb index 73f4e0bc..6f3bd5d9 100644 --- a/app/jobs/theme_extracter.rb +++ b/app/jobs/theme_extracter.rb @@ -6,8 +6,8 @@ module ThemeExtracter def self.perform(shop_id, theme_id, zip_path, addition_root_dir) # 用户上传主题文件后,转入后台解压 shop = Shop.find(shop_id) shop_theme = shop.themes.find(theme_id) - path = shop_theme.public_path - repo = Grit::Repo.init shop_theme.public_path # 初始化为git repo + path = shop_theme.path + repo = Grit::Repo.init path # 初始化为git repo begin Zip::ZipFile::open(zip_path) do |zf| addition_regexp = Regexp.new("^#{addition_root_dir}/") @@ -27,6 +27,8 @@ def self.perform(shop_id, theme_id, zip_path, addition_root_dir) # 用户上传 shop_theme.config_settings['presets'][shop_theme.load_preset].each_pair do |name, value| shop_theme.settings.create name: name, value: value end + FileUtils.mkdir_p shop_theme.public_path # 主题文件只有附件对外公开,其他文件不能被外部访问 + FileUtils.ln_s File.join(path, 'assets'), File.join(shop_theme.public_path, 'assets') shop_theme.unpublish! end diff --git a/app/liquids/shop_drop.rb b/app/liquids/shop_drop.rb index d1f5e418..5780f2a2 100644 --- a/app/liquids/shop_drop.rb +++ b/app/liquids/shop_drop.rb @@ -18,8 +18,8 @@ def url end # UrlFilter调用 - def asset_path(asset) - @theme.asset_relative_path(asset) + def asset_url(asset) + @theme.asset_url(asset) end end diff --git a/app/liquids/url_filter.rb b/app/liquids/url_filter.rb index 32ad4a20..20e7fc53 100644 --- a/app/liquids/url_filter.rb +++ b/app/liquids/url_filter.rb @@ -2,7 +2,7 @@ module UrlFilter def asset_url(input) shop = @context['shop'] #ShopDrop - "/#{shop.asset_path(input)}" + shop.asset_url(input) end def global_asset_url(input) diff --git a/app/models/asset.rb b/app/models/asset.rb index 2f7a52f5..593de485 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -13,7 +13,7 @@ def initialize(theme, key, name, url = nil) else extensions = name.split('.')[1] if !extensions.blank? and %w(jpg gif png jpeg).include? extensions - "/#{theme.asset_relative_path(name)}" + theme.asset_url(name) end end end diff --git a/app/models/shop.rb b/app/models/shop.rb index ba35dce0..5b1e0e6d 100644 --- a/app/models/shop.rb +++ b/app/models/shop.rb @@ -87,6 +87,14 @@ def available? !self.deadline.past? end + def path # 商店相关的文件(主题)存放路径 /data/shops/1 + Rails.root.join 'data', 'shops', self.id.to_s + end + + after_destroy do + FileUtils.rm_rf self.path # 删除对应的目录 + end + protected def init_valid_date self.deadline = Date.today.next_day(30) diff --git a/app/models/shop_theme.rb b/app/models/shop_theme.rb index f6750c1d..09d9dede 100644 --- a/app/models/shop_theme.rb +++ b/app/models/shop_theme.rb @@ -5,12 +5,12 @@ class ShopThemeSetting < ActiveRecord::Base # 修改此模块内方法要记得重启服务 module Extension # http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html #Association extensions - def html_path # public/s/files/1/theme/1/config/settings.html - File.join theme.public_path, 'config', 'settings.html' + def html_path # data/shops/1/theme/1/config/settings.html + File.join theme.path, 'config', 'settings.html' end - def data_path # public/s/files/1/theme/1/config/settings_data.json - File.join theme.public_path, 'config', 'settings_data.json' + def data_path # data/shops/1/theme/1/config/settings_data.json + File.join theme.path, 'config', 'settings_data.json' end def as_json @@ -48,7 +48,7 @@ def transform # settings.html的内容要先经过处理: 上传、字体、改 doc = Nokogiri::HTML(File.open(html_path), nil, 'utf-8') #http://nokogiri.org/tutorials/modifying_an_html_xml_document.html doc.css("input[type='file']").each do |file| # 上传文件 name = file['name'] - url = "/#{theme.files_relative_path}/assets/#{name}" + url = theme.asset_url(name) td = file.parent builder = Nokogiri::HTML::Builder.new do table.widget(cellspacing: 0) { @@ -131,13 +131,15 @@ class ShopTheme < ActiveRecord::Base after_create do if self.theme_id # 应用某个主题,而非手动上传主题 - repo = Grit::Repo.init public_path # 初始化为git repo - FileUtils.cp_r "#{theme.path}/.", public_path + repo = Grit::Repo.init path # 初始化为git repo + FileUtils.cp_r "#{theme.path}/.", path commit repo, '1' self.load_preset ||= config_settings['current'] # 初始化主题设置 config_settings['presets'][self.load_preset].each_pair do |name, value| self.settings.create name: name, value: value end + FileUtils.mkdir_p public_path # 主题文件只有附件对外公开,其他文件不能被外部访问 + FileUtils.ln_s File.join(path, 'assets'), File.join(public_path, 'assets') end end @@ -178,55 +180,66 @@ def files_relative_path # s/files/1/theme/1 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 + def asset_relative_path(asset) # s/files/1/theme/1/assets/checkout.css File.join files_relative_path, 'assets', asset end end + begin # 当前theme所在URL + def asset_url(name) # s/files/1/theme/1/assets/checkout.css + "/#{self.asset_relative_path(name)}" + end + end + begin # 当前theme所在PATH + def path # data/shops/1/themes/1 + File.join shop.path, 'themes', self.id.to_s + end + def public_path # public/s/files/1/theme/1 File.join Rails.root, 'public', files_relative_path end - def config_settings_path # public/s/files/1/config/settings.html - File.join public_path, 'config', 'settings.html' + def config_settings_path # data/shops/1/themes/1/config/settings.html + File.join path, 'config', 'settings.html' end - def config_settings_data_path # public/s/files/1/config/settings_data.json - File.join public_path, 'config', 'settings_data.json' + def config_settings_data_path # data/shops/1/themes/1/config/settings_data.json + File.join path, 'config', 'settings_data.json' end def config_settings JSON(File.read(config_settings_data_path)) end - def asset_path(asset) # public/s/files/1/theme/1/assets/checkout.css.liquid + def asset_path(asset) # data/shops/1/themes/1/assets/checkout.css.liquid asset_liquid = "#{asset}.liquid" - path = File.join public_path, 'assets', asset_liquid - if File.exist?(path) #存在liquid文件,则解释liquid - path + liquid_path = File.join path, 'assets', asset_liquid + if File.exist?(liquid_path) #存在liquid文件,则解释liquid + liquid_path else - File.join public_path, 'assets', asset + File.join path, 'assets', asset end end - def layout_theme_path # public/s/files/1/theme/1/layout/theme.liquid - File.join public_path, 'layout', 'theme.liquid' + def layout_theme_path # data/shops/1/theme/1/layout/theme.liquid + File.join path, 'layout', 'theme.liquid' end - def template_path(template) # public/s/files/1/theme/1/templates/products.liquid - File.join public_path, 'templates', "#{template}.liquid" + def template_path(template) # data/shops/1/theme/1/templates/products.liquid + File.join path, 'templates', "#{template}.liquid" end end def commit(repo, message) # 提交 - Dir.chdir public_path do # 必须切换当前目录,否则报错: fatal: 'path' is outside repository + Dir.chdir path do # 必须切换当前目录,否则报错: fatal: 'path' is outside repository repo.add '.' repo.commit_all message end end after_destroy do #删除对应的目录 + FileUtils.rm_rf self.path FileUtils.rm_rf self.public_path end end diff --git a/app/views/layouts/shop/checkout.html.haml b/app/views/layouts/shop/checkout.html.haml index 60c6a162..9d265da2 100644 --- a/app/views/layouts/shop/checkout.html.haml +++ b/app/views/layouts/shop/checkout.html.haml @@ -9,7 +9,7 @@ =stylesheet_link_tag :layout_checkout /[if lt IE 7] =stylesheet_link_tag "ie-checkout" - %link(href="/#{shop.theme.asset_relative_path('checkout.css')}" media="screen" rel="stylesheet" type="text/css") + %link(href="#{shop.theme.asset_url('checkout.css')}" media="screen" rel="stylesheet" type="text/css") = javascript_include_tag :checkout_application %body #info-bar diff --git a/app/views/shopqi/registrations/new.html.haml b/app/views/shopqi/registrations/new.html.haml index 7580a6c2..e4805101 100644 --- a/app/views/shopqi/registrations/new.html.haml +++ b/app/views/shopqi/registrations/new.html.haml @@ -103,7 +103,7 @@ %input#shop_terms_and_conditions(style="display: inline; margin-bottom: 0" type="checkbox" value="1") %label#terms-and-conditions-label(for="shop_terms_and_conditions") 我已经阅读并同意 - %a(href="#") 服务条款 + %a#agreement_link(href="/agreement" target="_blank") 服务条款 %p.btns(style="margin-bottom: 0; clear: left") %input(name="shop[plan]" type="hidden" value="#{params[:plan]}") %input(name="ref" type="hidden" value="") diff --git a/config/initializers/liquid.rb b/config/initializers/liquid.rb index efdaa476..6d49b112 100644 --- a/config/initializers/liquid.rb +++ b/config/initializers/liquid.rb @@ -28,7 +28,7 @@ class Include def render_with_file_system(context) # 修正: 商店首页(woodland主题)错误This liquid context does not allow includes shop = context['shop'].instance_variable_get('@shop') - context.registers[:file_system] = Liquid::SnippetFileSystem.new(Rails.root.join('public', shop.theme.files_relative_path, 'snippets')) + context.registers[:file_system] = Liquid::SnippetFileSystem.new(File.join(shop.theme.path, 'snippets')) render_without_file_system(context) end diff --git a/db/seeds.rb b/db/seeds.rb index aeb2dbc0..c99ab55d 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,9 +1,10 @@ # encoding: utf-8 ############### 注意 ############### -# # -# 放在这里的数据都要支持多次执行 # -# 例如:新增操作执行前要判断未存在 # -# # +# +# 数据操作都放这里,不要放在migrate脚本 +# 放在这里的数据都要支持多次执行 +# 例如:新增操作执行前要判断未存在 +# ############### 注意 ############### # 创建官网管理员 diff --git a/lib/tasks/bootstrap.rake b/lib/tasks/bootstrap.rake index 8b0a6ce1..dcd90866 100644 --- a/lib/tasks/bootstrap.rake +++ b/lib/tasks/bootstrap.rake @@ -1,9 +1,15 @@ +# encoding: utf-8 +# 初始化开发环境(注意:原有数据和文件会被清空) +# bundle exec rake shopqi:bootstrap namespace :shopqi do desc "Run all bootstrapping tasks" task :bootstrap do - FileUtils.mkdir_p Rails.root.join('data') - Rake::Task['db:setup'].invoke + data_files = Rails.root.join('data', 'public_s', 'files') + public_files = Rails.root.join('public', 's', 'files') + FileUtils.mkdir_p data_files + FileUtils.ln_s data_files, public_files + Rake::Task['db:setup'].invoke # 会调用db:schema:load,而非db:migrate end end diff --git a/public/s/files/README.textile b/public/s/files/README.textile deleted file mode 100644 index af825798..00000000 --- a/public/s/files/README.textile +++ /dev/null @@ -1,3 +0,0 @@ -此目录放置商店的主题文件及手动上传的商品图片 - -生产环境下,将此目录链接至其他目录