mobylette 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +83 -0
- data/Rakefile +37 -0
- data/lib/mobylette.rb +12 -0
- data/lib/mobylette/controllers/helpers.rb +52 -0
- data/lib/mobylette/controllers/respond_to_mobile_requests.rb +86 -0
- data/lib/mobylette/helmet.rb +41 -0
- data/lib/mobylette/helmet/faker.rb +19 -0
- data/lib/mobylette/helmet/helpers.rb +34 -0
- data/lib/mobylette/railtie.rb +10 -0
- data/lib/mobylette/version.rb +3 -0
- data/lib/tasks/mobylette_tasks.rake +4 -0
- data/spec/controllers/actioncontroller_base_spec.rb +16 -0
- data/spec/controllers/default_fallback_controller_spec.rb +36 -0
- data/spec/controllers/desktop_only_controller_spec.rb +17 -0
- data/spec/controllers/force_fallback_controller_spec.rb +41 -0
- data/spec/controllers/home_controller_spec.rb +89 -0
- data/spec/controllers/no_fallback_controller_spec.rb +56 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +9 -0
- data/spec/dummy/app/assets/javascripts/application_mobile.js +9 -0
- data/spec/dummy/app/assets/javascripts/default_fallback.js +2 -0
- data/spec/dummy/app/assets/javascripts/desktop_only.js +2 -0
- data/spec/dummy/app/assets/javascripts/fallbacks.js +2 -0
- data/spec/dummy/app/assets/javascripts/force_fallback.js +2 -0
- data/spec/dummy/app/assets/javascripts/home.js +2 -0
- data/spec/dummy/app/assets/javascripts/no_fallback.js +2 -0
- data/spec/dummy/app/assets/javascripts/testing.js +2 -0
- data/spec/dummy/app/assets/stylesheets/application.css +11 -0
- data/spec/dummy/app/assets/stylesheets/application_mobile.css +11 -0
- data/spec/dummy/app/assets/stylesheets/default_fallback.css +4 -0
- data/spec/dummy/app/assets/stylesheets/desktop_only.css +4 -0
- data/spec/dummy/app/assets/stylesheets/fallbacks.css +4 -0
- data/spec/dummy/app/assets/stylesheets/force_fallback.css +4 -0
- data/spec/dummy/app/assets/stylesheets/home.css +4 -0
- data/spec/dummy/app/assets/stylesheets/no_fallback.css +4 -0
- data/spec/dummy/app/assets/stylesheets/testing.css +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +4 -0
- data/spec/dummy/app/controllers/default_fallback_controller.rb +13 -0
- data/spec/dummy/app/controllers/desktop_only_controller.rb +2 -0
- data/spec/dummy/app/controllers/fallbacks_controller.rb +7 -0
- data/spec/dummy/app/controllers/force_fallback_controller.rb +13 -0
- data/spec/dummy/app/controllers/home_controller.rb +19 -0
- data/spec/dummy/app/controllers/no_fallback_controller.rb +13 -0
- data/spec/dummy/app/controllers/testing_controller.rb +9 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/helpers/default_fallback_helper.rb +2 -0
- data/spec/dummy/app/helpers/desktop_only_helper.rb +2 -0
- data/spec/dummy/app/helpers/fallbacks_helper.rb +2 -0
- data/spec/dummy/app/helpers/force_fallback_helper.rb +2 -0
- data/spec/dummy/app/helpers/home_helper.rb +2 -0
- data/spec/dummy/app/helpers/no_fallback_helper.rb +2 -0
- data/spec/dummy/app/helpers/testing_helper.rb +2 -0
- data/spec/dummy/app/views/default_fallback/index.html.erb +1 -0
- data/spec/dummy/app/views/default_fallback/index.js.erb +1 -0
- data/spec/dummy/app/views/default_fallback/index.mobile.erb +1 -0
- data/spec/dummy/app/views/default_fallback/test.html.erb +1 -0
- data/spec/dummy/app/views/default_fallback/test.js.erb +1 -0
- data/spec/dummy/app/views/fallbacks/index.html.erb +2 -0
- data/spec/dummy/app/views/fallbacks/index.js.erb +1 -0
- data/spec/dummy/app/views/fallbacks/index.mobile.erb +0 -0
- data/spec/dummy/app/views/fallbacks/index.xml.erb +0 -0
- data/spec/dummy/app/views/fallbacks/no_mobile.html.erb +0 -0
- data/spec/dummy/app/views/force_fallback/index.html.erb +1 -0
- data/spec/dummy/app/views/force_fallback/index.js.erb +1 -0
- data/spec/dummy/app/views/force_fallback/index.mobile.erb +1 -0
- data/spec/dummy/app/views/force_fallback/test.html.erb +1 -0
- data/spec/dummy/app/views/force_fallback/test.js.erb +1 -0
- data/spec/dummy/app/views/home/desktop.html.erb +1 -0
- data/spec/dummy/app/views/home/index.html.erb +7 -0
- data/spec/dummy/app/views/home/index.mobile.erb +7 -0
- data/spec/dummy/app/views/home/mobile.mobile.erb +1 -0
- data/spec/dummy/app/views/home/no_mobile_view.html.erb +1 -0
- data/spec/dummy/app/views/layouts/application.html.erb +16 -0
- data/spec/dummy/app/views/layouts/application.mobile.erb +16 -0
- data/spec/dummy/app/views/no_fallback/index.html.erb +1 -0
- data/spec/dummy/app/views/no_fallback/index.js.erb +1 -0
- data/spec/dummy/app/views/no_fallback/index.mobile.erb +1 -0
- data/spec/dummy/app/views/no_fallback/test.html.erb +1 -0
- data/spec/dummy/app/views/no_fallback/test.js.erb +1 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +46 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +33 -0
- data/spec/dummy/config/environments/production.rb +51 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +22 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/schema.rb +16 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +32 -0
- data/spec/dummy/log/test.log +1537 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +26 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/tmp/cache/assets/C82/3E0/sprockets%2F48eb4ac616538f8f36870451073315da +0 -0
- data/spec/dummy/tmp/cache/assets/CE4/1C0/sprockets%2F18d93e1533d585b2f64ec90b33176ac9 +0 -0
- data/spec/dummy/tmp/cache/assets/CED/A70/sprockets%2F621683d8791730a9cc7ce45e17a7e46d +0 -0
- data/spec/dummy/tmp/cache/assets/CFC/200/sprockets%2F716f89d85d4855706b8a9abec1969c62 +0 -0
- data/spec/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/spec/dummy/tmp/cache/assets/D54/ED0/sprockets%2F71c9fa01091d432b131da3bb73faf3d4 +0 -0
- data/spec/dummy/tmp/cache/assets/D84/210/sprockets%2Fabd0103ccec2b428ac62c94e4c40b384 +0 -0
- data/spec/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/spec/dummy/tmp/pids/server.pid +1 -0
- data/spec/helpers/mobylette_helpers_spec.rb +59 -0
- data/spec/integration/navigation_spec.rb +9 -0
- data/spec/mobylette_spec.rb +7 -0
- data/spec/spec_helper.rb +35 -0
- metadata +320 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2011 YOURNAME
|
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,83 @@
|
|
1
|
+
http://fd.img.v4.skyrock.net/fd6/mantaur/pics/206915407_small.jpg
|
2
|
+
|
3
|
+
= Mobylette http://travis-ci.org/tscolari/mobylette.png
|
4
|
+
|
5
|
+
This gem works by adding the 'mobile' format to your rails application. Whenever a request come from a mobile device, if you have your controller mobile enabled, it shall render the view.mobile.erb instead of the view.html.erb (or haml, or whatever).
|
6
|
+
|
7
|
+
This gem is based on the mobile_fu plugin (https://github.com/brendanlim/mobile-fu). Also these pages were very usefull for me:
|
8
|
+
* http://stackoverflow.com/questions/5126085/ruby-on-rails-mobile-application
|
9
|
+
* http://www.arctickiwi.com/blog/mobile-enable-your-ruby-on-rails-site-for-small-screens
|
10
|
+
* http://metautonomo.us/2011/01/05/mobile-devices-and-rails-maintaining-your-sanity/
|
11
|
+
|
12
|
+
== How does it work?
|
13
|
+
|
14
|
+
By adding "respond_to_mobile_requests" in your application_controller (or any other controller), your controllers (or that controller) will understand mobile requests as a new mime type alias "mobile". This will make the controller to search for the .mobile.erb file instead of the .html.erb. Also you will be able to do:
|
15
|
+
|
16
|
+
respond_to do |format|
|
17
|
+
format.html { ... }
|
18
|
+
format.mobile { ... }
|
19
|
+
end
|
20
|
+
|
21
|
+
== Instalation
|
22
|
+
|
23
|
+
Add the gem to your gemfile:
|
24
|
+
|
25
|
+
gem 'mobylette', :git => "git://github.com/tscolari/mobylette.git"
|
26
|
+
|
27
|
+
And add to your ApplicationController.rb (for enabling it to all your controllers) or to the controllers you want this functionality on:
|
28
|
+
|
29
|
+
respond_to_mobile_requests
|
30
|
+
|
31
|
+
After that, you may start adding your .mobile. views.
|
32
|
+
|
33
|
+
== Helpers
|
34
|
+
|
35
|
+
==== is_mobile_request?
|
36
|
+
|
37
|
+
This helper returns true if the request comes from a mobile device, false if it does not.
|
38
|
+
|
39
|
+
==== mobylette_stylesheet_link_tag(*sources)
|
40
|
+
|
41
|
+
This works like the stylesheet_link_tag helper, but when the request comes from a mobile device, it adds "_mobile" to the stylesheets before calling stylesheet_link_tag
|
42
|
+
|
43
|
+
==== mobylette_javascript_include_tag(*soruces)
|
44
|
+
|
45
|
+
Same as mobylette_stylesheet_link_tag, but for javascript files and javascript_include_tag
|
46
|
+
|
47
|
+
== Fall Backs
|
48
|
+
|
49
|
+
By default, when the mobile format is not found, mobylette will fall back to the request original format. For example, if a cell phone makes a request by html, and there is no mobile view, it will render the html. You may force it always to fall back to a especific format, by passing the :fall_back parameter to the respond_to_mobile_requests method:
|
50
|
+
|
51
|
+
respond_to_mobile_requests :fall_back => :html
|
52
|
+
|
53
|
+
This would force all views (mobile) to fall back to the html views. You may also disable this behavior, and no fall back will ever occur:
|
54
|
+
|
55
|
+
respond_to_mobile_requests :fall_back => false
|
56
|
+
|
57
|
+
== Testing
|
58
|
+
|
59
|
+
http://image.tin247.com/kenh14/080918143137-689-665.jpg
|
60
|
+
|
61
|
+
Don't drive your mobylette without your Helmet! It's always safer do tests!
|
62
|
+
|
63
|
+
For testing, include the Mobylette::Helmet module to your test/test_helpers.rb:
|
64
|
+
|
65
|
+
include Mobylette::Helmet
|
66
|
+
|
67
|
+
For RSpec: add to your spec/spec_helpers.rb or create a spec/support/mobylette.rb with the following:
|
68
|
+
|
69
|
+
RSpec.configure do |config|
|
70
|
+
config.include Mobylette::Helmet, :type => :controller
|
71
|
+
end
|
72
|
+
|
73
|
+
This will add 2 methods to your test scope:
|
74
|
+
|
75
|
+
force_mobile_request_agent(device = "Android")
|
76
|
+
|
77
|
+
this one will force your user_agent to the one specified, allowing you to test mobile requests.
|
78
|
+
|
79
|
+
reset_test_request_agent
|
80
|
+
|
81
|
+
and this one will reset your user_agent to the test default "Rails Testing". You don't need to call this everytime, all your requests by default are "Rails Testing" in your test env.
|
82
|
+
|
83
|
+
Friendly note: on your tests, call these functions BEFORE you make the request, otherwise they are useless =p
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'Mobylette'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
Bundler::GemHelper.install_tasks
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
|
29
|
+
Rake::TestTask.new(:test) do |t|
|
30
|
+
t.libs << 'lib'
|
31
|
+
t.libs << 'test'
|
32
|
+
t.pattern = 'test/**/*_test.rb'
|
33
|
+
t.verbose = false
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
task :default => :test
|
data/lib/mobylette.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
module Mobylette
|
2
|
+
module Controllers
|
3
|
+
autoload "RespondToMobileRequests", "mobylette/controllers/respond_to_mobile_requests"
|
4
|
+
autoload "Helpers" , "mobylette/controllers/helpers"
|
5
|
+
end
|
6
|
+
|
7
|
+
autoload "Helmet" , "mobylette/helmet"
|
8
|
+
require 'mobylette/railtie'
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'action_controller'
|
12
|
+
Mime::Type.register_alias "text/html", :mobile
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Mobylette
|
2
|
+
module Controllers
|
3
|
+
|
4
|
+
#
|
5
|
+
# Mobylette::Controllers::Helpers include few methods to
|
6
|
+
# include different css/js files for the mobile and for
|
7
|
+
# the normal version of your layout
|
8
|
+
#
|
9
|
+
# Personal note: I'm rethinking and I guess that, since you
|
10
|
+
# may have (and probably will) a different layout file for the
|
11
|
+
# "normal" and mobile version, you probably wont use this at all
|
12
|
+
module Helpers
|
13
|
+
extend ActiveSupport::Concern
|
14
|
+
|
15
|
+
# Adds a "_mobile" sulfix to the files you include (when the request is mobile)
|
16
|
+
def mobylette_stylesheet_link_tag(*sources)
|
17
|
+
options = sources.extract_options!
|
18
|
+
if is_mobile_request?
|
19
|
+
stylesheet_link_tag(*sulfix_mobile_assets(sources, :css).insert(-1, options))
|
20
|
+
else
|
21
|
+
stylesheet_link_tag(*sources.insert(-1, options))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Adds a "_mobile" sulfix to the files you include (when the request is mobile)
|
26
|
+
def mobylette_javascript_include_tag(*sources)
|
27
|
+
options = sources.extract_options!
|
28
|
+
if is_mobile_request?
|
29
|
+
javascript_include_tag(*sulfix_mobile_assets(sources, :js).insert(-1, options))
|
30
|
+
else
|
31
|
+
javascript_include_tag(*sources.insert(-1, options))
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
#
|
38
|
+
# Anex the "_mobile" sulfix to each string in the array,
|
39
|
+
# before the .#{extension}, if it exists
|
40
|
+
def sulfix_mobile_assets(sources, extension)
|
41
|
+
sources.map do |source|
|
42
|
+
if source =~ /.#{extension}/
|
43
|
+
"#{source.split(/.#{extension}$/)[0]}_mobile.#{extension}"
|
44
|
+
else
|
45
|
+
"#{source}_mobile"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
module Mobylette
|
2
|
+
module Controllers
|
3
|
+
|
4
|
+
# Mobylette::Controllers::RespondToMobileRequests includes the respond_to_mobile_requests
|
5
|
+
# to your ActionController::Base.
|
6
|
+
#
|
7
|
+
# The respond_to_mobile_requests method enables the controller mobile handling
|
8
|
+
module RespondToMobileRequests
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
included do
|
12
|
+
helper_method :is_mobile_request?
|
13
|
+
|
14
|
+
# List of mobile agents, from mobile_fu (https://github.com/brendanlim/mobile-fu)
|
15
|
+
MOBILE_USER_AGENTS = 'palm|blackberry|nokia|phone|midp|mobi|symbian|chtml|ericsson|minimo|' +
|
16
|
+
'audiovox|motorola|samsung|telit|upg1|windows ce|ucweb|astel|plucker|' +
|
17
|
+
'x320|x240|j2me|sgh|portable|sprint|docomo|kddi|softbank|android|mmp|' +
|
18
|
+
'pdxgw|netfront|xiino|vodafone|portalmmm|sagem|mot-|sie-|ipod|up\\.b|' +
|
19
|
+
'webos|amoi|novarra|cdm|alcatel|pocket|ipad|iphone|mobileexplorer|' +
|
20
|
+
'mobile'
|
21
|
+
end
|
22
|
+
|
23
|
+
module ClassMethods
|
24
|
+
|
25
|
+
# This method enables the controller do handle mobile requests
|
26
|
+
# You must add this to every controller you want to respond differently to mobile devices,
|
27
|
+
# or make it application wide calling it from the ApplicationController
|
28
|
+
#
|
29
|
+
# Options:
|
30
|
+
# * :fall_back => :html
|
31
|
+
# You may pass a fall_back option to the method, it will force the render
|
32
|
+
# to look for that other format, in case there is not a .mobile file for the view.
|
33
|
+
# By default, it will fall back to the format of the original request.
|
34
|
+
# If you don't want fall back at all, pass :fall_back => false
|
35
|
+
#
|
36
|
+
def respond_to_mobile_requests(options = {})
|
37
|
+
return if self.included_modules.include?(Mobylette::Controllers::RespondToMobileRequestsMethods)
|
38
|
+
|
39
|
+
cattr_accessor :fall_back_format
|
40
|
+
self.fall_back_format = options[:fall_back]
|
41
|
+
|
42
|
+
self.send(:include, Mobylette::Controllers::RespondToMobileRequestsMethods)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
module InstanceMethods
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
# helper method to check if the current request if from a mobile device or not
|
51
|
+
def is_mobile_request?
|
52
|
+
return true if (request.format.to_s == "mobile") or (params[:format] == "mobile")
|
53
|
+
request.user_agent.to_s.downcase =~ /#{MOBILE_USER_AGENTS}/
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
# RespondToMobileRequestsMethods is included by respond_to_mobile_requests
|
60
|
+
# This will check if the request is from a mobile device and change
|
61
|
+
# the request format to :mobile
|
62
|
+
module RespondToMobileRequestsMethods
|
63
|
+
extend ActiveSupport::Concern
|
64
|
+
|
65
|
+
included do
|
66
|
+
before_filter :handle_mobile
|
67
|
+
end
|
68
|
+
|
69
|
+
module InstanceMethods
|
70
|
+
private
|
71
|
+
|
72
|
+
# Changes the request.form to :mobile, when the request is from
|
73
|
+
# a mobile device
|
74
|
+
def handle_mobile
|
75
|
+
if is_mobile_request?
|
76
|
+
original_format = request.format.to_sym
|
77
|
+
request.format = :mobile
|
78
|
+
if self.fall_back_format != false
|
79
|
+
request.formats << Mime::Type.new((self.fall_back_format if self.fall_back_format) || original_format)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Mobylette
|
2
|
+
# Mobylette::Helmet provides helper methods to help you when testing your controllers
|
3
|
+
# for a mobile request.
|
4
|
+
#
|
5
|
+
# You must include Mobylette::Helmet into your test case, in the README there is more
|
6
|
+
# documentation about how to make this test wide.
|
7
|
+
module Helmet
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
autoload "Helpers", "mobylette/helmet/helpers"
|
10
|
+
autoload "Faker" , "mobylette/helmet/faker"
|
11
|
+
|
12
|
+
# Force the request for the user_agent
|
13
|
+
# Remember to add it BEFORE the request
|
14
|
+
#
|
15
|
+
# Example:
|
16
|
+
#
|
17
|
+
# it "should render the mobile_device view on mobile request" do
|
18
|
+
# force_mobile_request_agent("Android")
|
19
|
+
# get :index
|
20
|
+
# response.should render_template(:mobile_device)
|
21
|
+
# end
|
22
|
+
def force_mobile_request_agent(user_agent = "Android")
|
23
|
+
request.user_agent = user_agent
|
24
|
+
end
|
25
|
+
|
26
|
+
# Reset the user_aget to the default ("Rails Testing")
|
27
|
+
# Remember to add it BEFORE the request
|
28
|
+
#
|
29
|
+
# Example:
|
30
|
+
#
|
31
|
+
# it "should render the normal_view view on mobile request" do
|
32
|
+
# reset_test_request_agent
|
33
|
+
# get :index
|
34
|
+
# response.should render_template(:normal_view)
|
35
|
+
# end
|
36
|
+
def reset_test_request_agent
|
37
|
+
request.user_agent = "Rails Testing"
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Mobylette
|
2
|
+
module Helmet
|
3
|
+
|
4
|
+
# Forces the helper to think that all requests come
|
5
|
+
# from a mobile device
|
6
|
+
module Faker
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
cattr_accessor :is_mobile_request
|
11
|
+
end
|
12
|
+
|
13
|
+
def is_mobile_request?
|
14
|
+
is_mobile_request
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Mobylette
|
2
|
+
module Helmet
|
3
|
+
# This module is only for testing the view's helpers of the module
|
4
|
+
# it simulates the Helmet helpers and the controller methods
|
5
|
+
# that are necessary for the tests
|
6
|
+
module Helpers
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
cattr_accessor :user_agent
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
def force_mobile_request_agent
|
15
|
+
insert_faker
|
16
|
+
ActionController::Base.is_mobile_request = true
|
17
|
+
end
|
18
|
+
|
19
|
+
def reset_test_request_agent
|
20
|
+
insert_faker
|
21
|
+
ActionController::Base.is_mobile_request = false
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def insert_faker
|
27
|
+
return if ActionController::Base.included_modules.include?(Mobylette::Helmet::Faker)
|
28
|
+
ActionController::Base.send(:include, Mobylette::Helmet::Faker)
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module Mobylette
|
2
|
+
class Railtie < ::Rails::Railtie
|
3
|
+
initializer :mobylette do
|
4
|
+
ActiveSupport.on_load(:action_controller) do
|
5
|
+
::ActionController::Base.send(:include, Mobylette::Controllers::RespondToMobileRequests)
|
6
|
+
::ActionController::Base.helper Mobylette::Controllers::Helpers
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActionController::Base do
|
4
|
+
|
5
|
+
it "ActionController::Base class should respond to the respond_to_mobile_requests" do
|
6
|
+
ActionController::Base.respond_to?(:respond_to_mobile_requests).should be_true
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should have the :is_mobile_request? method" do
|
10
|
+
# works on ruby 1.9.2, but not on 1.8.7:
|
11
|
+
#@controller.private_methods.include?(:is_mobile_request?).should be_true
|
12
|
+
|
13
|
+
@controller.send(:is_mobile_request?).should be_nil
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DefaultFallbackController do
|
4
|
+
render_views
|
5
|
+
|
6
|
+
it "should find the correct view when it exists" do
|
7
|
+
force_mobile_request_agent
|
8
|
+
get :index
|
9
|
+
response.should render_template(:index)
|
10
|
+
response.body.should contain("MOBILE VIEW")
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should find the correct view when it exists" do
|
14
|
+
reset_test_request_agent
|
15
|
+
get :index
|
16
|
+
response.should render_template(:index)
|
17
|
+
response.body.should contain("HTML VIEW")
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
it "should fall back to the default request format when it doesnt" do
|
22
|
+
force_mobile_request_agent
|
23
|
+
get :test
|
24
|
+
response.should render_template(:test)
|
25
|
+
response.body.should contain("HTML VIEW")
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should fall back to the default request format when it doesnt (js)" do
|
29
|
+
force_mobile_request_agent
|
30
|
+
get :test, :format => :js
|
31
|
+
response.should render_template(:test)
|
32
|
+
response.body.should contain("JS VIEW")
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
end
|