0
require 'action_view/helpers/url_helper'
0
require 'action_view/helpers/tag_helper'
0
- RAILS_DEFAULT_JAVASCRIPTS = [:prototype, :effects, :dragdrop, :controls]
0
- attr_accessor :use_rails_default_javascripts
0
+# style_like :dolphins # assumes :media => 'all'
0
+# style_like :home, :media => 'print'
0
+# style_like :users, :media => ['print', 'screen']
0
+ DEFAULT_JAVASCRIPTS = ['prototype', 'effects', 'dragdrop', 'controls']
0
+ SCRIPT_PLACEHOLDER = '<!--SWEET_JAVASCRIPTS-->'
0
+ STYLE_PLACEHOLDER = '<!--SWEET_STYLESHEETS-->'
0
+ module ActionController
0
+ def self.included(base)
0
- include AssignmentMethods
0
- extend SweetAssetsShortcuts
0
+ include InstanceMethods
0
include AppendAssetsAfterRescue
0
- before_filter :initialize_assets_accessor
0
- before_filter :apply_default_styles
0
- before_filter :apply_default_scripts
0
- after_filter :apply_sweet_assets
0
+ after_filter :apply_sweet_assets
0
+ def style_like(*assets)
0
+ before_filter do |controller|
0
+ controller.send :style_like, *assets
0
- module SweetAssetsShortcuts
0
- def style_like(*assets)
0
- options = assets.extract_options!
0
- before_filter Proc.new {|controller| controller.style_like *assets }, options
0
+ def script_like(*assets)
0
+ before_filter do |controller|
0
+ controller.send :script_like, *assets
0
- def script_like(*assets)
0
- options = assets.extract_options!
0
- before_filter Proc.new {|controller| controller.script_like *assets }, options
0
+ module InstanceMethods
0
+ def style_like(*assets)
0
+ sweet_assets.style_like(*assets)
0
+ def script_like(*assets)
0
+ sweet_assets.script_like(*assets)
0
+ @sweet_assets ||= SweetAssets::Request.new(self)
0
+ def apply_sweet_assets
0
+ if response.body.respond_to?(:gsub!)
0
+ response.body.gsub! SweetAssets::STYLE_PLACEHOLDER, sweet_assets.tags.stylesheets
0
+ response.body.gsub! SweetAssets::SCRIPT_PLACEHOLDER, sweet_assets.tags.javascripts
0
- module AssignmentMethods
0
- def style_like(*styles)
0
- styles.each do |style|
0
- add_sweet_asset(style.to_s, :stylesheets)
0
+ def style_like(*assets)
0
+ sweet_assets.style_like(*assets)
0
+ def script_like(*assets)
0
+ sweet_assets.script_like(*assets)
0
+ SweetAssets::STYLE_PLACEHOLDER
0
+ SweetAssets::SCRIPT_PLACEHOLDER
0
+ controller.send :sweet_assets
0
- def script_like(*scripts)
0
- scripts.each do |script|
0
- if 'defaults' == script
0
- SweetAssets.use_rails_default_javascripts = true
0
- add_sweet_asset(script, :javascripts)
0
+ def initialize(controller)
0
+ @stylesheets = Array.new
0
+ @javascripts = Array.new
0
+ @controller = controller
0
+ # use the assets named 'application'
0
+ style_like :application
0
+ script_like :application
0
+ # use the assets named after the controller at higher priority
0
+ style_like "#{@controller.class.controller_name}!"
0
+ script_like "#{@controller.class.controller_name}!"
0
+ def style_like(*assets)
0
+ options = HashWithIndifferentAccess.new(assets.extract_options!)
0
+ media = [options[:media]].flatten.compact
0
+ media = ['all'] if media.blank?
0
+ media.each do |medium|
0
+ assets.each do |asset|
0
+ if asset.ends_with?('!')
0
+ # remove a lower-priority entry if it exists for this medium
0
+ @stylesheets.delete({:file => asset, :medium => medium, :priority => 'low'})
0
+ @stylesheets << {:file => asset, :medium => medium, :priority => 'high'}
0
+ # don't add this entry if it's already set to high-priority
0
+ unless @stylesheets.include?({:file => asset, :medium => medium, :priority => 'high'})
0
+ @stylesheets << {:file => asset, :medium => medium, :priority => 'low'}
0
- def add_sweet_asset(asset, collection)
0
- collection = sweet_assets[collection]
0
- if asset.ends_with?('!')
0
+ def script_like(*assets)
0
+ assets.each do |asset|
0
+ if 'defaults' == asset
0
+ DEFAULT_JAVASCRIPTS.each do |default|
0
+ @javascripts << {:file => default, :priority => 'first' }
0
+ elsif asset.ends_with?('!')
0
- collection[:top].delete(asset)
0
- collection[:bottom] << asset
0
+ # remove a lower-priority entry if it exists
0
+ @javascripts.delete({:file => asset, :priority => 'low'})
0
+ @javascripts << {:file => asset, :priority => 'high'}
0
- unless collection[:bottom].include?(asset)
0
- collection[:top] << asset
0
+ # don't add this entry if it's already set to high-priority
0
+ unless @javascripts.include?({:file => asset, :priority => 'high'})
0
+ @javascripts << {:file => asset, :priority => 'low'}
0
- self.is_a?(ActionView::Base) ?
0
- controller.instance_variable_get("@sweet_assets") :
0
- def method_missing_with_sweet_assets(method_name, *args, &block)
0
- if match = method_name.to_s.match(/^(style_like|script_like)_(\w+!?)$/)
0
- send match[1], match[2]
0
- method_missing_without_sweet_assets(method_name, *args, &block)
0
- alias_method_chain :method_missing, :sweet_assets
0
- def initialize_assets_accessor
0
- @sweet_assets = { :javascripts => {:top => [], :bottom => []},
0
- :stylesheets => {:top => [], :bottom => []} }
0
- def apply_default_styles
0
- style_like :application
0
- style_like "#{controller_name}!"
0
+ @tag_generator ||= TagGenerator.new(@stylesheets, @javascripts, @controller)
0
- def apply_default_scripts
0
- # we don't apply application.js by default because Rails already knows how to do that.
0
- script_like "#{controller_name}!"
0
+ include ::ActionView::Helpers::TagHelper
0
+ include ::ActionView::Helpers::AssetTagHelper
0
- def apply_sweet_assets
0
- return true unless @sweet_assets.any? {|asset_type, assets| assets.any? {|placement, files| !files.blank? } }
0
- generator = SweetAssetsGenerator.new(@sweet_assets, self)
0
- response.body.gsub! '<head>', "<head>\n#{generator.tags(:top)}\n" if response.body.respond_to?(:gsub!)
0
- response.body.gsub! '</head>', "\n#{generator.tags(:bottom)}\n</head>" if response.body.respond_to?(:gsub!)
0
- SweetAssets.use_rails_default_javascripts = false
0
+ def initialize(stylesheets, javascripts, controller)
0
+ @stylesheets = stylesheets
0
+ @javascripts = javascripts
0
+ @controller = controller
0
- class SweetAssetsGenerator
0
- include ActionView::Helpers::TagHelper
0
- include ActionView::Helpers::AssetTagHelper
0
+ media = @stylesheets.map {|asset| asset[:medium] }.uniq
0
- def initialize(assets, controller)
0
- @controller = controller
0
- javascript_tags(placement) + "\n" + stylesheet_tags(placement)
0
+ files_in_order = ['low', 'high'].map do |priority|
0
- def stylesheet_tags(placement)
0
- files = @assets[:stylesheets][placement].dup
0
- files.map! {|file| file =~ /\.css$/ ? file : "#{file}.css" }
0
- files = files.select {|file| File.exists?("#{STYLESHEETS_DIR}/#{file}") } unless RAILS_ENV.eql?('test')
0
- return '' if files.blank?
0
- files << {:cache => "sweet_stylesheets_#{files.join(',')}" } if ActionController::Base.perform_caching
0
- stylesheet_link_tag *files
0
+ files = @stylesheets.select do |asset|
0
+ asset[:medium] == medium && asset[:priority] == priority
0
+ files.map! {|asset| asset[:file] }
0
+ files.map! {|file| file =~ /\.css$/ ? file : "#{file}.css" }
0
- def javascript_tags(placement)
0
- files = @assets[:javascripts][placement].dup
0
- if SweetAssets.use_rails_default_javascripts
0
- files = SweetAssets::PRIMARY_JAVASCRIPTS + files
0
+ # check that the files exist
0
+ unless RAILS_ENV.eql?('test')
0
+ files = files.select {|file| File.exists?("#{STYLESHEETS_DIR}/#{file}") }
0
+ end.flatten.uniq.compact
0
+ stylesheet_link_tag(*(files_in_order << {:media => medium, :cache => cached_file_name(files_in_order)}))
0
+ files_in_order = ['first', 'low', 'high'].map do |priority|
0
+ files = @javascripts.select {|asset| asset[:priority] == priority }
0
+ files.map! {|asset| asset[:file] }
0
files.map! {|file| file =~ /\.js$/ ? file : "#{file}.js" }
0
- files = files.select {|file| File.exists?("#{JAVASCRIPTS_DIR}/#{file}") } unless RAILS_ENV.eql?('test')
0
- return '' if files.blank?
0
- files << {:cache => "sweet_javascripts_#{files.join(',')}" } if ActionController::Base.perform_caching
0
- javascript_include_tag *files
0
+ # check that the files exist
0
+ unless RAILS_ENV.eql?('test')
0
+ files = files.select {|file| File.exists?("#{JAVASCRIPTS_DIR}/#{file}") }
0
+ end.flatten.uniq.compact
0
+ files_in_order << {:cache => cached_file_name(files_in_order)}
0
+ javascript_include_tag *files_in_order
0
+ def cached_file_name(filenames)
0
+ "sweet_assets_#{Digest::MD5.hexdigest(filenames.join('_'))}"
0
# we're going to wrap our rescue_action around the top-level rescue_action method chain
0
# this ensures that we can apply the stylesheets even if the after filter was aborted due to an exception
0
module AppendAssetsAfterRescue
0
- def self.included(base)
0
- def initialize_with_append_sweet_assets_rescue_action(*args)
0
- initialize_without_append_sweet_assets_rescue_action(*args)
0
- self.class.send :alias_method_chain, :rescue_action, :append_sweet_assets unless respond_to?(:rescue_action_without_append_sweet_assets)
0
- alias_method_chain :initialize, :append_sweet_assets_rescue_action
0
- def rescue_action_with_append_sweet_assets(*args)
0
- rescue_action_without_append_sweet_assets(*args)
0
+ def rescue_action(*args)
0
\ No newline at end of file
Comments
No one has commented yet.