Skip to content

Instantly share code, notes, and snippets.

@dscataglini
Created July 31, 2009 18:15
Show Gist options
  • Save dscataglini/159353 to your computer and use it in GitHub Desktop.
Save dscataglini/159353 to your computer and use it in GitHub Desktop.
The final ImageTagOptionParser class also take account of other logic that lived in a separate controller. Thus I now have one single place that knows all the rules for the options. Plus I now have an easy way to write a test for it. (this was done TDD bt
transformations = ImageTagOptionParser.new(options)
final_filepath = "#{prefix}/resources/images/#{file}?#{transformations}"
class ImageTagOptionParser
KEYS = %w{scale constrain crop cropresize}
attr_accessor :transform
def initialize(options = {})
options.reject!{|key, value| (key == "scale" && value.to_s == "100")}
first_valid_key = KEYS.find{|key| !options[key].blank?}
transform = options.find{|key, val| key == first_valid_key }
end
def to_s
transform.join("=") unless blank?
end
def blank?
transform.blank?
end
end
scale = options['scale'] ? options.delete('scale') : '100' # 100%
crop = options['crop'] or nil
cropresize = options['cropresize'] or nil
if options['constrain']
dimensions = "&constrain=#{options.delete('constrain')}"
elsif crop
dimensions = "&crop=#{crop}"
elsif cropresize
dimensions = "&cropresize=#{cropresize}"
else
dimensions = ''
end
%{<img src="#{prefix}/resources/images/#{file}?scale=#{scale}#{dimensions}"#{attributes} />}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment