mobile-fu 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,27 +1,18 @@
1
- = Mobile Fu
1
+ Mobile Fu
2
+ =========
2
3
 
3
- Want to automatically detect mobile devices that access your Rails application?
4
+ Want to automatically detect mobile devices that access your Rails application?
4
5
  Mobile Fu allows you to do just that. People can access your site from a Palm,
5
6
  Blackberry, iPhone, iPad, Nokia, etc. and it will automatically adjust the format
6
- of the request from :html to :mobile.
7
+ of the request from :html to :mobile.
7
8
 
8
- == Rails 3 Compatibility
9
+ Installation
10
+ ------------
9
11
 
10
- The master branch of this plugin is Rails 3 compatible.
12
+ Simply add `gem 'mobile-fu'` to your Gemfile and run bundle install.
11
13
 
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
14
+ Usage
15
+ -----
25
16
 
26
17
  Add this this one line to the controller.
27
18
 
@@ -29,71 +20,71 @@ Add this this one line to the controller.
29
20
  has_mobile_fu
30
21
  end
31
22
 
32
- Once this is in place, any request that comes from a mobile device will be be
23
+ Once this is in place, any request that comes from a mobile device will be be
33
24
  set as :mobile format. It is up to you to determine how you want to handle
34
25
  these requests. It is also up to you to create the .mobile.erb versions of
35
26
  your views that are to be requested.
36
27
 
37
28
  Then add the line below to config/initializers/mime_types.rb
38
29
 
39
- Mime::Type.register_alias "text/html", :mobile
30
+ Mime::Type.register_alias "text/html", :mobile
40
31
 
41
32
  I recommend that you setup a before_filter that will redirect to a specific page
42
33
  depending on whether or not it is a mobile request. How can you check this?
43
34
 
44
- is_mobile_device? # => Returns true or false depending on the device
35
+ is_mobile_device? # => Returns true or false depending on the device
45
36
 
46
37
  You can also determine which format is currently set in by calling the following:
47
38
 
48
- is_mobile_view? # => Returns true or false depending on current req. format
39
+ is_mobile_view? # => Returns true or false depending on current req. format
49
40
 
50
41
  Also, if you want the ability to allow a user to switch between 'mobile' and
51
42
  'standard' format (:html), you can just adjust the mobile_view session variable
52
43
  in a custom controller action.
53
44
 
54
- session[:mobile_view] # => Set to true if request format is :mobile and false
55
- if set to :html
45
+ session[:mobile_view] # => Set to true if request format is :mobile and false
46
+ if set to :html
56
47
 
57
48
  So, different devices need different styling. Don't worry, we've got this
58
49
  baked in to Mobile Fu.
59
50
 
60
- If you are including a css or sass file via stylesheet_link_tag, all you have
51
+ If you are including a css or sass file via `stylesheet_link_tag`, all you have
61
52
  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
53
+ for a certain device. The stylesheet that is loaded is dependant on which device
63
54
  is making the request.
64
55
 
65
56
  e.g., Accessing a page from a Blackberry.
66
57
 
67
- ... stylesheet_link_tag 'mobile.css' ...
58
+ ... stylesheet_link_tag 'mobile.css' ...
68
59
 
69
60
  This loads mobile.css, and mobile_blackberry.css if the file exists.
70
61
 
71
62
  Supported stylesheet override device extensions at the moment are:
72
63
 
73
- blackberry
74
- iphone (iphone,ipod)
75
- ipad
76
- android
77
- mobileexplorer
78
- nokia
79
- palm
64
+ * blackberry
65
+ * iphone (iphone,ipod)
66
+ * ipad
67
+ * android
68
+ * mobileexplorer
69
+ * nokia
70
+ * palm
80
71
 
81
- The stylesheet awesomeness was derived from Michael Bleigh's browserized styles:
82
- http://www.intridea.com/2007/12/9/announcing-browserized-styles
72
+ The stylesheet awesomeness was derived from [Michael Bleigh's browserized styles](http://www.intridea.com/2007/12/9/announcing-browserized-styles)
83
73
 
84
- Inspiration for Mobile Fu came from Noel Rappin's rails_iui:
85
- http://blogs.pathf.com/agileajax/2008/05/rails-developme.html
74
+ Inspiration for Mobile Fu came from [Noel Rappin's rails_iui](http://blogs.pathf.com/agileajax/2008/05/rails-developme.html)
86
75
 
87
76
  Hopefully this should help you create some awesome mobile applications.
88
77
 
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.
78
+ Testing Mobile Interface
79
+ ------------------------
80
+
81
+ If you want to force the mobile interface for testing, you can either use a
82
+ mobile device emulator, or you can call `force_mobile_format` in a before filter.
93
83
 
94
- class ApplicationController < ActionController::Base
95
- has_mobile_fu(true)
96
- end
84
+ class ApplicationController < ActionController::Base
85
+ has_mobile_fu
86
+ before_filter :force_mobile_format
87
+ end
97
88
 
98
89
 
99
90
  Copyright (c) 2008 Brendan G. Lim, Intridea, Inc., released under the MIT license
@@ -28,40 +28,32 @@ module ActionController
28
28
  # has_mobile_fu
29
29
  # end
30
30
  #
31
- # You can also force mobile mode by passing in 'true'
31
+ # If you don't want mobile_fu to set the device type automatically,
32
+ # you cal pass false here.
32
33
  #
33
34
  # class ApplicationController < ActionController::Base
34
- # has_mobile_fu true
35
+ # has_mobile_fu false
35
36
  # end
37
+ #
36
38
 
37
- def has_mobile_fu(test_mode = false)
39
+ def has_mobile_fu(set_device_type = false)
38
40
  include ActionController::MobileFu::InstanceMethods
39
41
 
40
- before_filter test_mode ? :force_mobile_format : :set_mobile_format
42
+ before_filter :set_device_type if set_device_type
41
43
 
42
44
  helper_method :is_mobile_device?
43
45
  helper_method :in_mobile_view?
44
46
  helper_method :is_device?
45
47
  helper_method :mobile_device
46
48
  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
49
  end
60
50
 
61
51
  module InstanceMethods
52
+ def set_device_type(force_mobile = false)
53
+ force_mobile ? force_mobile_format : set_mobile_format
54
+ end
62
55
 
63
56
  # Forces the request format to be :mobile
64
-
65
57
  def force_mobile_format
66
58
  unless request.xhr?
67
59
  request.format = :mobile
@@ -104,7 +96,6 @@ module ActionController
104
96
  request.user_agent.to_s.downcase.include? type.to_s.downcase
105
97
  end
106
98
  end
107
-
108
99
  end
109
100
 
110
101
  end
@@ -1,22 +1,5 @@
1
1
  module MobileFu
2
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
3
  def js_enabled_mobile_device?
21
4
  is_device?('iphone') || is_device?('ipod') || is_device?('ipad') || is_device?('mobileexplorer') || is_device?('android')
22
5
  end
@@ -1,72 +1,23 @@
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
1
  module MobileFu
23
2
  module MobilizedStyles
24
3
 
25
4
  # This logic was taken from Michael Bleigh's browserized styles
26
5
  # with modification to work for mobile browsers.
27
6
 
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
7
+ def device_name
8
+ @device_name ||= headers['X_MOBILE_DEVICE']
51
9
  end
52
10
 
53
11
  def stylesheet_link_tag_with_mobilization(*sources)
54
12
  mobilized_sources = Array.new
55
13
  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
14
  mobilized_sources << source
61
15
 
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
16
+ path = File.join config.stylesheets_dir, "#{source.to_s.gsub '.css', ''}_#{device_name}.css"
17
+ mobilized_sources << possible_source if File.exist?(path)
67
18
  end
68
19
 
69
- stylesheet_link_tag_without_mobilization *mobilized_sources
20
+ stylesheet_link_tag *mobilized_sources
70
21
  end
71
22
  end
72
23
  end
@@ -1,3 +1,3 @@
1
1
  module MobileFu
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,38 +1,30 @@
1
1
  require 'spec_helper'
2
2
 
3
- module ActionView
4
- module Helpers
5
- module AssetTagHelper
6
- STYLESHEETS_DIR = "stylesheets"
7
- end
8
- end
9
- end
10
-
11
3
  describe MobileFu::MobilizedStyles do
12
4
 
13
5
  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)
6
+ @view = mock :ActionView
7
+ @request = mock :ActionRequest
8
+ @view.extend MobileFu::MobilizedStyles
9
+ @view.stub!(:request).and_return @request
10
+ @request.stub! :user_agent
19
11
  end
20
12
 
21
13
  def ua(str)
22
- @request.stub!(:user_agent).and_return(str)
14
+ @request.stub!(:user_agent).and_return str
23
15
  end
24
16
 
25
17
  it "will include a mobilized css file if it recognizes a string in the user agent" do
26
18
  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")
19
+ File.should_receive(:exist?).with("stylesheets/style_iphone.css").and_return true
20
+ @view.should_receive(:stylesheet_link_tag).with "style", "style_iphone"
21
+ @view.stylesheet_link_tag_with_mobilization "style"
30
22
  end
31
23
 
32
24
  it "includes mobiziled css files whether or not the original call to stylesheet_link_tag used a file extension" do
33
25
  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")
26
+ File.should_receive(:exist?).with("stylesheets/style_blackberry.css").and_return true
27
+ @view.should_receive(:stylesheet_link_tag).with "style.css", "style_blackberry"
28
+ @view.stylesheet_link_tag_with_mobilization "style.css"
37
29
  end
38
30
  end
metadata CHANGED
@@ -1,8 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobile-fu
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.0.1
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
6
10
  platform: ruby
7
11
  authors:
8
12
  - Brendan Lim
@@ -11,7 +15,7 @@ autorequire:
11
15
  bindir: bin
12
16
  cert_chain: []
13
17
 
14
- date: 2011-04-27 00:00:00 +01:00
18
+ date: 2011-05-13 00:00:00 +01:00
15
19
  default_executable:
16
20
  dependencies:
17
21
  - !ruby/object:Gem::Dependency
@@ -22,6 +26,8 @@ dependencies:
22
26
  requirements:
23
27
  - - ">="
24
28
  - !ruby/object:Gem::Version
29
+ segments:
30
+ - 0
25
31
  version: "0"
26
32
  type: :runtime
27
33
  version_requirements: *id001
@@ -33,6 +39,8 @@ dependencies:
33
39
  requirements:
34
40
  - - ">="
35
41
  - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
36
44
  version: "0"
37
45
  type: :runtime
38
46
  version_requirements: *id002
@@ -44,6 +52,8 @@ dependencies:
44
52
  requirements:
45
53
  - - ">="
46
54
  - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
47
57
  version: "0"
48
58
  type: :development
49
59
  version_requirements: *id003
@@ -55,6 +65,8 @@ dependencies:
55
65
  requirements:
56
66
  - - ">="
57
67
  - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
58
70
  version: "0"
59
71
  type: :development
60
72
  version_requirements: *id004
@@ -72,7 +84,7 @@ files:
72
84
  - CHANGELOG
73
85
  - Gemfile
74
86
  - MIT-LICENSE
75
- - README.rdoc
87
+ - README.md
76
88
  - Rakefile
77
89
  - lib/mobile-fu.rb
78
90
  - lib/mobile-fu/helper.rb
@@ -95,17 +107,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
107
  requirements:
96
108
  - - ">="
97
109
  - !ruby/object:Gem::Version
110
+ segments:
111
+ - 0
98
112
  version: "0"
99
113
  required_rubygems_version: !ruby/object:Gem::Requirement
100
114
  none: false
101
115
  requirements:
102
116
  - - ">="
103
117
  - !ruby/object:Gem::Version
118
+ segments:
119
+ - 0
104
120
  version: "0"
105
121
  requirements: []
106
122
 
107
123
  rubyforge_project: mobile-fu
108
- rubygems_version: 1.6.1
124
+ rubygems_version: 1.3.7
109
125
  signing_key:
110
126
  specification_version: 3
111
127
  summary: Automatically detect mobile requests from mobile devices in your Rails application.