Skip to content

Commit

Permalink
Enabled "static" assets symlinked to static bricks. Now css,
Browse files Browse the repository at this point in the history
javascript, etc can live inside the static brick. Added language
to dictionaries: dict.en.yml, dict.fr.yml.
  • Loading branch information
gaspard committed Jul 1, 2012
1 parent 6171e1f commit 928342f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 25 deletions.
1 change: 1 addition & 0 deletions History.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Enabled javascript on toggle.
* Enabled "static" brick to use zafu stored in brick file system.
* No need to use '>' or '<' in html params. \' escape works. Yeah !!
* Enabled "static" assets with symlink to skins in static bricks (/static/[brick]-[skin]/xxx.css => bricks/[brick]/skins/[skin]/static/xxx.css)

* Minor changes
* Fixed label traduction for param.
Expand Down
31 changes: 27 additions & 4 deletions bricks/static/lib/bricks/static.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,19 @@ def base.text_from_static(brick_name, skin_name, path, opts)
fullpath = "$#{brick_name}-#{skin_name}/#{path}"
section_id = nil
template = nil
abs_path = File.join(
RAILS_ROOT, 'bricks', brick_name,
'zena', 'skins', skin_name, path + "." + (opts[:ext] || 'zafu'))

abs_path = File.join(RAILS_ROOT, 'bricks', brick_name, 'zena', 'skins', skin_name, path)
if opts[:ext]
abs_path_l = abs_path + ".#{visitor.lang}.#{opts[:ext]}"
abs_path = abs_path + ".#{opts[:ext]}"
else
abs_path_l ||= abs_path + ".zafu"
end
if abs_path_l
if text = File.exist?(abs_path_l) ? File.read(abs_path_l) : nil
return text, fullpath, section_id, template
end
end
if text = File.exist?(abs_path) ? File.read(abs_path) : nil
return text, fullpath, section_id, template
end
Expand Down Expand Up @@ -123,7 +133,20 @@ def rebuild_static_index
end

private
def add_static_symlink(brick_name, skin_name, skin_path)
source_path = File.join(skin_path, 'static')
target_dir = File.join(RAILS_ROOT, 'public', 'static')
target_path = File.join(target_dir, "#{brick_name}-#{skin_name}")
if File.exist?(source_path) && !File.exist?(target_path)
if !File.exist?(target_dir)
Dir.mkdir(target_dir)
end
FileUtils.symlink_or_copy(source_path, target_path)
end
end

def build_static_index(brick_name, skin_name, path)
add_static_symlink(brick_name, skin_name, path)
# path = absolute path
# 1. Find all templates
Dir.foreach(path) do |elem|
Expand Down Expand Up @@ -156,4 +179,4 @@ def build_static_index(brick_name, skin_name, path)
end
end # SiteMethods
end # Static
end # Bricks
end # Bricks
6 changes: 5 additions & 1 deletion lib/tasks/zena.rake
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def symlink_assets(from, to)
# we could create a symlink in the sites dir to 'shared' -> /var/zena/current/public
# and then symlink with "#{host_path}/public/#{dir}" -> "../shared/public/#{dir}"
# OR we could symlink /var/zena/current/...
['calendar', 'images', 'javascripts', 'stylesheets'].each do |dir|
['static', 'calendar', 'images', 'javascripts', 'stylesheets'].each do |dir|
File.unlink("#{to}/public/#{dir}") if File.symlink?("#{to}/public/#{dir}")
if File.exist?("#{to}/public/#{dir}")
if File.directory?("#{to}/public/#{dir}")
Expand Down Expand Up @@ -355,12 +355,16 @@ namespace :zena do

desc 'Rebuild index for all sites or site defined by HOST param.'
task :rebuild_index => :environment do
# Make sure all bricks are loaded before executing the index rebuild
Zena::Use.upgrade_class('Site')

include Zena::Acts::Secure
if ENV['HOST']
sites = [Site.find_by_host(ENV['HOST'])]
else
sites = Site.all
end

sites.each do |site|
if ENV['WORKER'] == 'false' || RAILS_ENV == 'test'
# We avoid SiteWorker by passing nodes.
Expand Down
9 changes: 8 additions & 1 deletion lib/zafu/process/html.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Zafu
module Process
module HTML
SAFE_SRC_REGEX = %r{\A/[^\.]+\.[a-zA-Z]+\Z}
def self.included(base)
base.wrap :wrap_html
end
Expand Down Expand Up @@ -118,7 +119,13 @@ def r_rename_asset

src = @params.delete(key)
if src && src[0..7] != 'http://'
new_value = helper.send(:template_url_for_asset, :src => src, :base_path => @options[:base_path], :type => type)
if new_value = helper.send(:template_url_for_asset, :src => src, :base_path => @options[:base_path], :type => type)
elsif src =~ SAFE_SRC_REGEX
fullpath = "#{SITES_ROOT}#{current_site.public_path}#{src}"
if File.exist?(fullpath) && File.stat(fullpath)
src = "#{src}?#{File.mtime(fullpath).to_i}"
end
end
@markup.params[key] = new_value.blank? ? src : new_value
end

Expand Down
22 changes: 4 additions & 18 deletions lib/zena/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,11 @@ def self.included(base)
end
Bricks.apply_patches('application_controller.rb')
Bricks.apply_patches('application_helper.rb')

::User.class_eval do
Zena::Use.each_module_for('User') do |mod|
include mod
end
end

::Site.class_eval do
Zena::Use.each_module_for('Site') do |mod|
include mod
end
end

::Skin.class_eval do
Zena::Use.each_module_for('Skin') do |mod|
puts mod
include mod
end
end
Zena::Use.upgrade_class('User')
Zena::Use.upgrade_class('Site')
Zena::Use.upgrade_class('Skin')

end
end
end
15 changes: 14 additions & 1 deletion lib/zena/use.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Use
MODULE_NAME = Hash[*MODULE_NAMES.map {|n| [n, "#{n}#{SUFFIX_NAME}"]}.flatten]

class << self
attr_accessor :modules, :extra_routes
attr_accessor :modules, :extra_routes, :upgraded_classes

# Declare a module (or list of modules) that should be used in Zena. The module should implement
# sub-modules named ControllerMethods, ViewMethods or ZafuMethods in order to add features to
Expand Down Expand Up @@ -37,6 +37,19 @@ def modules_for(name)
create_module_hash
self.modules[name] || []
end

def upgrade_class(class_name)
self.upgraded_classes ||= {}
if !self.upgraded_classes[class_name]
self.upgraded_classes[class_name] = true
klass = eval "::#{class_name}"
klass.class_eval do
Zena::Use.each_module_for(class_name) do |mod|
include mod
end
end
end
end

def routes(rez)
if self.extra_routes.nil?
Expand Down

0 comments on commit 928342f

Please sign in to comment.