Created
July 31, 2009 18:15
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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