mobile_view 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,8 +1,7 @@
1
1
  .bundle/
2
2
  log/*.log
3
3
  pkg/
4
- test/dummy/db/*.sqlite3
5
- test/dummy/log/*.log
6
- test/dummy/tmp/
7
- test/dummy/.sass-cache
4
+ spec/dummy/db/*.sqlite3
8
5
  spec/dummy/log/*.log
6
+ spec/dummy/tmp/
7
+ spec/dummy/.sass-cache
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mobile_view (0.1.0)
4
+ mobile_view (0.2.0)
5
5
  rack-mobile-detect (~> 0.4.0)
6
6
  rails (~> 3.2.0)
7
7
 
@@ -38,19 +38,23 @@ GEM
38
38
  addressable (2.3.2)
39
39
  arel (3.0.2)
40
40
  builder (3.0.4)
41
- capybara (2.0.1)
41
+ capybara (1.1.3)
42
42
  mime-types (>= 1.16)
43
43
  nokogiri (>= 1.3.3)
44
44
  rack (>= 1.0.0)
45
45
  rack-test (>= 0.5.4)
46
46
  selenium-webdriver (~> 2.0)
47
- xpath (~> 1.0.0)
47
+ xpath (~> 0.1.4)
48
48
  childprocess (0.3.6)
49
49
  ffi (~> 1.0, >= 1.0.6)
50
50
  diff-lcs (1.1.3)
51
51
  erubis (2.7.0)
52
+ eventmachine (1.0.0)
53
+ faye-websocket (0.4.6)
54
+ eventmachine (>= 0.12.0)
52
55
  ffi (1.2.0)
53
56
  hike (1.2.1)
57
+ http_parser.rb (0.5.3)
54
58
  i18n (0.6.1)
55
59
  journey (1.0.4)
56
60
  jquery-rails (2.1.4)
@@ -67,6 +71,12 @@ GEM
67
71
  mime-types (1.19)
68
72
  multi_json (1.5.0)
69
73
  nokogiri (1.5.6)
74
+ poltergeist (1.0.2)
75
+ capybara (~> 1.1)
76
+ childprocess (~> 0.3)
77
+ faye-websocket (~> 0.4, >= 0.4.4)
78
+ http_parser.rb (~> 0.5.3)
79
+ multi_json (~> 1.0)
70
80
  polyglot (0.3.3)
71
81
  rack (1.4.1)
72
82
  rack-cache (1.2)
@@ -124,14 +134,15 @@ GEM
124
134
  polyglot (>= 0.3.1)
125
135
  tzinfo (0.3.35)
126
136
  websocket (1.0.6)
127
- xpath (1.0.0)
137
+ xpath (0.1.4)
128
138
  nokogiri (~> 1.3)
129
139
 
130
140
  PLATFORMS
131
141
  ruby
132
142
 
133
143
  DEPENDENCIES
134
- capybara
144
+ capybara (~> 1.1.0)
135
145
  jquery-rails
136
146
  mobile_view!
147
+ poltergeist
137
148
  rspec-rails
data/README.markdown CHANGED
@@ -1,24 +1,28 @@
1
1
  # MobileView
2
2
 
3
- View template for mobile devices made easy.
3
+ Rails view template for mobile devices made easy.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
- gem "mobile_view", "~> 0.1.0"
9
+ gem "mobile_view", "~> 0.2.0"
10
10
 
11
11
  And then execute:
12
12
 
13
13
  $ bundle
14
14
 
15
+ ## Dependency
16
+
17
+ * Rails 3.2.x
18
+
15
19
  ## Usage
16
20
 
17
21
  ### `action.mobile.html.erb` View Template
18
22
 
19
23
  #### Scenario
20
24
 
21
- You have a view template for `posts#show`, with many DOM elements and JavaScripts, which is good for desktop, but painful for mobile devices. You're tired of `<%= render_something if mobile_device? %>` conditional hell. You want to make it mobile-friendly, with a view that is mostly different form scratch.
25
+ You have a view template for `posts#show`, with many DOM elements and JavaScripts, which is good for desktop, but painful for mobile devices. You're tired of `<%= render_something if mobile? %>` conditional hell. You want to make it mobile-friendly, with a view that is mostly different form scratch.
22
26
 
23
27
  #### Solution
24
28
 
@@ -30,9 +34,41 @@ It also works for partial views. When a mobile version of partial view is not av
30
34
 
31
35
  It also works for layout view. So you can now use `views/layouts/application.mobile.html.erb` for mobile devices.
32
36
 
33
- ### `mobile_device?` Helper
37
+ ### `mobile?` Helper
38
+
39
+ Still want to detect mobile device in Controller and View? Use `mobile?` helper. It returns `true` if it thinks the browser is a mobile device, `false` otherwise.
40
+
41
+ To use `mobile?` helper in a mounted engine, for example, [Rails Cell](https://github.com/apotonick/cells), simply include the `MobileView::ControllerAdditions` module:
42
+
43
+ ```ruby
44
+ class PostCell < Cell::Rails
45
+ include MobileView::ControllerAdditions
46
+ end
47
+ ```
48
+
49
+ ## Cookie-based Mobile View Switching
50
+
51
+ By setting `mobile` cookie, you can force it to load mobile views. This is helpful when debugging the app in desktop browsers, or allowing user to switch to mobile version manually.
52
+
53
+ Example:
34
54
 
35
- Still want to detect mobile device in Controller and View? Use `mobile_device?` helper. It returns `true` if it thinks the browser is a mobile device, `false` otherwise.
55
+ ```ruby
56
+ class AppliactionController < ActionController::Base
57
+ before_filter :manual_mobile_switching
58
+
59
+ protected
60
+ # Detects `_mobile_view` parameter.
61
+ # If it is any value other than 0, turn on mobile view,
62
+ # otherwise, turn off mobile view.
63
+ def manual_mobile_switching
64
+ if params[:_mobile_view] != "0"
65
+ cookies["mobile"] = true # or anything other than falsy value
66
+ else
67
+ cookies.delete "mobile" # remove `mobile' cookie
68
+ end
69
+ end
70
+ end
71
+ ```
36
72
 
37
73
  ## Known Issues
38
74
 
@@ -46,6 +82,22 @@ According to the algorithm of [Rack::MobileDetect](https://github.com/talison/ra
46
82
  * [#397 Action View Walkthrough (pro) - RailsCasts](http://railscasts.com/episodes/397-action-view-walkthrough)
47
83
  * [Implementing a Rails 3 View Resolver - jkfill blog](http://jkfill.com/2011/03/11/implementing-a-rails-3-view-resolver/)
48
84
 
85
+ ## Changelog
86
+
87
+ ### 0.2.0
88
+
89
+ * Support cookie-based switching
90
+ * Rename helper `mobile_device?` to `mobile?` since it now not only detects mobile device, but also accepts cookie-based switching.
91
+ * Ability to have other mounted engines use `mobile?` helper. (Inspired from [Devise](https://github.com/plataformatec/devise/blob/v2.1.2/lib/devise/controllers/helpers.rb))
92
+
93
+ ### 0.1.0
94
+
95
+ :birthday: First Release
96
+
97
+ * `*.mobile` view template auto-overriding
98
+ * Automatically mobile device detection
99
+ * `mobile_device?` helper for controller and view
100
+
49
101
  ## License
50
102
 
51
103
  The MIT License
@@ -56,4 +108,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
56
108
 
57
109
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
58
110
 
59
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
111
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,20 @@
1
+ module MobileView
2
+ module ControllerAdditions
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ # make mobile? method a view helper too
7
+ helper_method :mobile?
8
+
9
+ # find mobile view templates first, if mobile device is detected.
10
+ before_filter do
11
+ prepend_view_path(MobileView.resolver) if mobile?
12
+ end
13
+ end
14
+
15
+ protected
16
+ def mobile?
17
+ cookies[:mobile].present? || request.headers["X_MOBILE_DEVICE"].present?
18
+ end
19
+ end
20
+ end
@@ -1,4 +1,4 @@
1
- require "mobile_view/controller_helper"
1
+ require "mobile_view/controller_additions"
2
2
  require "rack/mobile-detect"
3
3
 
4
4
  module MobileView
@@ -8,15 +8,9 @@ module MobileView
8
8
  end
9
9
 
10
10
  initializer "mobile_view.controller_methods" do |app|
11
- ActionController::Base.send :include, MobileView::ControllerHelper
12
-
13
- # make mobile_device? method a view helper too
14
- ActionController::Base.send :helper_method, :mobile_device?
15
-
16
- # find mobile view templates first, if mobile device is detected.
17
- ActionController::Base.send :before_filter, Proc.new {
18
- prepend_view_path(MobileView.resolver) if mobile_device?
19
- }
11
+ ActiveSupport.on_load(:action_controller) do
12
+ include MobileView::ControllerAdditions
13
+ end
20
14
  end
21
15
  end
22
16
  end
@@ -1,5 +1,7 @@
1
1
  module MobileView
2
2
  class Resolver < ::ActionView::FileSystemResolver
3
+ # pattern is modified from ActionView
4
+ # https://github.com/rails/rails/blob/v3.2.9/actionpack/lib/action_view/template/resolver.rb#L106
3
5
  MOBILE_PATTERN = ":prefix/:action.mobile{.:locale,}{.:formats,}{.:handlers,}"
4
6
 
5
7
  def initialize(path)
@@ -1,3 +1,3 @@
1
1
  module MobileView
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/mobile_view.gemspec CHANGED
@@ -7,8 +7,8 @@ Gem::Specification.new do |s|
7
7
  s.authors = ["Yu-Cheng Chuang"]
8
8
  s.email = ["ducksteven@gmail.com"]
9
9
  s.homepage = "https://github.com/chitsaou/mobile_view"
10
- s.summary = "View template for mobile devices made easy."
11
- s.description = "Easily specify mobile view template for mobile devices."
10
+ s.summary = "Rails view template for mobile devices made easy."
11
+ s.description = "Easily specify mobile-specific view template for mobile devices in Rails application."
12
12
 
13
13
  s.files = `git ls-files`.split($\)
14
14
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
@@ -17,7 +17,8 @@ Gem::Specification.new do |s|
17
17
  s.add_dependency "rack-mobile-detect", "~> 0.4.0"
18
18
 
19
19
  s.add_development_dependency "rspec-rails"
20
- s.add_development_dependency "capybara"
20
+ s.add_development_dependency "capybara", "~> 1.1.0"
21
+ s.add_development_dependency "poltergeist"
21
22
 
22
23
  s.require_paths = ["lib"]
23
24
  end
@@ -16,9 +16,9 @@ class PagesController < ApplicationController
16
16
  render :layout => "yet-another"
17
17
  end
18
18
 
19
- # This page uses mobile_device? helper to determine the content to be rendered
19
+ # This page uses mobile? helper to determine the content to be rendered
20
20
  def conditional
21
- @text = if mobile_device?
21
+ @text = if mobile?
22
22
  "Hi Mobile!"
23
23
  else
24
24
  "Hi Desktop!"
@@ -1,4 +1,4 @@
1
- <% if mobile_device? %>
1
+ <% if mobile? %>
2
2
  <div id="mobile-device"><%= @text %></div>
3
3
  <% else %>
4
4
  <div id="non-mobile-device"><%= @text %></div>
@@ -4,90 +4,120 @@ require 'spec_helper'
4
4
  IPHONE_USER_AGNET = "Mozilla/5.0 (iPhone; U; CPU iOS 2_0 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/XXXXX Safari/525.20"
5
5
 
6
6
  def use_iphone
7
- # depends on defualt driver Capybara::RackTest::Driver
8
- page.driver.header("USER_AGENT", IPHONE_USER_AGNET)
7
+ # depends on poltergeist
8
+ page.driver.headers = { "User-Agent" => IPHONE_USER_AGNET }
9
+ end
10
+
11
+ def set_mobile_cookie
12
+ # depends on poltergeist
13
+ page.driver.set_cookie "mobile", true
9
14
  end
10
15
 
11
16
  describe "View Template Overriding" do
12
- context "Action view template" do
13
- it "renders mobile template when client is a mobile device" do
17
+ context "when client is mobile device" do
18
+ before :each do
14
19
  use_iphone
20
+ end
15
21
 
22
+ it "renders mobile-specific action view template" do
16
23
  visit "/pages/action_view"
17
24
 
18
25
  page.should have_selector('#mobile-view')
19
26
  end
20
27
 
21
- it "renders default template when client is not a mobile device" do
22
- visit "/pages/action_view"
28
+ it "renders mobile-specific partial view template" do
29
+ visit "/pages/partial_view"
23
30
 
24
- page.should have_selector('#default-view')
31
+ page.should have_selector('#mobile-partial-view')
25
32
  end
26
- end
27
33
 
28
- context "Overriding partial view template" do
29
- it "renders mobile template when client is a mobile device" do
30
- use_iphone
34
+ it "renders mobile-specific layout view template" do
35
+ visit "/pages/layout_template"
31
36
 
32
- visit "/pages/partial_view"
37
+ page.should have_selector('body.mobile')
38
+ end
33
39
 
34
- page.should have_selector('#mobile-partial-view')
40
+ it "renders default view template when there is no mobile-specific overriding" do
41
+ visit "/pages/default"
42
+
43
+ page.should have_selector('#hello-world')
35
44
  end
36
45
 
37
- it "renders default template when client is not a mobile device" do
38
- visit "/pages/partial_view"
46
+ it "renders mobile-specific content" do
47
+ visit "/pages/conditional"
39
48
 
40
- page.should have_selector('#default-partial-view')
49
+ page.should have_selector('#mobile-device')
50
+ page.should have_content('Hi Mobile!')
41
51
  end
42
52
  end
43
53
 
44
- context "Overriding layout view template" do
45
- it "renders mobile layout template when client is a mobile device" do
46
- use_iphone
54
+ context "when client provides mobile cookie" do
55
+ before :each do
56
+ set_mobile_cookie
57
+ end
47
58
 
48
- visit "/pages/layout_template"
59
+ it "renders mobile-specific action view template" do
60
+ visit "/pages/action_view"
49
61
 
50
- page.should have_selector('body.mobile')
62
+ page.should have_selector('#mobile-view')
51
63
  end
52
64
 
53
- it "renders default layout template when client is not a mobile device" do
54
- visit "/pages/layout_template"
65
+ it "renders mobile-specific partial view template" do
66
+ visit "/pages/partial_view"
55
67
 
56
- page.should have_selector('body.desktop')
68
+ page.should have_selector('#mobile-partial-view')
57
69
  end
58
- end
59
70
 
60
- context "Not overriding" do
61
- it "renders default template when client is a mobile device" do
62
- use_iphone
71
+ it "renders mobile-specific layout view template" do
72
+ visit "/pages/layout_template"
73
+
74
+ page.should have_selector('body.mobile')
75
+ end
63
76
 
77
+ it "renders default view template when there is no mobile-specific overriding" do
64
78
  visit "/pages/default"
65
79
 
66
80
  page.should have_selector('#hello-world')
67
81
  end
68
82
 
69
- it "renders default template when client is not a mobile device" do
70
- visit "/pages/default"
83
+ it "renders mobile-specific content" do
84
+ visit "/pages/conditional"
71
85
 
72
- page.should have_selector('#hello-world')
86
+ page.should have_selector('#mobile-device')
87
+ page.should have_content('Hi Mobile!')
73
88
  end
74
89
  end
75
- end
76
90
 
77
- describe "mobile_device? usage" do
78
- it "renders mobile-specific content when client is a mobile device" do
79
- use_iphone
91
+ context "when client is not mobile browser" do
92
+ it "renders default action view template" do
93
+ visit "/pages/action_view"
80
94
 
81
- visit "/pages/conditional"
95
+ page.should have_selector('#default-view')
96
+ end
82
97
 
83
- page.should have_selector('#mobile-device')
84
- page.should have_content('Hi Mobile!')
85
- end
98
+ it "renders default partial view template" do
99
+ visit "/pages/partial_view"
86
100
 
87
- it "renders general content when client is not a mobile device" do
88
- visit "/pages/conditional"
101
+ page.should have_selector('#default-partial-view')
102
+ end
103
+
104
+ it "renders default layout view template" do
105
+ visit "/pages/layout_template"
106
+
107
+ page.should have_selector('body.desktop')
108
+ end
109
+
110
+ it "renders default view template when there is no mobile-specific overriding" do
111
+ visit "/pages/default"
112
+
113
+ page.should have_selector('#hello-world')
114
+ end
89
115
 
90
- page.should have_selector('#non-mobile-device')
91
- page.should have_content('Hi Desktop!')
116
+ it "renders general content" do
117
+ visit "/pages/conditional"
118
+
119
+ page.should have_selector('#non-mobile-device')
120
+ page.should have_content('Hi Desktop!')
121
+ end
92
122
  end
93
- end
123
+ end
data/spec/spec_helper.rb CHANGED
@@ -9,6 +9,9 @@ require 'capybara/rspec'
9
9
 
10
10
  Capybara.app = Dummy::Application
11
11
 
12
+ require 'capybara/poltergeist'
13
+ Capybara.default_driver = :poltergeist
14
+
12
15
  # Requires supporting ruby files with custom matchers and macros, etc,
13
16
  # in spec/support/ and its subdirectories.
14
17
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobile_view
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -61,6 +61,22 @@ dependencies:
61
61
  version: '0'
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: capybara
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.1.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.1.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: poltergeist
64
80
  requirement: !ruby/object:Gem::Requirement
65
81
  none: false
66
82
  requirements:
@@ -75,7 +91,8 @@ dependencies:
75
91
  - - ! '>='
76
92
  - !ruby/object:Gem::Version
77
93
  version: '0'
78
- description: Easily specify mobile view template for mobile devices.
94
+ description: Easily specify mobile-specific view template for mobile devices in Rails
95
+ application.
79
96
  email:
80
97
  - ducksteven@gmail.com
81
98
  executables: []
@@ -89,7 +106,7 @@ files:
89
106
  - README.markdown
90
107
  - Rakefile
91
108
  - lib/mobile_view.rb
92
- - lib/mobile_view/controller_helper.rb
109
+ - lib/mobile_view/controller_additions.rb
93
110
  - lib/mobile_view/engine.rb
94
111
  - lib/mobile_view/resolver.rb
95
112
  - lib/mobile_view/version.rb
@@ -153,7 +170,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
153
170
  version: '0'
154
171
  segments:
155
172
  - 0
156
- hash: -1475394104596427624
173
+ hash: 232344584105567251
157
174
  required_rubygems_version: !ruby/object:Gem::Requirement
158
175
  none: false
159
176
  requirements:
@@ -162,13 +179,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
179
  version: '0'
163
180
  segments:
164
181
  - 0
165
- hash: -1475394104596427624
182
+ hash: 232344584105567251
166
183
  requirements: []
167
184
  rubyforge_project:
168
185
  rubygems_version: 1.8.24
169
186
  signing_key:
170
187
  specification_version: 3
171
- summary: View template for mobile devices made easy.
188
+ summary: Rails view template for mobile devices made easy.
172
189
  test_files:
173
190
  - spec/dummy/README.rdoc
174
191
  - spec/dummy/Rakefile
@@ -1,8 +0,0 @@
1
- module MobileView
2
- module ControllerHelper
3
- protected
4
- def mobile_device?
5
- request.headers["X_MOBILE_DEVICE"].present?
6
- end
7
- end
8
- end