From fcfc0e3f2669b223c1e0af6600e24319ea8d9adb Mon Sep 17 00:00:00 2001 From: Johan Kim Date: Mon, 23 Mar 2015 12:20:15 +0900 Subject: [PATCH] helper: file_exist (in sass-extension) --- .../compass/core/sass_extensions/functions.rb | 3 ++- .../sass_extensions/functions/file_exist.rb | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 core/lib/compass/core/sass_extensions/functions/file_exist.rb diff --git a/core/lib/compass/core/sass_extensions/functions.rb b/core/lib/compass/core/sass_extensions/functions.rb index d50daaf4e1..66f823dfd8 100644 --- a/core/lib/compass/core/sass_extensions/functions.rb +++ b/core/lib/compass/core/sass_extensions/functions.rb @@ -11,7 +11,7 @@ def declare(*args) inline_image image_size constants gradient_support font_files lists colors math env cross_browser_support configuration - files + files file_exist ).each do |func| require "compass/core/sass_extensions/functions/#{func}" end @@ -33,6 +33,7 @@ module Sass::Script::Functions include Compass::Core::SassExtensions::Functions::Math include Compass::Core::SassExtensions::Functions::CrossBrowserSupport include Compass::Core::SassExtensions::Functions::Env + include Compass::Core::SassExtensions::Functions::FileExist end # Wierd that this has to be re-included to pick up sub-modules. Ruby bug? diff --git a/core/lib/compass/core/sass_extensions/functions/file_exist.rb b/core/lib/compass/core/sass_extensions/functions/file_exist.rb new file mode 100644 index 0000000000..8038ecb759 --- /dev/null +++ b/core/lib/compass/core/sass_extensions/functions/file_exist.rb @@ -0,0 +1,24 @@ +module Compass::SassExtensions::Functions::FileExist + def file_exist(image_file) + image_file = image_file.value # get to the string value of the literal. + + # Compute the real path to the image on the file stystem if the generated_images_dir is set. + real_path = if Compass.configuration.images_path + images_path = Compass.configuration.images_path + if Pathname.new(images_path).relative? && Rails.present? && Rails.root.present? + File.join(Rails.root, "app", "assets", images_path, image_file) + else + File.join(images_path, image_file) + end + else + images_path = Compass.configuration.project_path + if Pathname.new(images_path).relative? && Rails.present? && Rails.root.present? + File.join(Rails.root, "public", images_path, image_file) + else + File.join(images_path, image_file) + end + end + + return Sass::Script::Bool.new(File.exist?(real_path)) + end +end \ No newline at end of file