Permalink
Browse files

Simplify MobilizedStyles significantly

  • Loading branch information...
1 parent 6fd9698 commit 5ac4c6e5719cf4be8e77345fa52b15b68054e557 @benlangfeld committed May 13, 2011
Showing with 17 additions and 74 deletions.
  1. +5 −54 lib/mobile-fu/mobilized_styles.rb
  2. +12 −20 spec/mobilized_styles_spec.rb
@@ -1,72 +1,23 @@
-# Copyright (c) 2008 Brendan G. Lim (brendan@intridea.com)
-#
-# Permission is hereby granted, free of charge, to any person obtaining
-# a copy of this software and associated documentation files (the
-# "Software"), to deal in the Software without restriction, including
-# without limitation the rights to use, copy, modify, merge, publish,
-# distribute, sublicense, and/or sell copies of the Software, and to
-# permit persons to whom the Software is furnished to do so, subject to
-# the following conditions:
-#
-# The above copyright notice and this permission notice shall be
-# included in all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
module MobileFu
module MobilizedStyles
# This logic was taken from Michael Bleigh's browserized styles
# with modification to work for mobile browsers.
- def user_agent_device_name
- @user_agent_device_name ||= begin
-
- ua = request.user_agent
- return nil if ua.nil?
- ua.downcase!
-
- if ua.index('mobileexplorer') || ua.index('windows ce')
- 'mobileexplorer'
- elsif ua.index('blackberry')
- 'blackberry'
- elsif ua.index('iphone') || ua.index('ipod')
- 'iphone'
- elsif ua.index('ipad')
- 'ipad'
- elsif ua.index('android')
- 'android'
- elsif ua.index('nokia')
- 'nokia'
- elsif ua.index('palm')
- 'palm'
- end
- end
+ def device_name
+ @device_name ||= headers['X_MOBILE_DEVICE']
end
def stylesheet_link_tag_with_mobilization(*sources)
mobilized_sources = Array.new
sources.each do |source|
- subbed_source = source.to_s.gsub ".css",""
-
- possible_sources = ["#{subbed_source.to_s}_#{user_agent_device_name}"]
-
mobilized_sources << source
- for possible_source in possible_sources
- path = File.join config.stylesheets_dir, "#{possible_source}.css"
- sass_path = File.join config.stylesheets_dir, "sass", "#{possible_source}.sass"
- mobilized_sources << possible_source if File.exist?(path) || File.exist?(sass_path)
- end
+ path = File.join config.stylesheets_dir, "#{source.to_s.gsub '.css', ''}_#{device_name}.css"
+ mobilized_sources << possible_source if File.exist?(path)
end
- stylesheet_link_tag_without_mobilization *mobilized_sources
+ stylesheet_link_tag *mobilized_sources
end
end
end
@@ -1,38 +1,30 @@
require 'spec_helper'
-module ActionView
- module Helpers
- module AssetTagHelper
- STYLESHEETS_DIR = "stylesheets"
- end
- end
-end
-
describe MobileFu::MobilizedStyles do
before(:each) do
- @view = mock(:ActionView)
- @request = mock(:ActionRequest)
- @view.extend(MobileFu::MobilizedStyles)
- @view.stub!(:request).and_return(@request)
- @request.stub!(:user_agent)
+ @view = mock :ActionView
+ @request = mock :ActionRequest
+ @view.extend MobileFu::MobilizedStyles
+ @view.stub!(:request).and_return @request
+ @request.stub! :user_agent
end
def ua(str)
- @request.stub!(:user_agent).and_return(str)
+ @request.stub!(:user_agent).and_return str
end
it "will include a mobilized css file if it recognizes a string in the user agent" do
ua "iphone"
- File.should_receive(:exist?).with("stylesheets/style_iphone.css").and_return(true)
- @view.should_receive(:stylesheet_link_tag_without_mobilization).with("style", "style_iphone")
- @view.stylesheet_link_tag_with_mobilization("style")
+ File.should_receive(:exist?).with("stylesheets/style_iphone.css").and_return true
+ @view.should_receive(:stylesheet_link_tag).with "style", "style_iphone"
+ @view.stylesheet_link_tag_with_mobilization "style"
end
it "includes mobiziled css files whether or not the original call to stylesheet_link_tag used a file extension" do
ua "blackberry"
- File.should_receive(:exist?).with("stylesheets/style_blackberry.css").and_return(true)
- @view.should_receive(:stylesheet_link_tag_without_mobilization).with("style.css", "style_blackberry")
- @view.stylesheet_link_tag_with_mobilization("style.css")
+ File.should_receive(:exist?).with("stylesheets/style_blackberry.css").and_return true
+ @view.should_receive(:stylesheet_link_tag).with "style.css", "style_blackberry"
+ @view.stylesheet_link_tag_with_mobilization "style.css"
end
end

0 comments on commit 5ac4c6e

Please sign in to comment.