mobile-fu 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,7 +4,7 @@ Mobile Fu
4
4
  Want to automatically detect mobile devices that access your Rails application?
5
5
  Mobile Fu allows you to do just that. People can access your site from a Palm,
6
6
  Blackberry, iPhone, iPad, Nokia, etc. and it will automatically adjust the format
7
- of the request from :html to :mobile.
7
+ of the request from :html to :mobile or :tablet.
8
8
 
9
9
  Installation
10
10
  ------------
@@ -25,18 +25,22 @@ set as :mobile format. It is up to you to determine how you want to handle
25
25
  these requests. It is also up to you to create the .mobile.erb versions of
26
26
  your views that are to be requested.
27
27
 
28
- Then add the line below to config/initializers/mime_types.rb
29
-
30
- Mime::Type.register_alias "text/html", :mobile
28
+ Mobile Fu automatically adds a new `:mobile` and `:tablet` to `text/html` mime type
29
+ alias for Rails apps. If you already have a custom `:mobile` alias registered in
30
+ `config/initializers/mime_types.rb`, you can remove that.
31
31
 
32
32
  I recommend that you setup a before_filter that will redirect to a specific page
33
33
  depending on whether or not it is a mobile request. How can you check this?
34
34
 
35
- is_mobile_device? # => Returns true or false depending on the device
35
+ is_mobile_device? # => Returns true or false depending on the device or
36
+
37
+ is_tablet_device? # => Returns true if the device is a tablet
36
38
 
37
39
  You can also determine which format is currently set in by calling the following:
38
40
 
39
- is_mobile_view? # => Returns true or false depending on current req. format
41
+ in_mobile_view? # => Returns true or false depending on current req. format or
42
+
43
+ in_tablet_view? # => Returns true if the current req. format is for tablet view
40
44
 
41
45
  Also, if you want the ability to allow a user to switch between 'mobile' and
42
46
  'standard' format (:html), you can just adjust the mobile_view session variable
@@ -45,6 +49,9 @@ in a custom controller action.
45
49
  session[:mobile_view] # => Set to true if request format is :mobile and false
46
50
  if set to :html
47
51
 
52
+ session[:tablet_view] # => Set to true if request format is :tablet and false
53
+ if set to :html
54
+
48
55
  So, different devices need different styling. Don't worry, we've got this
49
56
  baked in to Mobile Fu.
50
57
 
@@ -86,5 +93,11 @@ mobile device emulator, or you can call `force_mobile_format` in a before filter
86
93
  before_filter :force_mobile_format
87
94
  end
88
95
 
96
+ You can also force the tablet view by calling `force_tablet_format` instead
97
+
98
+ class ApplicationController < ActionController::Base
99
+ has_mobile_fu
100
+ before_filter :force_tablet_format
101
+ end
89
102
 
90
103
  Copyright (c) 2008 Brendan G. Lim, Intridea, Inc., released under the MIT license
data/lib/mobile-fu.rb CHANGED
@@ -25,11 +25,16 @@ module MobileFu
25
25
  end
26
26
 
27
27
  Mime::Type.register_alias "text/html", :mobile
28
+ Mime::Type.register_alias "text/html", :tablet
28
29
  end
29
30
  end
30
31
 
31
32
  module ActionController
32
33
  module MobileFu
34
+ # These are various strings that can be found in tablet devices. Please feel free
35
+ # to add on to this list.
36
+ TABLET_USER_AGENTS = 'ipad|android 3.0|xoom|sch-i800|playbook|tablet|kindle|honeycomb'
37
+
33
38
  def self.included(base)
34
39
  base.extend ClassMethods
35
40
  end
@@ -55,11 +60,13 @@ module ActionController
55
60
  before_filter :set_request_format if set_request_format
56
61
 
57
62
  helper_method :is_mobile_device?
63
+ helper_method :is_tablet_device?
58
64
  helper_method :in_mobile_view?
65
+ helper_method :in_tablet_view?
59
66
  helper_method :is_device?
60
67
  helper_method :mobile_device
61
68
  end
62
-
69
+
63
70
  # Add this to your controllers to prevent the mobile format from being set for specific actions
64
71
  # class AwesomeController < ApplicationController
65
72
  # has_no_mobile_fu_for :index
@@ -91,13 +98,25 @@ module ActionController
91
98
  end
92
99
  end
93
100
 
101
+ # Forces the request format to be :tablet
102
+ def force_tablet_format
103
+ unless request.xhr?
104
+ request.format = :tablet
105
+ session[:tablet_view] = true if session[:tablet_view].nil?
106
+ end
107
+ end
108
+
94
109
  # Determines the request format based on whether the device is mobile or if
95
- # the user has opted to use either the 'Standard' view or 'Mobile' view.
110
+ # the user has opted to use either the 'Standard' view or 'Mobile' view or
111
+ # 'Tablet' view.
96
112
 
97
113
  def set_mobile_format
98
114
  if !mobile_exempt? && is_mobile_device? && !request.xhr?
99
- request.format = session[:mobile_view] == false ? :html : :mobile
115
+ request.format = :mobile unless session[:mobile_view] == false
100
116
  session[:mobile_view] = true if session[:mobile_view].nil?
117
+ elsif !mobile_exempt? && is_tablet_device? && !request.xhr?
118
+ request.format = :tablet unless session[:tablet_view] == false
119
+ session[:tablet_view] = true if session[:tablet_view].nil?
101
120
  end
102
121
  end
103
122
 
@@ -109,6 +128,14 @@ module ActionController
109
128
  request.format.to_sym == :mobile
110
129
  end
111
130
 
131
+ # Returns either true or false depending on whether or not the format of the
132
+ # request is either :tablet or not.
133
+
134
+ def in_tablet_view?
135
+ return false unless request.format
136
+ request.format.to_sym == :tablet
137
+ end
138
+
112
139
  # Returns either true or false depending on whether or not the user agent of
113
140
  # the device making the request is matched to a device in our regex.
114
141
 
@@ -116,6 +143,10 @@ module ActionController
116
143
  !!mobile_device
117
144
  end
118
145
 
146
+ def is_tablet_device?
147
+ request.user_agent.to_s.downcase =~ Regexp.new(ActionController::MobileFu::TABLET_USER_AGENTS)
148
+ end
149
+
119
150
  def mobile_device
120
151
  request.headers['X_MOBILE_DEVICE']
121
152
  end
@@ -126,10 +157,10 @@ module ActionController
126
157
  def is_device?(type)
127
158
  request.user_agent.to_s.downcase.include? type.to_s.downcase
128
159
  end
129
-
160
+
130
161
  # Returns true if current action isn't supposed to use mobile format
131
162
  # See #has_no_mobile_fu_for
132
-
163
+
133
164
  def mobile_exempt?
134
165
  self.class.instance_variable_get("@mobile_exempt_actions").try(:include?, params[:action].to_sym)
135
166
  end
@@ -6,18 +6,36 @@ module MobileFu
6
6
 
7
7
  def stylesheet_link_tag_with_mobilization(*sources)
8
8
  mobilized_sources = Array.new
9
+
10
+ # Figure out where stylesheets live, which differs depending if the asset
11
+ # pipeline is used or not.
12
+ stylesheets_dir = config.stylesheets_dir # Rails.root/public/stylesheets
13
+
14
+ # Look for mobilized stylesheets in the app/assets path if asset pipeline
15
+ # is enabled, because public/stylesheets will be missing in development
16
+ # mode without precompiling assets first, and may also contain multiple
17
+ # stylesheets with similar names because of checksumming during
18
+ # precompilation.
19
+ if Rails.application.config.respond_to?(:assets) # don't break pre-rails3.1
20
+ if Rails.application.config.assets.enabled
21
+ stylesheets_dir = File.join(Rails.root, 'app/assets/stylesheets/')
22
+ end
23
+ end
24
+
25
+ device_names = respond_to?(:is_mobile_device?) && is_mobile_device? ? ['mobile', mobile_device.downcase] : []
26
+
9
27
  sources.each do |source|
10
28
  mobilized_sources << source
11
-
12
- device_names = respond_to?(:is_mobile_device?) && is_mobile_device? ? ['mobile', mobile_device.downcase] : []
13
-
29
+
14
30
  device_names.compact.each do |device_name|
15
- possible_source = "#{source.to_s.gsub '.css', ''}_#{device_name}.css"
16
- path = File.join config.stylesheets_dir, possible_source
17
- mobilized_sources << possible_source if File.exist?(path)
31
+ # support ERB and/or SCSS extensions (e.g., mobile.css.erb, mobile.css.scss.erb)
32
+ possible_source = source.to_s.sub(/\.css.*$/, '') + "_#{device_name}"
33
+
34
+ mobilized_files = Dir.glob(File.join(stylesheets_dir, "#{possible_source}.css*")).map { |f| f.sub(stylesheets_dir, '') }
35
+ mobilized_sources += mobilized_files.map { |f| f.sub(/\.css.*/, '') }
18
36
  end
19
37
  end
20
-
38
+
21
39
  stylesheet_link_tag_without_mobilization *mobilized_sources
22
40
  end
23
41
  end
@@ -1,3 +1,3 @@
1
1
  module MobileFu
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobile-fu
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-01-21 00:00:00.000000000 Z
13
+ date: 2012-04-30 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
17
- requirement: &2156767760 !ruby/object:Gem::Requirement
17
+ requirement: !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,15 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2156767760
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
26
31
  - !ruby/object:Gem::Dependency
27
32
  name: rack-mobile-detect
28
- requirement: &2156767340 !ruby/object:Gem::Requirement
33
+ requirement: !ruby/object:Gem::Requirement
29
34
  none: false
30
35
  requirements:
31
36
  - - ! '>='
@@ -33,10 +38,15 @@ dependencies:
33
38
  version: '0'
34
39
  type: :runtime
35
40
  prerelease: false
36
- version_requirements: *2156767340
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
37
47
  - !ruby/object:Gem::Dependency
38
48
  name: rspec
39
- requirement: &2156766920 !ruby/object:Gem::Requirement
49
+ requirement: !ruby/object:Gem::Requirement
40
50
  none: false
41
51
  requirements:
42
52
  - - ! '>='
@@ -44,10 +54,15 @@ dependencies:
44
54
  version: '0'
45
55
  type: :development
46
56
  prerelease: false
47
- version_requirements: *2156766920
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
48
63
  - !ruby/object:Gem::Dependency
49
64
  name: rdoc
50
- requirement: &2156766480 !ruby/object:Gem::Requirement
65
+ requirement: !ruby/object:Gem::Requirement
51
66
  none: false
52
67
  requirements:
53
68
  - - ! '>='
@@ -55,7 +70,12 @@ dependencies:
55
70
  version: '0'
56
71
  type: :development
57
72
  prerelease: false
58
- version_requirements: *2156766480
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
59
79
  description: Want to automatically detect mobile devices that access your Rails application?
60
80
  Mobile Fu allows you to do just that. People can access your site from a Palm, Blackberry,
61
81
  iPhone, iPad, Nokia, etc. and it will automatically adjust the format of the request
@@ -66,16 +86,12 @@ executables: []
66
86
  extensions: []
67
87
  extra_rdoc_files: []
68
88
  files:
69
- - .gitignore
70
89
  - CHANGELOG
71
- - Gemfile
72
90
  - MIT-LICENSE
73
91
  - README.md
74
- - Rakefile
75
92
  - lib/mobile-fu.rb
76
93
  - lib/mobile-fu/helper.rb
77
94
  - lib/mobile-fu/version.rb
78
- - mobile-fu.gemspec
79
95
  - spec/mobilized_styles_spec.rb
80
96
  - spec/spec_helper.rb
81
97
  homepage: https://github.com/benlangfeld/mobile-fu
@@ -90,24 +106,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
106
  - - ! '>='
91
107
  - !ruby/object:Gem::Version
92
108
  version: '0'
93
- segments:
94
- - 0
95
- hash: 1843169329843289785
96
109
  required_rubygems_version: !ruby/object:Gem::Requirement
97
110
  none: false
98
111
  requirements:
99
112
  - - ! '>='
100
113
  - !ruby/object:Gem::Version
101
114
  version: '0'
102
- segments:
103
- - 0
104
- hash: 1843169329843289785
105
115
  requirements: []
106
116
  rubyforge_project: mobile-fu
107
- rubygems_version: 1.8.10
117
+ rubygems_version: 1.8.21
108
118
  signing_key:
109
119
  specification_version: 3
110
120
  summary: Automatically detect mobile requests from mobile devices in your Rails application.
111
121
  test_files:
112
122
  - spec/mobilized_styles_spec.rb
113
123
  - spec/spec_helper.rb
124
+ has_rdoc:
data/.gitignore DELETED
@@ -1,5 +0,0 @@
1
- *.gem
2
- .bundle
3
- Gemfile.lock
4
- pkg/*
5
- vendor/ruby
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- # Specify your gem's dependencies in mobile-fu.gemspec
4
- gemspec
data/Rakefile DELETED
@@ -1,16 +0,0 @@
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/mobile-fu.gemspec DELETED
@@ -1,26 +0,0 @@
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