<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>test/magick_view_only_test.rb</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -1,10 +1,11 @@
 *svn*
     * allow for directories in file_column dirs as well
     * use subdirs for versions instead of fiddling with filename
-    * implemented dynamic resizing of images from views
+    * url_for_image_column_helper for dynamic resizing of images from views
     * new &quot;crop&quot; feature [Sean Treadway]
-    * do not require model objects to be stored in instance variable in
-      url_for_file_column helper
+    * url_for_file_column helper: do not require model objects to be stored in
+      instance variables
+      
 
 0.3.1
     * make object with file_columns serializable</diff>
      <filename>CHANGELOG</filename>
    </modified>
    <modified>
      <diff>@@ -32,4 +32,5 @@ task :test do
   sh &quot;cd test; ruby file_column_test.rb&quot;
   sh &quot;cd test; ruby file_column_helper_test.rb&quot;
   sh &quot;cd test; ruby magick_test.rb&quot;
+  sh &quot;cd test; ruby magick_view_only_test.rb&quot;
 end</diff>
      <filename>Rakefile</filename>
    </modified>
    <modified>
      <diff>@@ -42,37 +42,72 @@ module FileColumnHelper
   #
   # 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)
+    case object
+    when String, Symbol
+      object = instance_variable_get(&quot;@#{object.to_s}&quot;)
+    end
+    relative_path = object.send(&quot;#{method}_relative_path&quot;, subdir)
+    return nil unless relative_path
+    url = &quot;&quot;
+    url &lt;&lt; @request.relative_url_root.to_s &lt;&lt; &quot;/&quot;
+    url &lt;&lt; object.send(&quot;#{method}_options&quot;)[:base_url] &lt;&lt; &quot;/&quot;
+    url &lt;&lt; relative_path
+  end
+
+  # Same as +url_for_file_colum+ but allows you to access different versions
+  # of the image that have been processed by RMagick.
   #
-  # If your +options+ parameter contains a key &lt;tt&gt;:version&lt;/tt&gt; this will
+  # If your +options+ parameter is non-nil this will
   # access a different version of an image that will be produced by
-  # RMagick. You can use the following types of versions:
+  # RMagick. You can use the following types for +options+:
   #
-  # * &lt;tt&gt;:version =&gt; :symbol&lt;/tt&gt; will select a version defined in the model
-  #   via FileColumn::Magick's version feature.
-  # * &lt;tt&gt;:version =&gt; geometry_string&lt;/tt&gt; will dynamically create an
+  # * a &lt;tt&gt;:symbol&lt;/tt&gt; will select a version defined in the model
+  #   via FileColumn::Magick's &lt;tt&gt;:versions&lt;/tt&gt; feature.
+  # * a &lt;tt&gt;geometry_string&lt;/tt&gt; will dynamically create an
   #   image resized as specified by &lt;tt&gt;geometry_string&lt;/tt&gt;. The image will
   #   be stored so that it does not have to be recomputed the next time the
   #   same version string is used.
-  # * &lt;tt&gt;:version =&gt; some_hash&lt;/tt&gt; will dynamically create an image
+  # * &lt;tt&gt;some_hash&lt;/tt&gt; will dynamically create an image
   #   that is created according to the options in &lt;tt&gt;some_hash&lt;/tt&gt;. This
   #   accepts exactly the same options as Magick's version feature.
   #
-  # Note that if you pass a string or a symbol as the +object+ parameter,
-  # the file_column will be looked up in instance variable named +object+.
-  def url_for_file_column(object, method, options=nil)
+  # The version produces by RMagick will be stored in a special sub-directories.
+  # The directories name will be derived from the options you specify but if want
+  # to set it yourself, you can use the &lt;tt&gt;:name =&gt; name&lt;/tt&gt; option.
+  #
+  # Examples:
+  #
+  #    &lt;%= url_for_image_column @entry, &quot;image&quot;, &quot;640x480&quot; %&gt;
+  #
+  # will produce an URL like this
+  #
+  #    /entry/image/42/bdn19n/filename.jpg
+  #    # &quot;640x480&quot;.hash.abs.to_s(36) == &quot;bdn19n&quot;
+  #
+  # and
+  #
+  #    &lt;%= url_for_image_column @entry, &quot;image&quot;, 
+  #       :size =&gt; &quot;50x50&quot;, :crop =&gt; &quot;1:1&quot;, :name =&gt; &quot;thumb&quot; %&gt;
+  #
+  # will produce something like this:
+  #
+  #    /entry/image/42/thumb/filename.jpg
+  #
+  # Hint: If you are using the same geometry string / options hash multiple times, you should
+  # define it in a helper to stay with DRY. Another option is to define it in the model via
+  # FileColumn::Magick's &lt;tt&gt;:versions&lt;/tt&gt; feature and then refer to it via a symbol.
+  #
+  # If there is currently no image uploaded, this method will return +nil+.
+  def url_for_image_column(object, method, options=nil)
     case object
     when String, Symbol
       object = instance_variable_get(&quot;@#{object.to_s}&quot;)
     end
     subdir = nil
-    if options and options[:version]
-      subdir = object.send(&quot;#{method}_state&quot;).create_magick_version_if_needed(options[:version])
+    if options
+      subdir = object.send(&quot;#{method}_state&quot;).create_magick_version_if_needed(options)
     end
-    relative_path = object.send(&quot;#{method}_relative_path&quot;, subdir)
-    return nil unless relative_path
-    url = &quot;&quot;
-    url &lt;&lt; @request.relative_url_root.to_s &lt;&lt; &quot;/&quot;
-    url &lt;&lt; object.send(&quot;#{method}_options&quot;)[:base_url] &lt;&lt; &quot;/&quot;
-    url &lt;&lt; relative_path
+    url_for_file_column(object, method, subdir)
   end
 end</diff>
      <filename>lib/file_column_helper.rb</filename>
    </modified>
    <modified>
      <diff>@@ -19,7 +19,7 @@ module FileColumn # :nodoc:
             resize_image(img, version_options, absolute_path(dirname))
           end
         end
-        if options[:magick][:geometry] or options[:magick][:crop]
+        if options[:magick][:size] or options[:magick][:crop]
           resize_image(img, options[:magick], absolute_path)
         end
 
@@ -28,6 +28,16 @@ module FileColumn # :nodoc:
     end
 
     def create_magick_version_if_needed(version)
+      # RMagick might not have been loaded so far.
+      # We do not want to require it on every call of this method
+      # as this might be fairly expensive, so we just try if ::Magick
+      # exists and require it if not.
+      begin 
+        ::Magick 
+      rescue NameError
+        require 'RMagick'
+      end
+
       if version.is_a?(Symbol)
         version_options = options[:magick][:versions][version]
       else
@@ -54,7 +64,7 @@ module FileColumn # :nodoc:
     
     def needs_resize?
       options[:magick] and just_uploaded? and 
-        (options[:magick][:geometry] or options[:magick][:versions])
+        (options[:magick][:size] or options[:magick][:versions])
     end
     
     def resize_image(img, img_options, dest_path)
@@ -66,8 +76,8 @@ module FileColumn # :nodoc:
                          [img.rows, h].min)
         end
 
-        if img_options[:geometry]
-          img = img.change_geometry(img_options[:geometry]) do |c, r, i|
+        if img_options[:size]
+          img = img.change_geometry(img_options[:size]) do |c, r, i|
             i.resize(c, r)
           end
         end
@@ -88,23 +98,23 @@ module FileColumn # :nodoc:
   # after a new file is assigned to the file_column attribute.
   #
   # To resize the uploaded image according to an imagemagick geometry
-  # string, just use the &lt;tt&gt;:geometry&lt;/tt&gt; option:
+  # string, just use the &lt;tt&gt;:size&lt;/tt&gt; option:
   #
-  #    file_column :image, :magick =&gt; {:geometry =&gt; &quot;800x600&gt;&quot;}
+  #    file_column :image, :magick =&gt; {:size =&gt; &quot;800x600&gt;&quot;}
   #
   # You can also create additional versions of your image, for example
   # thumb-nails, like this:
   #    file_column :image, :magick =&gt; {:versions =&gt; {
-  #         &quot;thumb&quot; =&gt; {:geometry =&gt; &quot;50x50&quot;},
-  #         &quot;medium&quot; =&gt; {:geometry =&gt; &quot;640x480&gt;&quot;}
+  #         &quot;thumb&quot; =&gt; {:size =&gt; &quot;50x50&quot;},
+  #         &quot;medium&quot; =&gt; {:size =&gt; &quot;640x480&gt;&quot;}
   #       }
   #
   # If you wish to crop your images with a size ratio before scaling
   # them according to your version geometry, you can use the :crop directive.
   #    file_column :image, :magick =&gt; {:versions =&gt; {
-  #         &quot;square&quot; =&gt; {:crop =&gt; &quot;1:1&quot;, :geometry =&gt; &quot;50x50&quot;},
-  #         &quot;screen&quot; =&gt; {:crop =&gt; &quot;4:3&quot;, :geometry =&gt; &quot;640x480&gt;&quot;},
-  #         &quot;widescreen&quot; =&gt; {:crop =&gt; &quot;16:9&quot;, :geometry =&gt; &quot;640x360!&quot;},
+  #         &quot;square&quot; =&gt; {:crop =&gt; &quot;1:1&quot;, :size =&gt; &quot;50x50&quot;},
+  #         &quot;screen&quot; =&gt; {:crop =&gt; &quot;4:3&quot;, :size =&gt; &quot;640x480&gt;&quot;},
+  #         &quot;widescreen&quot; =&gt; {:crop =&gt; &quot;16:9&quot;, :size =&gt; &quot;640x360!&quot;},
   #       }
   #    }
   #
@@ -121,6 +131,7 @@ module FileColumn # :nodoc:
 
     def self.file_column(klass, attr, options) # :nodoc:
       require 'RMagick'
+      options[:magick] = process_options(options[:magick],false) if options[:magick]
       if options[:magick][:versions]
         options[:magick][:versions].each_pair do |name, value|
           options[:magick][:versions][name] = process_options(value)
@@ -147,16 +158,20 @@ module FileColumn # :nodoc:
     end
 
     
-    def self.process_options(options)
-      options = {:geometry =&gt; options } if options.kind_of?(String)
-      unless options[:name]
+    def self.process_options(options,create_name=true)
+      options = {:size =&gt; options } if options.kind_of?(String)
+      if options[:geometry]
+        options[:size] = options.delete(:geometry)
+      end
+      if options[:name].nil? and create_name
         hash = 0
-        for key in [:geometry, :crop]
+        for key in [:size, :crop]
           hash = hash ^ options[key].hash if options[key]
         end
         options[:name] = hash.abs.to_s(36)
       end
       options
     end
+
   end
 end</diff>
      <filename>lib/magick_file_column.rb</filename>
    </modified>
    <modified>
      <diff>@@ -22,6 +22,12 @@ class ActiveRecord::Base
 end
 
 
+class RequestMock
+  def relative_url_root
+    &quot;&quot;
+  end
+end
+
 class Test::Unit::TestCase
   private
   </diff>
      <filename>test/abstract_unit.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,12 +1,6 @@
 require 'abstract_unit'
 require 'fixtures/entry'
 
-class RequestMock
-  def relative_url_root
-    &quot;&quot;
-  end
-end
-
 class UrlForFileColumnTest &lt; Test::Unit::TestCase
   include FileColumnHelper
 
@@ -50,3 +44,4 @@ class UrlForFileColumnTest &lt; Test::Unit::TestCase
     assert_nil url_for_file_column(e, &quot;image&quot;)
   end
 end
+</diff>
      <filename>test/file_column_helper_test.rb</filename>
    </modified>
    <modified>
      <diff>@@ -169,3 +169,46 @@ class RMagickCroppingTest &lt; AbstractRMagickTest
   end
     
 end
+
+class UrlForImageColumnTest &lt; AbstractRMagickTest
+  include FileColumnHelper
+
+  def setup
+    Entry.file_column :image, :magick =&gt; {
+      :versions =&gt; {:thumb =&gt; {:size =&gt; &quot;50x50&quot;, :name =&gt; &quot;thumb&quot; } }
+    }
+    @request = RequestMock.new
+  end
+    
+  def test_should_use_version_on_symbol_option
+    e = Entry.new(:image =&gt; upload(&quot;skanthak.png&quot;))
+    
+    url = url_for_image_column(e, &quot;image&quot;, :thumb)
+    assert_match %r{^/entry/image/tmp/.+/thumb/skanthak.png$}, url
+  end
+
+  def test_should_use_string_as_size
+    e = Entry.new(:image =&gt; upload(&quot;skanthak.png&quot;))
+
+    url = url_for_image_column(e, &quot;image&quot;, &quot;50x50&quot;)
+    
+    assert_match %r{^/entry/image/tmp/.+/.+/skanthak.png$}, url
+    
+    url =~ /\/([^\/]+)\/skanthak.png$/
+    dirname = $1
+
+    assert_max_image_size read_image(e.image(dirname)), 50
+  end
+
+  def test_should_accept_version_hash
+    e = Entry.new(:image =&gt; upload(&quot;skanthak.png&quot;))
+
+    url = url_for_image_column(e, &quot;image&quot;, :size =&gt; &quot;50x50&quot;, :crop =&gt; &quot;1:1&quot;, :name =&gt; &quot;small&quot;)
+
+    assert_match %r{^/entry/image/tmp/.+/small/skanthak.png$}, url
+
+    img = read_image(e.image(&quot;small&quot;))
+    assert_equal 50, img.rows
+    assert_equal 50, img.columns
+  end
+end</diff>
      <filename>test/magick_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>5cd26e12b1cac28e311e23ae512e23a8bb116208</id>
    </parent>
  </parents>
  <author>
    <name>skanthak</name>
    <email>skanthak@6a5db2c9-b104-0410-84e5-917aad89f9d9</email>
  </author>
  <url>http://github.com/kch/file_column/commit/d74ae32de6f5ab22036eb03d3198caf248bf6ecb</url>
  <id>d74ae32de6f5ab22036eb03d3198caf248bf6ecb</id>
  <committed-date>2005-11-23T13:23:31-08:00</committed-date>
  <authored-date>2005-11-23T13:23:31-08:00</authored-date>
  <message>moved dynamic view resizing into url_for_image_column helper


git-svn-id: http://opensvn.csie.org/rails_file_column/plugins/file_column/trunk@37 6a5db2c9-b104-0410-84e5-917aad89f9d9</message>
  <tree>dc0c2a5dc7ddfd9174e218959223d9b7d9d8f0ec</tree>
  <committer>
    <name>skanthak</name>
    <email>skanthak@6a5db2c9-b104-0410-84e5-917aad89f9d9</email>
  </committer>
</commit>
