Permalink
Browse files

Better handling of custom viewport attributes.

  • Loading branch information...
1 parent 4e31d51 commit dc4497b0bbea2aca74330439c7728fc972c75fed Anton Lindqvist committed Aug 30, 2010
Showing with 15 additions and 6 deletions.
  1. +4 −0 README.md
  2. +8 −3 lib/iphone_helpers.rb
  3. +3 −3 test/iphone_helpers_test.rb
View
@@ -10,6 +10,10 @@ Examples
<%= iphone :viewport %>
+### viewport with custom attributes
+
+ <%= iphone :viewport, :width => 320, :height => 480, :initial_scale => 2.5, :minimum_scale => 0.5, :maximum_scale => 3, :user_scalable => 'no' %>
+
### apple touch icon
<%= iphone :icon, :href => "iphone.png" %>
View
@@ -1,7 +1,7 @@
module ::ActionView::Helpers::TagHelper
def iphone(key, *options)
options = options.extract_options!
- tagname, attributes = send("iphone_#{key}", options)
+ tagname, attributes = send("iphone_#{key}", options || {})
tag(tagname, attributes)
end
@@ -36,8 +36,13 @@ def iphone_status_bar(options)
end
def iphone_viewport(options)
- defaults = {'initial-scale' => 1.0, 'maximum-scale' => 1.0, 'width' => 'device-width'}.to_options!
- content = defaults.merge(options).collect { |k,v| "#{k}=#{v}" }.join('; ')
+ translate = {:initial_scale => :'initial-scale', :maximum_scale => :'maximum-scale', :minimum_scale => :'minimum-scale', :user_scalable => :'user-scalable'}
+ defaults = {:'initial-scale' => 1, :'maximum-scale' => 1, :width => 'device-width'}
+ content = options.each do |k,v|
+ k = (translate.key?(k) && options.delete(k) && translate[k]) || k
+ options[k] = v
+ end
+ content = defaults.merge(content).collect { |k,v| "#{k}=#{v}" }.join('; ')
options = {:content => content, :name => 'viewport'}
[:meta, options]
end
@@ -32,10 +32,10 @@ class IphoneHelpersTest < ActionView::TestCase
end
test 'viewport' do
- assert_equal iphone(:viewport), '<meta content="initial-scale=1.0; width=device-width; maximum-scale=1.0" name="viewport" />'
+ assert_equal iphone(:viewport), '<meta content="width=device-width; initial-scale=1; maximum-scale=1" name="viewport" />'
end
- test 'viewport with custom width and height' do
- assert_equal iphone(:viewport, :width => 320, :height => 480), '<meta content="initial-scale=1.0; width=320; height=480; maximum-scale=1.0" name="viewport" />'
+ test 'viewport with custom attributes' do
+ assert_equal iphone(:viewport, :width => 320, :height => 480, :initial_scale => 2.5, :minimum_scale => 0.5, :maximum_scale => 3, :user_scalable => 'no'), '<meta content="width=320; height=480; initial-scale=2.5; maximum-scale=3; minimum-scale=0.5; user-scalable=no" name="viewport" />'
end
end

0 comments on commit dc4497b

Please sign in to comment.