mobylette 1.6.0 → 2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. data/README.rdoc +35 -22
  2. data/Rakefile +0 -19
  3. data/lib/mobylette.rb +1 -4
  4. data/lib/mobylette/helmet.rb +0 -2
  5. data/lib/mobylette/railtie.rb +0 -6
  6. data/lib/mobylette/respond_to_mobile_requests.rb +135 -0
  7. data/lib/mobylette/version.rb +1 -1
  8. data/spec/controllers/application_controller_spec.rb +37 -0
  9. data/spec/dummy/app/controllers/application_controller.rb +0 -1
  10. data/spec/dummy/log/development.log +116 -0
  11. data/spec/dummy/log/test.log +21 -585
  12. data/spec/dummy/tmp/cache/assets/C82/3E0/sprockets%2F48eb4ac616538f8f36870451073315da +14 -0
  13. data/spec/dummy/tmp/cache/assets/CE4/1C0/sprockets%2F18d93e1533d585b2f64ec90b33176ac9 +0 -0
  14. data/spec/dummy/tmp/cache/assets/CED/A70/sprockets%2F621683d8791730a9cc7ce45e17a7e46d +0 -0
  15. data/spec/dummy/tmp/cache/assets/CFC/200/sprockets%2F716f89d85d4855706b8a9abec1969c62 +16 -0
  16. data/spec/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
  17. data/spec/dummy/tmp/cache/assets/D54/ED0/sprockets%2F71c9fa01091d432b131da3bb73faf3d4 +16 -0
  18. data/spec/dummy/tmp/cache/assets/D84/210/sprockets%2Fabd0103ccec2b428ac62c94e4c40b384 +14 -0
  19. data/spec/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
  20. data/spec/lib/respond_to_mobile_requests_spec.rb +253 -0
  21. data/spec/spec_helper.rb +3 -0
  22. metadata +45 -107
  23. data/lib/mobylette/controllers/helpers.rb +0 -53
  24. data/lib/mobylette/controllers/respond_to_mobile_requests.rb +0 -125
  25. data/lib/mobylette/helmet/faker.rb +0 -19
  26. data/lib/mobylette/helmet/helpers.rb +0 -36
  27. data/spec/controllers/actioncontroller_base_spec.rb +0 -24
  28. data/spec/controllers/default_fallback_controller_spec.rb +0 -37
  29. data/spec/controllers/desktop_only_controller_spec.rb +0 -18
  30. data/spec/controllers/force_fallback_controller_spec.rb +0 -46
  31. data/spec/controllers/home_controller_spec.rb +0 -145
  32. data/spec/controllers/no_fallback_controller_spec.rb +0 -58
  33. data/spec/controllers/skip_xhr_request_controller_spec.rb +0 -19
  34. data/spec/dummy/app/controllers/default_fallback_controller.rb +0 -13
  35. data/spec/dummy/app/controllers/desktop_only_controller.rb +0 -2
  36. data/spec/dummy/app/controllers/fallbacks_controller.rb +0 -7
  37. data/spec/dummy/app/controllers/force_fallback_controller.rb +0 -13
  38. data/spec/dummy/app/controllers/home_controller.rb +0 -20
  39. data/spec/dummy/app/controllers/no_fallback_controller.rb +0 -13
  40. data/spec/dummy/app/controllers/skip_xhr_request_controller.rb +0 -7
  41. data/spec/dummy/app/controllers/testing_controller.rb +0 -9
  42. data/spec/dummy/app/views/default_fallback/index.html.erb +0 -1
  43. data/spec/dummy/app/views/default_fallback/index.js.erb +0 -1
  44. data/spec/dummy/app/views/default_fallback/index.mobile.erb +0 -1
  45. data/spec/dummy/app/views/default_fallback/test.html.erb +0 -1
  46. data/spec/dummy/app/views/default_fallback/test.js.erb +0 -1
  47. data/spec/dummy/app/views/fallbacks/index.html.erb +0 -2
  48. data/spec/dummy/app/views/fallbacks/index.js.erb +0 -1
  49. data/spec/dummy/app/views/fallbacks/index.mobile.erb +0 -0
  50. data/spec/dummy/app/views/fallbacks/index.xml.erb +0 -0
  51. data/spec/dummy/app/views/fallbacks/no_mobile.html.erb +0 -0
  52. data/spec/dummy/app/views/force_fallback/index.html.erb +0 -1
  53. data/spec/dummy/app/views/force_fallback/index.js.erb +0 -1
  54. data/spec/dummy/app/views/force_fallback/index.mobile.erb +0 -1
  55. data/spec/dummy/app/views/force_fallback/test.html.erb +0 -1
  56. data/spec/dummy/app/views/force_fallback/test.js.erb +0 -1
  57. data/spec/dummy/app/views/home/desktop.html.erb +0 -3
  58. data/spec/dummy/app/views/home/index.html.erb +0 -9
  59. data/spec/dummy/app/views/home/index.js.erb +0 -1
  60. data/spec/dummy/app/views/home/index.mobile.erb +0 -9
  61. data/spec/dummy/app/views/home/mobile.mobile.erb +0 -3
  62. data/spec/dummy/app/views/home/no_mobile_view.html.erb +0 -3
  63. data/spec/dummy/app/views/ignore_mobile_path/index.mobile.erb +0 -1
  64. data/spec/dummy/app/views/no_fallback/index.html.erb +0 -1
  65. data/spec/dummy/app/views/no_fallback/index.js.erb +0 -1
  66. data/spec/dummy/app/views/no_fallback/index.mobile.erb +0 -1
  67. data/spec/dummy/app/views/no_fallback/test.html.erb +0 -1
  68. data/spec/dummy/app/views/no_fallback/test.js.erb +0 -1
  69. data/spec/dummy/app/views/skip_xhr_request/index.html.erb +0 -9
  70. data/spec/dummy/app/views/skip_xhr_request/index.js.erb +0 -1
  71. data/spec/dummy/app/views/skip_xhr_request/index.mobile.erb +0 -9
  72. data/spec/dummy/app/views/view_path/index.mobile.erb +0 -1
  73. data/spec/helpers/mobylette_helpers_spec.rb +0 -59
@@ -1,53 +0,0 @@
1
- module Mobylette
2
- module Controllers
3
-
4
- # Mobylette::Controllers::Helpers include few methods to
5
- # include different css/js files for the mobile and for
6
- # the normal version of your layout
7
- #
8
- # Personal notes:
9
- # * 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
- # * Also I'm not sure it's the case of using 'is_mobile_request?'
13
- # or 'is_mobile_view?' check here.'
14
- module Helpers
15
- extend ActiveSupport::Concern
16
-
17
- # Adds a "_mobile" sulfix to the files you include (when the request is mobile)
18
- def mobylette_stylesheet_link_tag(*sources)
19
- options = sources.extract_options!
20
- if is_mobile_request?
21
- stylesheet_link_tag(*sulfix_mobile_assets(sources, :css).insert(-1, options))
22
- else
23
- stylesheet_link_tag(*sources.insert(-1, options))
24
- end
25
- end
26
-
27
- # Adds a "_mobile" sulfix to the files you include (when the request is mobile)
28
- def mobylette_javascript_include_tag(*sources)
29
- options = sources.extract_options!
30
- if is_mobile_request?
31
- javascript_include_tag(*sulfix_mobile_assets(sources, :js).insert(-1, options))
32
- else
33
- javascript_include_tag(*sources.insert(-1, options))
34
- end
35
- end
36
-
37
- private
38
-
39
- # Anex the "_mobile" sulfix to each string in the array,
40
- # before the .#{extension}, if it exists
41
- def sulfix_mobile_assets(sources, extension)
42
- sources.map do |source|
43
- if source =~ /.#{extension}/
44
- "#{source.split(/.#{extension}$/)[0]}_mobile.#{extension}"
45
- else
46
- "#{source}_mobile"
47
- end
48
- end
49
- end
50
- end
51
-
52
- end
53
- end
@@ -1,125 +0,0 @@
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
- helper_method :is_mobile_view?
14
-
15
- # List of mobile agents, from mobile_fu (https://github.com/brendanlim/mobile-fu)
16
- MOBILE_USER_AGENTS = 'palm|blackberry|nokia|phone|midp|mobi|symbian|chtml|ericsson|minimo|' +
17
- 'audiovox|motorola|samsung|telit|upg1|windows ce|ucweb|astel|plucker|' +
18
- 'x320|x240|j2me|sgh|portable|sprint|docomo|kddi|softbank|android|mmp|' +
19
- 'pdxgw|netfront|xiino|vodafone|portalmmm|sagem|mot-|sie-|ipod|up\\.b|' +
20
- 'webos|amoi|novarra|cdm|alcatel|pocket|iphone|mobileexplorer|mobile'
21
- end
22
-
23
- module ClassMethods
24
-
25
- # This method enables the controller do handle mobile requests
26
- #
27
- # You must add this to every controller you want to respond differently to mobile devices,
28
- # or make it application wide calling it from the ApplicationController
29
- #
30
- # Options:
31
- # * fall_back: :html
32
- # You may pass a fall_back option to the method, it will force the render
33
- # to look for that other format, in case there is not a .mobile file for the view.
34
- # By default, it will fall back to the format of the original request.
35
- # If you don't want fall back at all, pass fall_back: false
36
- # * skip_xhr_requests: true/false
37
- # By default this is set to true. When a xhr request enters in, it will skip the
38
- # mobile verification. This will let your ajax calls to work as intended.
39
- # You may disable this (actually you will have to) if you are using JQuery Mobile, or
40
- # other js framework that uses ajax. To disable, set skip_xhr_requests: false
41
- def respond_to_mobile_requests(options = {})
42
- return if self.included_modules.include?(Mobylette::Controllers::RespondToMobileRequestsMethods)
43
-
44
- options.reverse_merge!({
45
- skip_xhr_requests: true
46
- })
47
-
48
- cattr_accessor :mobylette_options
49
- # works on 1.9, but not on 1.8
50
- #valid_options = [:fall_back, :skip_xhr_requests]
51
- #self.mobylette_options = options.reject {|option| !valid_options.include?(option)}
52
- self.mobylette_options = options
53
-
54
- self.send(:include, Mobylette::Controllers::RespondToMobileRequestsMethods)
55
- end
56
- end
57
-
58
- private
59
-
60
- # :doc:
61
- # This helper returns exclusively if the request's user_aget is from a mobile
62
- # device or not.
63
- def is_mobile_request?
64
- request.user_agent.to_s.downcase =~ /#{MOBILE_USER_AGENTS}/
65
- end
66
-
67
- # :doc:
68
- # This helper returns exclusively if the current format is mobile or not
69
- def is_mobile_view?
70
- true if (request.format.to_s == "mobile") or (params[:format] == "mobile")
71
- end
72
- end
73
-
74
- # RespondToMobileRequestsMethods is included by respond_to_mobile_requests
75
- #
76
- # This will check if the request is from a mobile device and change
77
- # the request format to :mobile
78
- module RespondToMobileRequestsMethods
79
- extend ActiveSupport::Concern
80
-
81
- included do
82
- before_filter :handle_mobile
83
- end
84
-
85
- private
86
-
87
- # Returns true if this request should be treated as a mobile request
88
- def respond_as_mobile?
89
- processing_xhr_requests? and skip_mobile_param_not_present? and (force_mobile_by_session? or is_mobile_request? or (params[:format] == 'mobile'))
90
- end
91
-
92
- # Returns true if the visitor has de force_mobile session
93
- def force_mobile_by_session?
94
- session[:mobylette_override] == :force_mobile
95
- end
96
-
97
- # Returns true when ?skip_mobile=true is not passed to the request
98
- def skip_mobile_param_not_present?
99
- params[:skip_mobile] != 'true'
100
- end
101
-
102
- # Returns true only if treating XHR requests (when skip_xhr_requests are set to false) or
103
- # or when this is a non xhr request
104
- def processing_xhr_requests?
105
- not self.mobylette_options[:skip_xhr_requests] && request.xhr?
106
- end
107
-
108
- # :doc:
109
- # Changes the request.form to :mobile, when the request is from
110
- # a mobile device
111
- def handle_mobile
112
- return if session[:mobylette_override] == :ignore_mobile
113
- if respond_as_mobile?
114
-
115
- original_format = request.format.to_sym
116
- request.format = :mobile
117
- if self.mobylette_options[:fall_back] != false
118
- request.formats << Mime::Type.new(self.mobylette_options[:fall_back] || original_format)
119
- end
120
- end
121
- end
122
-
123
- end
124
- end
125
- end
@@ -1,19 +0,0 @@
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
@@ -1,36 +0,0 @@
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
- # Force the request to be a mobile request
14
- def force_mobile_request_agent
15
- insert_faker
16
- ActionController::Base.is_mobile_request = true
17
- end
18
-
19
- # Reset the request mobile agent of the request
20
- def reset_test_request_agent
21
- insert_faker
22
- ActionController::Base.is_mobile_request = false
23
- end
24
-
25
- private
26
-
27
- # includes the faker module for faking mobile requests
28
- def insert_faker
29
- return if ActionController::Base.included_modules.include?(Mobylette::Helmet::Faker)
30
- ActionController::Base.send(:include, Mobylette::Helmet::Faker)
31
- end
32
-
33
-
34
- end
35
- end
36
- end
@@ -1,24 +0,0 @@
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
- it "should have the :is_mobile_request? method" do
17
- # works on ruby 1.9.2, but not on 1.8.7:
18
- #@controller.private_methods.include?(:is_mobile_request?).should be_true
19
-
20
- @controller.send(:is_mobile_view?).should be_nil
21
- end
22
-
23
-
24
- end
@@ -1,37 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe DefaultFallbackController do
4
- # This controller calls respond_to_mobile_requests with no params
5
- # Index action has views for html, js and mobile
6
- # Test action has only html and js
7
-
8
- render_views
9
-
10
- it "should find the correct view when it exists" do
11
- force_mobile_request_agent
12
- get :index
13
- response.should render_template(:index)
14
- response.body.should contain("MOBILE VIEW")
15
- end
16
-
17
- it "should find the correct view when it exists" do
18
- reset_test_request_agent
19
- get :index
20
- response.should render_template(:index)
21
- response.body.should contain("HTML VIEW")
22
- end
23
-
24
- it "should fall back to the default request format when it doesnt" do
25
- force_mobile_request_agent
26
- get :test
27
- response.should render_template(:test)
28
- response.body.should contain("HTML VIEW")
29
- end
30
-
31
- it "should fall back to the default request format when it doesnt (js)" do
32
- force_mobile_request_agent
33
- get :test, :format => :js
34
- response.should render_template(:test)
35
- response.body.should contain("JS VIEW")
36
- end
37
- end
@@ -1,18 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe DesktopOnlyController do
4
- # this controller has no actions
5
- # and do not call respond_to_mobile_requests
6
-
7
- it "should not have the :handle_mobile method" do
8
- @controller.private_methods.include?(:handle_mobile).should_not be_true
9
- end
10
-
11
- it "should have the :is_mobile_request? method" do
12
- # Works on ruby 1.9.2 but not on 1.8.7:
13
- #@controller.private_methods.include?(:is_mobile_request?).should be_true
14
-
15
- @controller.send(:is_mobile_request?).should be_nil
16
- end
17
-
18
- end
@@ -1,46 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe ForceFallbackController do
4
- # this controller calls respond_to_mobile_requests :fall_back => :js
5
- # forcing fallback to be javascript
6
- # index action has html, js and mobile views
7
- # test action has html and js only
8
-
9
- render_views
10
-
11
- it "should find the correct view (mobile) when it exists" do
12
- force_mobile_request_agent
13
- get :index
14
- response.should render_template(:index)
15
- response.body.should contain("MOBILE VIEW")
16
- end
17
-
18
- it "should find the correct view (html) when it exists" do
19
- reset_test_request_agent
20
- get :index
21
- response.should render_template(:index)
22
- response.body.should contain("HTML VIEW")
23
- end
24
-
25
- it "should fall back to the JS format when it doesnt have mobile" do
26
- force_mobile_request_agent
27
- get :test
28
- response.should render_template(:test)
29
- response.body.should contain("JS VIEW")
30
- end
31
-
32
- it "should fall back to the JS format when it doesnt have mobile" do
33
- force_mobile_request_agent
34
- get :test, :format => :html
35
- response.should render_template(:test)
36
- response.body.should contain("JS VIEW")
37
- end
38
-
39
- it "should fall back to the JS format when it doesnt (js)" do
40
- force_mobile_request_agent
41
- get :test, :format => :js
42
- response.should render_template(:test)
43
- response.body.should contain("JS VIEW")
44
- end
45
-
46
- end
@@ -1,145 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe HomeController do
4
- # This controller calls respond_to_mobile_requests with no params
5
- # no_mobile_view action has only html
6
- # mobile action has only mobile
7
- # index action has html, js and mobile
8
- # desktop action has only html
9
- render_views
10
-
11
- it "should have the :handle_mobile method" do
12
- # Works on ruby 1.9.2 but not on 1.8.7
13
- # @controller.private_methods.include?(:handle_mobile).should be_true
14
- # this is a hack, not perfect, but if it didnt have the method it would
15
- # throw an error, and the test would fail
16
- @controller.send(:handle_mobile).should be_nil
17
- end
18
-
19
- it "should render the index view" do
20
- get :index
21
- response.should render_template(:index)
22
- response.body.should contain("this is the html view")
23
- end
24
-
25
- describe "is_mobile_request? and force_mobile_request_agent/reset_test_request_agent" do
26
- it "should have the Rails Testing request type by default" do
27
- get :index
28
- request.user_agent.should == "Rails Testing"
29
- @controller.send(:is_mobile_request?).should be_false
30
- end
31
-
32
- it "should, on a mobile request, have 'is_mobile_request?' == true" do
33
- force_mobile_request_agent("Android")
34
- get :index
35
- request.user_agent.should == "Android"
36
- @controller.send(:is_mobile_request?).should be_true
37
- end
38
-
39
- it "should, on a mobile request, render the mobile type of view" do
40
- force_mobile_request_agent
41
- get :index
42
- @controller.send(:is_mobile_request?).should be_true
43
- response.body.should contain("this is the mobile view")
44
- end
45
-
46
- it "should, on reset_test_request_agent call, reset the user_agent to default (Rails Testing)" do
47
- force_mobile_request_agent("Iphone")
48
- reset_test_request_agent
49
- get :index
50
- @controller.send(:is_mobile_request?).should be_false
51
- request.user_agent.should == "Rails Testing"
52
- end
53
- end
54
-
55
- describe "Output Rendering" do
56
- it "should display THIS A MOBILE DEVICE on index from mobile" do
57
- force_mobile_request_agent("Android")
58
- get :index
59
- response.body.should contain("THIS A MOBILE DEVICE")
60
- end
61
-
62
- it "should not display THIS IS NOT A MOBILE DEVICE on index from mobile" do
63
- force_mobile_request_agent("Android")
64
- get :index
65
- response.body.should_not contain("THIS IS NOT A MOBILE DEVICE")
66
- end
67
-
68
- it "should not display THIS A MOBILE DEVICE on index from non mobile" do
69
- reset_test_request_agent
70
- get :index
71
- response.body.should_not contain("THIS A MOBILE DEVICE")
72
- end
73
-
74
- it "should display THIS IS NOT A MOBILE DEVICE on index from non mobile" do
75
- reset_test_request_agent
76
- get :index
77
- response.body.should contain("THIS IS NOT A MOBILE DEVICE")
78
- end
79
- end
80
-
81
- describe "respond_to with different views for different request types" do
82
- it "should render desktop view on non mobile request" do
83
- reset_test_request_agent
84
- get :respond_to_test
85
- response.should render_template(:desktop)
86
- end
87
-
88
- it "should render mobile view on mobile request" do
89
- force_mobile_request_agent("Android")
90
- get :respond_to_test
91
- response.should render_template(:mobile)
92
- end
93
-
94
- it "should render mobile view when format => 'mobile' is passed" do
95
- get :respond_to_test, :format => "mobile"
96
- response.should render_template(:mobile)
97
- end
98
- end
99
-
100
- #######################################################
101
- # Testing XHR requests
102
- describe "XHR Requests" do
103
- it "should not use mobile format for xhr requests by default" do
104
- force_mobile_request_agent("Android")
105
- xhr :get, :index
106
- response.should render_template(:index)
107
- response.body.should contain("AJAX VIEW")
108
- end
109
- end
110
-
111
- #######################################################
112
- # Testing Session Override
113
- describe "Session Override" do
114
- it "should force mobile view if session says it so" do
115
- reset_test_request_agent
116
- set_session_override(:force_mobile)
117
- get :index
118
- response.should render_template(:index)
119
- response.should contain("this is the mobile view")
120
- response.should contain("THIS IS NOT A MOBILE DEVICE")
121
- end
122
-
123
- it "should ignore mobile view processing if session says it so" do
124
- force_mobile_request_agent("Android")
125
- set_session_override(:ignore_mobile)
126
- get :index
127
- response.should render_template(:index)
128
- response.should contain("this is the html view")
129
- response.should contain("THIS A MOBILE DEVICE")
130
- end
131
- end
132
-
133
- ###################################################
134
- # Param forcing
135
- describe "Forcing to skip mobile by param" do
136
- it "should not render mobile view if skip_mobile param is present and set to true" do
137
- force_mobile_request_agent
138
- get :index, :skip_mobile => 'true'
139
- response.should render_template(:index)
140
- response.should contain("this is the html view")
141
- response.should contain("THIS A MOBILE DEVICE")
142
- end
143
- end
144
-
145
- end