Permalink
Browse files

Some work on the way to making this a gem

  • Loading branch information...
1 parent 93d875d commit 062ef974242e5dae1a431b9fcce0cc4e49098e98 @benlangfeld benlangfeld committed Apr 27, 2011
View
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in mobile-fu.gemspec
+gemspec
View
@@ -0,0 +1,89 @@
+PATH
+ remote: .
+ specs:
+ mobile-fu (0.0.1)
+ rails
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ abstract (1.0.0)
+ actionmailer (3.0.6)
+ actionpack (= 3.0.6)
+ mail (~> 2.2.15)
+ actionpack (3.0.6)
+ activemodel (= 3.0.6)
+ activesupport (= 3.0.6)
+ builder (~> 2.1.2)
+ erubis (~> 2.6.6)
+ i18n (~> 0.5.0)
+ rack (~> 1.2.1)
+ rack-mount (~> 0.6.14)
+ rack-test (~> 0.5.7)
+ tzinfo (~> 0.3.23)
+ activemodel (3.0.6)
+ activesupport (= 3.0.6)
+ builder (~> 2.1.2)
+ i18n (~> 0.5.0)
+ activerecord (3.0.6)
+ activemodel (= 3.0.6)
+ activesupport (= 3.0.6)
+ arel (~> 2.0.2)
+ tzinfo (~> 0.3.23)
+ activeresource (3.0.6)
+ activemodel (= 3.0.6)
+ activesupport (= 3.0.6)
+ activesupport (3.0.6)
+ arel (2.0.9)
+ builder (2.1.2)
+ diff-lcs (1.1.2)
+ erubis (2.6.6)
+ abstract (>= 1.0.0)
+ i18n (0.5.0)
+ mail (2.2.15)
+ activesupport (>= 2.3.6)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.16)
+ polyglot (0.3.1)
+ rack (1.2.2)
+ rack-mount (0.6.14)
+ rack (>= 1.0.0)
+ rack-test (0.5.7)
+ rack (>= 1.0)
+ rails (3.0.6)
+ actionmailer (= 3.0.6)
+ actionpack (= 3.0.6)
+ activerecord (= 3.0.6)
+ activeresource (= 3.0.6)
+ activesupport (= 3.0.6)
+ bundler (~> 1.0)
+ railties (= 3.0.6)
+ railties (3.0.6)
+ actionpack (= 3.0.6)
+ activesupport (= 3.0.6)
+ rake (>= 0.8.7)
+ thor (~> 0.14.4)
+ rake (0.8.7)
+ rdoc (3.5.3)
+ rspec (2.5.0)
+ rspec-core (~> 2.5.0)
+ rspec-expectations (~> 2.5.0)
+ rspec-mocks (~> 2.5.0)
+ rspec-core (2.5.1)
+ rspec-expectations (2.5.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.5.0)
+ thor (0.14.6)
+ treetop (1.4.9)
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.26)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ mobile-fu!
+ rdoc
+ rspec
View
@@ -1,22 +1,16 @@
require 'rake'
-require 'rake/testtask'
-require 'rake/rdoctask'
+require 'bundler'
+Bundler::GemHelper.install_tasks
-desc 'Default: run unit tests.'
-task :default => :test
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new('spec')
+task :default => :spec
-desc 'Test the sms_fu plugin.'
-Rake::TestTask.new(:test) do |t|
- t.libs << 'lib'
- t.pattern = 'test/**/*_test.rb'
- t.verbose = true
-end
-
-desc 'Generate documentation for the sms_fu plugin.'
-Rake::RDocTask.new(:rdoc) do |rdoc|
- rdoc.rdoc_dir = 'rdoc'
- rdoc.title = 'SmsFu'
- rdoc.options << '--line-numbers' << '--inline-source'
- rdoc.rdoc_files.include('README')
- rdoc.rdoc_files.include('lib/**/*.rb')
-end
+# desc 'Generate documentation for the mobile_fu plugin.'
+# Rake::RDocTask.new(:rdoc) do |rdoc|
+# rdoc.rdoc_dir = 'rdoc'
+# rdoc.title = 'MobileFu'
+# rdoc.options << '--line-numbers' << '--inline-source'
+# rdoc.rdoc_files.include('README')
+# rdoc.rdoc_files.include('lib/**/*.rb')
+# end
View
@@ -1,7 +0,0 @@
-require File.dirname(__FILE__) + '/lib/mobile_fu_helper.rb'
-require File.dirname(__FILE__) + '/lib/mobilized_styles'
-require File.dirname(__FILE__) + '/lib/mobile_fu'
-
-ActionView::Base.send(:include, MobileFuHelper)
-ActionView::Base.send(:include, MobilizedStyles)
-ActionView::Base.send(:alias_method_chain, :stylesheet_link_tag, :mobilization)
View
No changes.
@@ -1,3 +1,10 @@
+require 'rails'
+
+module MobileFu
+ autoload :Helper, 'mobile-fu/helper'
+ autoload :MobilizedStyles, 'mobile-fu/mobilized_styles'
+end
+
module ActionController
module MobileFu
# These are various strings that can be found in mobile devices. Please feel free
@@ -8,39 +15,35 @@ module MobileFu
'pdxgw|netfront|xiino|vodafone|portalmmm|sagem|mot-|sie-|ipod|up\\.b|' +
'webos|amoi|novarra|cdm|alcatel|pocket|ipad|iphone|mobileexplorer|' +
'mobile'
-
+
def self.included(base)
- base.extend(ClassMethods)
+ base.extend ClassMethods
end
-
+
module ClassMethods
-
- # Add this to one of your controllers to use MobileFu.
+
+ # Add this to one of your controllers to use MobileFu.
#
- # class ApplicationController < ActionController::Base
+ # class ApplicationController < ActionController::Base
# has_mobile_fu
# end
#
# You can also force mobile mode by passing in 'true'
#
- # class ApplicationController < ActionController::Base
- # has_mobile_fu(true)
+ # class ApplicationController < ActionController::Base
+ # has_mobile_fu true
# end
-
+
def has_mobile_fu(test_mode = false)
include ActionController::MobileFu::InstanceMethods
- if test_mode
- before_filter :force_mobile_format
- else
- before_filter :set_mobile_format
- end
+ before_filter test_mode ? :force_mobile_format : :set_mobile_format
helper_method :is_mobile_device?
helper_method :in_mobile_view?
helper_method :is_device?
end
-
+
def is_mobile_device?
@@is_mobile_device
end
@@ -53,52 +56,52 @@ def is_device?(type)
@@is_device
end
end
-
+
module InstanceMethods
-
+
# Forces the request format to be :mobile
-
+
def force_mobile_format
- if !request.xhr?
+ unless request.xhr?
request.format = :mobile
session[:mobile_view] = true if session[:mobile_view].nil?
end
end
-
+
# Determines the request format based on whether the device is mobile or if
# the user has opted to use either the 'Standard' view or 'Mobile' view.
-
+
def set_mobile_format
if is_mobile_device? && !request.xhr?
request.format = session[:mobile_view] == false ? :html : :mobile
session[:mobile_view] = true if session[:mobile_view].nil?
end
end
-
+
# Returns either true or false depending on whether or not the format of the
# request is either :mobile or not.
-
+
def in_mobile_view?
request.format.to_sym == :mobile
end
-
+
# Returns either true or false depending on whether or not the user agent of
# the device making the request is matched to a device in our regex.
-
+
def is_mobile_device?
request.user_agent.to_s.downcase =~ Regexp.new(ActionController::MobileFu::MOBILE_USER_AGENTS)
end
# Can check for a specific user agent
# e.g., is_device?('iphone') or is_device?('mobileexplorer')
-
+
def is_device?(type)
- request.user_agent.to_s.downcase.include?(type.to_s.downcase)
+ request.user_agent.to_s.downcase.include? type.to_s.downcase
end
end
-
+
end
-
+
end
-ActionController::Base.send(:include, ActionController::MobileFu)
+ActionController::Base.send :include, ActionController::MobileFu
@@ -0,0 +1,26 @@
+module MobileFu
+ module Helper
+ ACCEPTABLE_TYPES = [:mobile, :basic]
+
+ def mobile_xhtml_doctype(type = :mobile, version = '1.0')
+ raise Exception.new("MobileFu: XHTML DOCTYPE type must either be ':mobile' or ':basic'") unless ACCEPTABLE_TYPES.include?(type)
+ raise Exception.new("MobileFu: XHTML DOCTYPE version must be in the format of '1.0' or '1.1', etc.") unless version.include?('.')
+
+ doc_type = "<?xml version=\"1.0\" charset=\"UTF-8\" ?>\n"
+ doc_type += "<!DOCTYPE html PUBLIC "
+ doc_type += case type
+ when :mobile
+ "\"-//WAPFORUM//DTD XHTML Mobile #{version}//EN\" \"http://www.openmobilealliance.org/tech/DTD/xhtml-mobile#{version.gsub('.','')}.dtd\">"
+ when :basic
+ "\"-//W3C//DTD XHTML Basic #{version}//EN\" \"http://www.w3.org/TR/xhtml-basic/xhtml-basic#{version.gsub('.','')}.dtd\">"
+ end
+ doc_type
+ end
+
+ def js_enabled_mobile_device?
+ is_device?('iphone') || is_device?('ipod') || is_device?('ipad') || is_device?('mobileexplorer') || is_device?('android')
+ end
+ end
+end
+
+ActionView::Base.send :include, MobileFu::Helper
@@ -0,0 +1,75 @@
+# 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
+ 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
+ end
+
+ stylesheet_link_tag_without_mobilization *mobilized_sources
+ end
+ end
+end
+
+ActionView::Base.send :include, MobileFu::MobilizedStyles
+ActionView::Base.send :alias_method_chain, :stylesheet_link_tag, :mobilization
@@ -0,0 +1,3 @@
+module MobileFu
+ VERSION = "0.0.1"
+end
Oops, something went wrong.

0 comments on commit 062ef97

Please sign in to comment.