Skip to content

Commit

Permalink
link the asset direct to public.
Browse files Browse the repository at this point in the history
  • Loading branch information
saberma committed Oct 24, 2011
1 parent 489b4ad commit cf02323
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -56,3 +56,5 @@ data

#redis
dump.rdb
resque.pid
resque_scheduler.pid
6 changes: 4 additions & 2 deletions app/jobs/theme_extracter.rb
Expand Up @@ -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}/")
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions app/liquids/shop_drop.rb
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/liquids/url_filter.rb
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/models/asset.rb
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions app/models/shop.rb
Expand Up @@ -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)
Expand Down
57 changes: 35 additions & 22 deletions app/models/shop_theme.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/views/layouts/shop/checkout.html.haml
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/shopqi/registrations/new.html.haml
Expand Up @@ -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="")
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/liquid.rb
Expand Up @@ -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

Expand Down
9 changes: 5 additions & 4 deletions db/seeds.rb
@@ -1,9 +1,10 @@
# encoding: utf-8
############### 注意 ###############
# #
# 放在这里的数据都要支持多次执行 #
# 例如:新增操作执行前要判断未存在 #
# #
#
# 数据操作都放这里,不要放在migrate脚本
# 放在这里的数据都要支持多次执行
# 例如:新增操作执行前要判断未存在
#
############### 注意 ###############

# 创建官网管理员
Expand Down
10 changes: 8 additions & 2 deletions 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
3 changes: 0 additions & 3 deletions public/s/files/README.textile

This file was deleted.

0 comments on commit cf02323

Please sign in to comment.