mobile-fu 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/CHANGELOG +113 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +99 -0
- data/Rakefile +16 -0
- data/lib/mobile-fu.rb +112 -0
- data/lib/mobile-fu/helper.rb +26 -0
- data/lib/mobile-fu/mobilized_styles.rb +75 -0
- data/lib/mobile-fu/version.rb +3 -0
- data/mobile-fu.gemspec +26 -0
- data/spec/mobilized_styles_spec.rb +38 -0
- data/spec/spec_helper.rb +1 -0
- metadata +114 -0
data/.gitignore
ADDED
data/CHANGELOG
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
commit a46e0d5ff0a95407bbd8f3437a372ced53d90c9b
|
|
2
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
3
|
+
Date: Sat Dec 20 20:59:49 2008 -0500
|
|
4
|
+
|
|
5
|
+
Added a helper method to determine if a device is JavaScript enabled and one to output the XHTML Mobile 1.0 doctype
|
|
6
|
+
|
|
7
|
+
commit f4b29b2e2c68a79b8904dddb7a91efd4341540ee
|
|
8
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
9
|
+
Date: Sun Sep 21 21:39:10 2008 -0400
|
|
10
|
+
|
|
11
|
+
Fixes issue with 'in_mobile_view' instead of 'is_mobile_view' helper method
|
|
12
|
+
|
|
13
|
+
commit 29e8e8b407dd3dc8d5f3248183223d11c6a78843
|
|
14
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
15
|
+
Date: Wed Sep 10 12:51:12 2008 -0400
|
|
16
|
+
|
|
17
|
+
Updating CHANGELOG
|
|
18
|
+
|
|
19
|
+
commit 0aac5bd7f0d2f71e6af7b144a04a18dad9f5aca5
|
|
20
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
21
|
+
Date: Wed Sep 10 12:50:50 2008 -0400
|
|
22
|
+
|
|
23
|
+
Styling overrides for iPods
|
|
24
|
+
|
|
25
|
+
commit f30c97c794ba74e737856aa9226328469c8395b6
|
|
26
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
27
|
+
Date: Mon Sep 8 00:07:27 2008 -0400
|
|
28
|
+
|
|
29
|
+
Adding in some comments to the latest changes
|
|
30
|
+
|
|
31
|
+
commit 78dae861e46d383356d7e4d6dfdaefbf60072e27
|
|
32
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
33
|
+
Date: Mon Sep 8 00:06:16 2008 -0400
|
|
34
|
+
|
|
35
|
+
Updating README
|
|
36
|
+
|
|
37
|
+
commit 1a66ccd2142e387f6d3138acb688a58df4687fcd
|
|
38
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
39
|
+
Date: Mon Sep 8 00:05:48 2008 -0400
|
|
40
|
+
|
|
41
|
+
Adding in a helper method to check for a specific mobile device and adding in 'ipod' to the list of mobile user agents
|
|
42
|
+
|
|
43
|
+
commit cbb26fff3287e16a757ee2aed2dcc8e98cbfb369
|
|
44
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
45
|
+
Date: Fri Aug 22 22:16:19 2008 -0400
|
|
46
|
+
|
|
47
|
+
Updating the CHANGELOG
|
|
48
|
+
|
|
49
|
+
commit ee49945b593b1c55bf546da1d595a0941d071f94
|
|
50
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
51
|
+
Date: Fri Aug 22 22:16:00 2008 -0400
|
|
52
|
+
|
|
53
|
+
Updating README
|
|
54
|
+
|
|
55
|
+
commit 02c12a8563466f7cef6aba8e53915dc2efec908c
|
|
56
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
57
|
+
Date: Mon Aug 18 16:08:51 2008 -0400
|
|
58
|
+
|
|
59
|
+
Adding in support for Android
|
|
60
|
+
|
|
61
|
+
commit 2dffa5958905c0419288d03a0650127a0734b41a
|
|
62
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
63
|
+
Date: Sat Jul 26 14:08:56 2008 -0400
|
|
64
|
+
|
|
65
|
+
Adding in other phone user agents, including some Japanese and French manufacturers
|
|
66
|
+
|
|
67
|
+
commit e8f9f1a9c2e668d639690f4f1beb4f701f742cfa
|
|
68
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
69
|
+
Date: Fri Jul 18 11:15:52 2008 -0400
|
|
70
|
+
|
|
71
|
+
Adding in CHANGELOG
|
|
72
|
+
|
|
73
|
+
commit a3122b40c0c722b2942e9cb5341614d0e0d834a5
|
|
74
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
75
|
+
Date: Fri Jul 18 11:14:35 2008 -0400
|
|
76
|
+
|
|
77
|
+
Changes to README
|
|
78
|
+
|
|
79
|
+
commit 64a24e36b6a770757963eb2d961262fb8a14ec1b
|
|
80
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
81
|
+
Date: Fri Jul 18 11:10:40 2008 -0400
|
|
82
|
+
|
|
83
|
+
Quick fix
|
|
84
|
+
|
|
85
|
+
commit 116fa5b7c1168471cf56cb9961b98f0b73fc3308
|
|
86
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
87
|
+
Date: Fri Jul 18 10:04:41 2008 -0400
|
|
88
|
+
|
|
89
|
+
Fixed issue with mobile view session variable getting set incorrectly, when forcing mobile format
|
|
90
|
+
|
|
91
|
+
commit d172665c9d0458801fd672076e269a3c0ee9d80d
|
|
92
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
93
|
+
Date: Fri Jul 18 01:04:19 2008 -0400
|
|
94
|
+
|
|
95
|
+
Adding in more MIT license goodness
|
|
96
|
+
|
|
97
|
+
commit a6c947d684d2d9abdbe354dc1ea757f23cd5a633
|
|
98
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
99
|
+
Date: Fri Jul 18 01:00:55 2008 -0400
|
|
100
|
+
|
|
101
|
+
Change to the README
|
|
102
|
+
|
|
103
|
+
commit dad4f4db1f3cb816283069a91c01982c953ff8a5
|
|
104
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
105
|
+
Date: Fri Jul 18 01:00:16 2008 -0400
|
|
106
|
+
|
|
107
|
+
Change to the README
|
|
108
|
+
|
|
109
|
+
commit c5ec9de6ef19e48a66917d8f6f3a59771295765b
|
|
110
|
+
Author: Brendan G. Lim <brendan@intridea.com>
|
|
111
|
+
Date: Fri Jul 18 00:59:36 2008 -0400
|
|
112
|
+
|
|
113
|
+
Releasing Mobile Fu to the world
|
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2008 Brendan G. Lim, Intridea, Inc.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
= Mobile Fu
|
|
2
|
+
|
|
3
|
+
Want to automatically detect mobile devices that access your Rails application?
|
|
4
|
+
Mobile Fu allows you to do just that. People can access your site from a Palm,
|
|
5
|
+
Blackberry, iPhone, iPad, Nokia, etc. and it will automatically adjust the format
|
|
6
|
+
of the request from :html to :mobile.
|
|
7
|
+
|
|
8
|
+
== Rails 3 Compatibility
|
|
9
|
+
|
|
10
|
+
The master branch of this plugin is Rails 3 compatible.
|
|
11
|
+
|
|
12
|
+
To use the Rails 3 compatible version of this plugin, please install it like so:
|
|
13
|
+
|
|
14
|
+
rails plugin install git://github.com/brendanlim/mobile-fu.git
|
|
15
|
+
|
|
16
|
+
== Rails 2 Compatibility
|
|
17
|
+
|
|
18
|
+
You will need to install this plugin from the 2.x branch.
|
|
19
|
+
|
|
20
|
+
To use the Rails 2 compatible version of this plugin, please install it like so:
|
|
21
|
+
|
|
22
|
+
script/plugin install git://github.com/brendanlim/mobile-fu.git -r 2.x
|
|
23
|
+
|
|
24
|
+
== Usage
|
|
25
|
+
|
|
26
|
+
Add this this one line to the controller.
|
|
27
|
+
|
|
28
|
+
class ApplicationController < ActionController::Base
|
|
29
|
+
has_mobile_fu
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
Once this is in place, any request that comes from a mobile device will be be
|
|
33
|
+
set as :mobile format. It is up to you to determine how you want to handle
|
|
34
|
+
these requests. It is also up to you to create the .mobile.erb versions of
|
|
35
|
+
your views that are to be requested.
|
|
36
|
+
|
|
37
|
+
Then add the line below to config/initializers/mime_types.rb
|
|
38
|
+
|
|
39
|
+
Mime::Type.register_alias "text/html", :mobile
|
|
40
|
+
|
|
41
|
+
I recommend that you setup a before_filter that will redirect to a specific page
|
|
42
|
+
depending on whether or not it is a mobile request. How can you check this?
|
|
43
|
+
|
|
44
|
+
is_mobile_device? # => Returns true or false depending on the device
|
|
45
|
+
|
|
46
|
+
You can also determine which format is currently set in by calling the following:
|
|
47
|
+
|
|
48
|
+
is_mobile_view? # => Returns true or false depending on current req. format
|
|
49
|
+
|
|
50
|
+
Also, if you want the ability to allow a user to switch between 'mobile' and
|
|
51
|
+
'standard' format (:html), you can just adjust the mobile_view session variable
|
|
52
|
+
in a custom controller action.
|
|
53
|
+
|
|
54
|
+
session[:mobile_view] # => Set to true if request format is :mobile and false
|
|
55
|
+
if set to :html
|
|
56
|
+
|
|
57
|
+
So, different devices need different styling. Don't worry, we've got this
|
|
58
|
+
baked in to Mobile Fu.
|
|
59
|
+
|
|
60
|
+
If you are including a css or sass file via stylesheet_link_tag, all you have
|
|
61
|
+
to do is add _device to the name of one of your files to override your styling
|
|
62
|
+
for a certain device. The stylesheet that is loaded is dependant on which device
|
|
63
|
+
is making the request.
|
|
64
|
+
|
|
65
|
+
e.g., Accessing a page from a Blackberry.
|
|
66
|
+
|
|
67
|
+
... stylesheet_link_tag 'mobile.css' ...
|
|
68
|
+
|
|
69
|
+
This loads mobile.css, and mobile_blackberry.css if the file exists.
|
|
70
|
+
|
|
71
|
+
Supported stylesheet override device extensions at the moment are:
|
|
72
|
+
|
|
73
|
+
blackberry
|
|
74
|
+
iphone (iphone,ipod)
|
|
75
|
+
ipad
|
|
76
|
+
android
|
|
77
|
+
mobileexplorer
|
|
78
|
+
nokia
|
|
79
|
+
palm
|
|
80
|
+
|
|
81
|
+
The stylesheet awesomeness was derived from Michael Bleigh's browserized styles:
|
|
82
|
+
http://www.intridea.com/2007/12/9/announcing-browserized-styles
|
|
83
|
+
|
|
84
|
+
Inspiration for Mobile Fu came from Noel Rappin's rails_iui:
|
|
85
|
+
http://blogs.pathf.com/agileajax/2008/05/rails-developme.html
|
|
86
|
+
|
|
87
|
+
Hopefully this should help you create some awesome mobile applications.
|
|
88
|
+
|
|
89
|
+
== Testing Mobile Interface
|
|
90
|
+
|
|
91
|
+
If you want to force the mobile interface for testing, you can either use a
|
|
92
|
+
mobile device emulator, or you can pass 'true' to has_mobile_fu.
|
|
93
|
+
|
|
94
|
+
class ApplicationController < ActionController::Base
|
|
95
|
+
has_mobile_fu(true)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
Copyright (c) 2008 Brendan G. Lim, Intridea, Inc., released under the MIT license
|
data/Rakefile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require 'rake'
|
|
2
|
+
require 'bundler'
|
|
3
|
+
Bundler::GemHelper.install_tasks
|
|
4
|
+
|
|
5
|
+
require 'rspec/core/rake_task'
|
|
6
|
+
RSpec::Core::RakeTask.new('spec')
|
|
7
|
+
task :default => :spec
|
|
8
|
+
|
|
9
|
+
# desc 'Generate documentation for the mobile_fu plugin.'
|
|
10
|
+
# Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
11
|
+
# rdoc.rdoc_dir = 'rdoc'
|
|
12
|
+
# rdoc.title = 'MobileFu'
|
|
13
|
+
# rdoc.options << '--line-numbers' << '--inline-source'
|
|
14
|
+
# rdoc.rdoc_files.include('README')
|
|
15
|
+
# rdoc.rdoc_files.include('lib/**/*.rb')
|
|
16
|
+
# end
|
data/lib/mobile-fu.rb
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
require 'rails'
|
|
2
|
+
require 'rack/mobile-detect'
|
|
3
|
+
|
|
4
|
+
module MobileFu
|
|
5
|
+
autoload :Helper, 'mobile-fu/helper'
|
|
6
|
+
autoload :MobilizedStyles, 'mobile-fu/mobilized_styles'
|
|
7
|
+
|
|
8
|
+
class Railtie < Rails::Railtie
|
|
9
|
+
initializer "mobile-fu.configure" do |app|
|
|
10
|
+
app.config.middleware.use Rack::MobileDetect
|
|
11
|
+
end
|
|
12
|
+
Mime::Type.register_alias "text/html", :mobile
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
module ActionController
|
|
17
|
+
module MobileFu
|
|
18
|
+
|
|
19
|
+
def self.included(base)
|
|
20
|
+
base.extend ClassMethods
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
module ClassMethods
|
|
24
|
+
|
|
25
|
+
# Add this to one of your controllers to use MobileFu.
|
|
26
|
+
#
|
|
27
|
+
# class ApplicationController < ActionController::Base
|
|
28
|
+
# has_mobile_fu
|
|
29
|
+
# end
|
|
30
|
+
#
|
|
31
|
+
# You can also force mobile mode by passing in 'true'
|
|
32
|
+
#
|
|
33
|
+
# class ApplicationController < ActionController::Base
|
|
34
|
+
# has_mobile_fu true
|
|
35
|
+
# end
|
|
36
|
+
|
|
37
|
+
def has_mobile_fu(test_mode = false)
|
|
38
|
+
include ActionController::MobileFu::InstanceMethods
|
|
39
|
+
|
|
40
|
+
before_filter test_mode ? :force_mobile_format : :set_mobile_format
|
|
41
|
+
|
|
42
|
+
helper_method :is_mobile_device?
|
|
43
|
+
helper_method :in_mobile_view?
|
|
44
|
+
helper_method :is_device?
|
|
45
|
+
helper_method :mobile_device
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def is_mobile_device?
|
|
49
|
+
@@is_mobile_device
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def in_mobile_view?
|
|
53
|
+
@@in_mobile_view
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def is_device?(type)
|
|
57
|
+
@@is_device
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
module InstanceMethods
|
|
62
|
+
|
|
63
|
+
# Forces the request format to be :mobile
|
|
64
|
+
|
|
65
|
+
def force_mobile_format
|
|
66
|
+
unless request.xhr?
|
|
67
|
+
request.format = :mobile
|
|
68
|
+
session[:mobile_view] = true if session[:mobile_view].nil?
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Determines the request format based on whether the device is mobile or if
|
|
73
|
+
# the user has opted to use either the 'Standard' view or 'Mobile' view.
|
|
74
|
+
|
|
75
|
+
def set_mobile_format
|
|
76
|
+
if is_mobile_device? && !request.xhr?
|
|
77
|
+
request.format = session[:mobile_view] == false ? :html : :mobile
|
|
78
|
+
session[:mobile_view] = true if session[:mobile_view].nil?
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Returns either true or false depending on whether or not the format of the
|
|
83
|
+
# request is either :mobile or not.
|
|
84
|
+
|
|
85
|
+
def in_mobile_view?
|
|
86
|
+
request.format.to_sym == :mobile
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Returns either true or false depending on whether or not the user agent of
|
|
90
|
+
# the device making the request is matched to a device in our regex.
|
|
91
|
+
|
|
92
|
+
def is_mobile_device?
|
|
93
|
+
!!mobile_device
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def mobile_device
|
|
97
|
+
request.headers['X_MOBILE_DEVICE']
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Can check for a specific user agent
|
|
101
|
+
# e.g., is_device?('iphone') or is_device?('mobileexplorer')
|
|
102
|
+
|
|
103
|
+
def is_device?(type)
|
|
104
|
+
request.user_agent.to_s.downcase.include? type.to_s.downcase
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
ActionController::Base.send :include, ActionController::MobileFu
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module MobileFu
|
|
2
|
+
module Helper
|
|
3
|
+
ACCEPTABLE_TYPES = [:mobile, :basic]
|
|
4
|
+
|
|
5
|
+
def mobile_xhtml_doctype(type = :mobile, version = '1.0')
|
|
6
|
+
raise Exception.new("MobileFu: XHTML DOCTYPE type must either be ':mobile' or ':basic'") unless ACCEPTABLE_TYPES.include?(type)
|
|
7
|
+
raise Exception.new("MobileFu: XHTML DOCTYPE version must be in the format of '1.0' or '1.1', etc.") unless version.include?('.')
|
|
8
|
+
|
|
9
|
+
doc_type = "<?xml version=\"1.0\" charset=\"UTF-8\" ?>\n"
|
|
10
|
+
doc_type += "<!DOCTYPE html PUBLIC "
|
|
11
|
+
doc_type += case type
|
|
12
|
+
when :mobile
|
|
13
|
+
"\"-//WAPFORUM//DTD XHTML Mobile #{version}//EN\" \"http://www.openmobilealliance.org/tech/DTD/xhtml-mobile#{version.gsub('.','')}.dtd\">"
|
|
14
|
+
when :basic
|
|
15
|
+
"\"-//W3C//DTD XHTML Basic #{version}//EN\" \"http://www.w3.org/TR/xhtml-basic/xhtml-basic#{version.gsub('.','')}.dtd\">"
|
|
16
|
+
end
|
|
17
|
+
doc_type
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def js_enabled_mobile_device?
|
|
21
|
+
is_device?('iphone') || is_device?('ipod') || is_device?('ipad') || is_device?('mobileexplorer') || is_device?('android')
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
ActionView::Base.send :include, MobileFu::Helper
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Copyright (c) 2008 Brendan G. Lim (brendan@intridea.com)
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
# a copy of this software and associated documentation files (the
|
|
5
|
+
# "Software"), to deal in the Software without restriction, including
|
|
6
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
# the following conditions:
|
|
10
|
+
#
|
|
11
|
+
# The above copyright notice and this permission notice shall be
|
|
12
|
+
# included in all copies or substantial portions of the Software.
|
|
13
|
+
#
|
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
21
|
+
|
|
22
|
+
module MobileFu
|
|
23
|
+
module MobilizedStyles
|
|
24
|
+
|
|
25
|
+
# This logic was taken from Michael Bleigh's browserized styles
|
|
26
|
+
# with modification to work for mobile browsers.
|
|
27
|
+
|
|
28
|
+
def user_agent_device_name
|
|
29
|
+
@user_agent_device_name ||= begin
|
|
30
|
+
|
|
31
|
+
ua = request.user_agent
|
|
32
|
+
return nil if ua.nil?
|
|
33
|
+
ua.downcase!
|
|
34
|
+
|
|
35
|
+
if ua.index('mobileexplorer') || ua.index('windows ce')
|
|
36
|
+
'mobileexplorer'
|
|
37
|
+
elsif ua.index('blackberry')
|
|
38
|
+
'blackberry'
|
|
39
|
+
elsif ua.index('iphone') || ua.index('ipod')
|
|
40
|
+
'iphone'
|
|
41
|
+
elsif ua.index('ipad')
|
|
42
|
+
'ipad'
|
|
43
|
+
elsif ua.index('android')
|
|
44
|
+
'android'
|
|
45
|
+
elsif ua.index('nokia')
|
|
46
|
+
'nokia'
|
|
47
|
+
elsif ua.index('palm')
|
|
48
|
+
'palm'
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def stylesheet_link_tag_with_mobilization(*sources)
|
|
54
|
+
mobilized_sources = Array.new
|
|
55
|
+
sources.each do |source|
|
|
56
|
+
subbed_source = source.to_s.gsub ".css",""
|
|
57
|
+
|
|
58
|
+
possible_sources = ["#{subbed_source.to_s}_#{user_agent_device_name}"]
|
|
59
|
+
|
|
60
|
+
mobilized_sources << source
|
|
61
|
+
|
|
62
|
+
for possible_source in possible_sources
|
|
63
|
+
path = File.join config.stylesheets_dir, "#{possible_source}.css"
|
|
64
|
+
sass_path = File.join config.stylesheets_dir, "sass", "#{possible_source}.sass"
|
|
65
|
+
mobilized_sources << possible_source if File.exist?(path) || File.exist?(sass_path)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
stylesheet_link_tag_without_mobilization *mobilized_sources
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
ActionView::Base.send :include, MobileFu::MobilizedStyles
|
|
75
|
+
ActionView::Base.send :alias_method_chain, :stylesheet_link_tag, :mobilization
|
data/mobile-fu.gemspec
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
|
3
|
+
require "mobile-fu/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |s|
|
|
6
|
+
s.name = "mobile-fu"
|
|
7
|
+
s.version = MobileFu::VERSION
|
|
8
|
+
s.platform = Gem::Platform::RUBY
|
|
9
|
+
s.authors = ["Brendan Lim", "Ben Langfeld"]
|
|
10
|
+
s.email = ["brendangl@gmail.com, ben@langfeld.me"]
|
|
11
|
+
s.homepage = "https://github.com/benlangfeld/mobile-fu"
|
|
12
|
+
s.summary = %q{Automatically detect mobile requests from mobile devices in your Rails application.}
|
|
13
|
+
s.description = %q{Want to automatically detect mobile devices that access your Rails application? Mobile Fu allows you to do just that. People can access your site from a Palm, Blackberry, iPhone, iPad, Nokia, etc. and it will automatically adjust the format of the request from :html to :mobile.}
|
|
14
|
+
|
|
15
|
+
s.rubyforge_project = "mobile-fu"
|
|
16
|
+
|
|
17
|
+
s.files = `git ls-files`.split("\n")
|
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
20
|
+
s.require_paths = ["lib"]
|
|
21
|
+
|
|
22
|
+
s.add_dependency 'rails'
|
|
23
|
+
s.add_dependency 'rack-mobile-detect'
|
|
24
|
+
s.add_development_dependency 'rspec'
|
|
25
|
+
s.add_development_dependency 'rdoc'
|
|
26
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
module ActionView
|
|
4
|
+
module Helpers
|
|
5
|
+
module AssetTagHelper
|
|
6
|
+
STYLESHEETS_DIR = "stylesheets"
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
describe MobileFu::MobilizedStyles do
|
|
12
|
+
|
|
13
|
+
before(:each) do
|
|
14
|
+
@view = mock(:ActionView)
|
|
15
|
+
@request = mock(:ActionRequest)
|
|
16
|
+
@view.extend(MobileFu::MobilizedStyles)
|
|
17
|
+
@view.stub!(:request).and_return(@request)
|
|
18
|
+
@request.stub!(:user_agent)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def ua(str)
|
|
22
|
+
@request.stub!(:user_agent).and_return(str)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "will include a mobilized css file if it recognizes a string in the user agent" do
|
|
26
|
+
ua "iphone"
|
|
27
|
+
File.should_receive(:exist?).with("stylesheets/style_iphone.css").and_return(true)
|
|
28
|
+
@view.should_receive(:stylesheet_link_tag_without_mobilization).with("style", "style_iphone")
|
|
29
|
+
@view.stylesheet_link_tag_with_mobilization("style")
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "includes mobiziled css files whether or not the original call to stylesheet_link_tag used a file extension" do
|
|
33
|
+
ua "blackberry"
|
|
34
|
+
File.should_receive(:exist?).with("stylesheets/style_blackberry.css").and_return(true)
|
|
35
|
+
@view.should_receive(:stylesheet_link_tag_without_mobilization).with("style.css", "style_blackberry")
|
|
36
|
+
@view.stylesheet_link_tag_with_mobilization("style.css")
|
|
37
|
+
end
|
|
38
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'mobile-fu'
|
metadata
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mobile-fu
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
prerelease:
|
|
5
|
+
version: 0.0.1
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Brendan Lim
|
|
9
|
+
- Ben Langfeld
|
|
10
|
+
autorequire:
|
|
11
|
+
bindir: bin
|
|
12
|
+
cert_chain: []
|
|
13
|
+
|
|
14
|
+
date: 2011-04-27 00:00:00 +01:00
|
|
15
|
+
default_executable:
|
|
16
|
+
dependencies:
|
|
17
|
+
- !ruby/object:Gem::Dependency
|
|
18
|
+
name: rails
|
|
19
|
+
prerelease: false
|
|
20
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
21
|
+
none: false
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: "0"
|
|
26
|
+
type: :runtime
|
|
27
|
+
version_requirements: *id001
|
|
28
|
+
- !ruby/object:Gem::Dependency
|
|
29
|
+
name: rack-mobile-detect
|
|
30
|
+
prerelease: false
|
|
31
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
|
32
|
+
none: false
|
|
33
|
+
requirements:
|
|
34
|
+
- - ">="
|
|
35
|
+
- !ruby/object:Gem::Version
|
|
36
|
+
version: "0"
|
|
37
|
+
type: :runtime
|
|
38
|
+
version_requirements: *id002
|
|
39
|
+
- !ruby/object:Gem::Dependency
|
|
40
|
+
name: rspec
|
|
41
|
+
prerelease: false
|
|
42
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
|
43
|
+
none: false
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: "0"
|
|
48
|
+
type: :development
|
|
49
|
+
version_requirements: *id003
|
|
50
|
+
- !ruby/object:Gem::Dependency
|
|
51
|
+
name: rdoc
|
|
52
|
+
prerelease: false
|
|
53
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
|
54
|
+
none: false
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: "0"
|
|
59
|
+
type: :development
|
|
60
|
+
version_requirements: *id004
|
|
61
|
+
description: Want to automatically detect mobile devices that access your Rails application? Mobile Fu allows you to do just that. People can access your site from a Palm, Blackberry, iPhone, iPad, Nokia, etc. and it will automatically adjust the format of the request from :html to :mobile.
|
|
62
|
+
email:
|
|
63
|
+
- brendangl@gmail.com, ben@langfeld.me
|
|
64
|
+
executables: []
|
|
65
|
+
|
|
66
|
+
extensions: []
|
|
67
|
+
|
|
68
|
+
extra_rdoc_files: []
|
|
69
|
+
|
|
70
|
+
files:
|
|
71
|
+
- .gitignore
|
|
72
|
+
- CHANGELOG
|
|
73
|
+
- Gemfile
|
|
74
|
+
- MIT-LICENSE
|
|
75
|
+
- README.rdoc
|
|
76
|
+
- Rakefile
|
|
77
|
+
- lib/mobile-fu.rb
|
|
78
|
+
- lib/mobile-fu/helper.rb
|
|
79
|
+
- lib/mobile-fu/mobilized_styles.rb
|
|
80
|
+
- lib/mobile-fu/version.rb
|
|
81
|
+
- mobile-fu.gemspec
|
|
82
|
+
- spec/mobilized_styles_spec.rb
|
|
83
|
+
- spec/spec_helper.rb
|
|
84
|
+
has_rdoc: true
|
|
85
|
+
homepage: https://github.com/benlangfeld/mobile-fu
|
|
86
|
+
licenses: []
|
|
87
|
+
|
|
88
|
+
post_install_message:
|
|
89
|
+
rdoc_options: []
|
|
90
|
+
|
|
91
|
+
require_paths:
|
|
92
|
+
- lib
|
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
94
|
+
none: false
|
|
95
|
+
requirements:
|
|
96
|
+
- - ">="
|
|
97
|
+
- !ruby/object:Gem::Version
|
|
98
|
+
version: "0"
|
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
|
+
none: false
|
|
101
|
+
requirements:
|
|
102
|
+
- - ">="
|
|
103
|
+
- !ruby/object:Gem::Version
|
|
104
|
+
version: "0"
|
|
105
|
+
requirements: []
|
|
106
|
+
|
|
107
|
+
rubyforge_project: mobile-fu
|
|
108
|
+
rubygems_version: 1.6.1
|
|
109
|
+
signing_key:
|
|
110
|
+
specification_version: 3
|
|
111
|
+
summary: Automatically detect mobile requests from mobile devices in your Rails application.
|
|
112
|
+
test_files:
|
|
113
|
+
- spec/mobilized_styles_spec.rb
|
|
114
|
+
- spec/spec_helper.rb
|