Skip to content
Browse files

make url_for_*_column return relative URLs

git-svn-id: http://opensvn.csie.org/rails_file_column/plugins/file_column/trunk@57 6a5db2c9-b104-0410-84e5-917aad89f9d9
  • Loading branch information...
1 parent f3d05df commit 0b2d40b3cbce6367696c2166670f68cb16e1c46c skanthak committed Jan 31, 2006
Showing with 85 additions and 4 deletions.
  1. +5 −0 CHANGELOG
  2. +33 −2 lib/file_column_helper.rb
  3. +4 −2 test/abstract_unit.rb
  4. +43 −0 test/file_column_helper_test.rb
View
5 CHANGELOG
@@ -15,6 +15,11 @@
column is declared as NON NULL on some databases
* don't expand absolute paths. This is necessary for file_column to work
when your rails app is deployed into a sub-directory via a symbolic link
+ * url_for_*_column will no longer return absolute URLs! Instead, although the
+ generated URL starts with a slash, it will be relative to your application's
+ root URL. This is so, because rails' image_tag helper will automatically
+ convert it to an absolute URL. If you need an absolute URL (e.g., to pass
+ it to link_to) use url_for_file_column's :absolute => true option.
0.3.1
* make object with file_columns serializable
View
35 lib/file_column_helper.rb
@@ -40,17 +40,44 @@ def file_column_field(object, method, options={})
# This will produce a valid URL even for temporary uploaded files, e.g. files where the object
# they are belonging to has not been saved in the database yet.
#
+ # The URL produces, although starting with a slash, will be relative
+ # to your app's root. If you pass it to one rails' +image_tag+
+ # helper, rails will properly convert it to an absolute
+ # URL. However, this will not be the case, if you create a link with
+ # the +link_to+ helper. In this case, you can pass <tt>:absolute =>
+ # true</tt> to +options+, which will make sure, the generated URL is
+ # absolute on your server. Examples:
+ #
+ # <%= image_tag url_for_file_column(@entry, "image") %>
+ # <%= link_to "Download", url_for_file_column(@entry, "image", :absolute => true) %>
+ #
# If there is currently no uploaded file stored in the object's column this method will
# return +nil+.
- def url_for_file_column(object, method, subdir=nil)
+ def url_for_file_column(object, method, options=nil)
case object
when String, Symbol
object = instance_variable_get("@#{object.to_s}")
end
+
+ # parse options
+ subdir = nil
+ absolute = false
+ if options
+ case options
+ when Hash
+ subdir = options[:subdir]
+ absolute = options[:absolute]
+ when String, Symbol
+ subdir = options
+ end
+ end
+
relative_path = object.send("#{method}_relative_path", subdir)
return nil unless relative_path
+
url = ""
- url << @request.relative_url_root.to_s << "/"
+ url << request.relative_url_root.to_s if absolute
+ url << "/"
url << object.send("#{method}_options")[:base_url] << "/"
url << relative_path
end
@@ -99,6 +126,10 @@ def url_for_file_column(object, method, subdir=nil)
# define it in a helper to stay with DRY. Another option is to define it in the model via
# FileColumn::Magick's <tt>:versions</tt> feature and then refer to it via a symbol.
#
+ # The URL produced by this method is relative to your application's root URL,
+ # although it will start with a slash.
+ # If you pass this URL to rails' +image_tag+ helper, it will be converted to an
+ # absolute URL automatically.
# If there is currently no image uploaded, this method will return +nil+.
def url_for_image_column(object, method, options=nil)
case object
View
6 test/abstract_unit.rb
@@ -26,8 +26,10 @@ class ActiveRecord::Base
class RequestMock
- def relative_url_root
- ""
+ attr_accessor :relative_url_root
+
+ def initialize
+ @relative_url_root = ""
end
end
View
43 test/file_column_helper_test.rb
@@ -52,3 +52,46 @@ def test_url_for_file_column_without_extension
end
end
+class UrlForFileColumnTest < Test::Unit::TestCase
+ include FileColumnHelper
+ include ActionView::Helpers::AssetTagHelper
+ include ActionView::Helpers::TagHelper
+ include ActionView::Helpers::UrlHelper
+
+ def setup
+ Entry.file_column :image
+
+ # mock up some request data structures for AssetTagHelper
+ @request = RequestMock.new
+ @request.relative_url_root = "/foo/bar"
+ @controller = self
+ end
+
+ def request
+ @request
+ end
+
+ IMAGE_URL = %r{^/foo/bar/entry/image/.+/skanthak.png$}
+ def test_with_image_tag
+ e = Entry.new(:image => upload("skanthak.png"))
+ html = image_tag url_for_file_column(e, "image")
+ url = html.scan(/src=\"(.+)\"/).first.first
+
+ assert_match IMAGE_URL, url
+ end
+
+ def test_with_link_to_tag
+ e = Entry.new(:image => upload("skanthak.png"))
+ html = link_to "Download", url_for_file_column(e, "image", :absolute => true)
+ url = html.scan(/href=\"(.+)\"/).first.first
+
+ assert_match IMAGE_URL, url
+ end
+
+ def test_relative_url_root_not_modified
+ e = Entry.new(:image => upload("skanthak.png"))
+ url_for_file_column(e, "image", :absolute => true)
+
+ assert_equal "/foo/bar", @request.relative_url_root
+ end
+end

0 comments on commit 0b2d40b

Please sign in to comment.
Something went wrong with that request. Please try again.