homura 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +8 -0
- data/lib/homura/mobile_detection.rb +66 -0
- data/lib/homura/set_locale.rb +8 -4
- data/lib/homura/version.rb +1 -1
- metadata +5 -4
data/README.md
CHANGED
@@ -42,6 +42,14 @@ In application controller:
|
|
42
42
|
Your app will set locale via params/cookies/http-accept-language before any
|
43
43
|
requests.
|
44
44
|
|
45
|
+
### Mobile Detection
|
46
|
+
|
47
|
+
In application controller:
|
48
|
+
|
49
|
+
include Homura::MobileDetection
|
50
|
+
|
51
|
+
Then put your mobile views in `app/mobile_views`.
|
52
|
+
|
45
53
|
## Contributing
|
46
54
|
|
47
55
|
1. Fork it
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# This module will add mobile device detection support.
|
2
|
+
#
|
3
|
+
# It makes mobile and desktop to share the same controller, the same url,
|
4
|
+
# but different templates.
|
5
|
+
#
|
6
|
+
# It will store current status in cookie[mobile].
|
7
|
+
#
|
8
|
+
# Client may pass mobile=0|1 to any url to modify the cookie.
|
9
|
+
#
|
10
|
+
# Server will serve templates in app/mobile_views rather than app/views
|
11
|
+
# if requested templates available.
|
12
|
+
#
|
13
|
+
# References:
|
14
|
+
# http://erniemiller.org/2011/01/05/mobile-devices-and-rails-maintaining-your-sanity/
|
15
|
+
# http://johannburkard.de/blog/www/mobile/simple-mobile-phone-detection.html
|
16
|
+
module Homura
|
17
|
+
module MobileDetection
|
18
|
+
MOBILE_USER_AGENTS = /(IEMobile|Windows CE|NetFront|PlayStation|
|
19
|
+
PLAYSTATION|like Mac OS X|MIDP|UP\.Browser|
|
20
|
+
Symbian|Nintendo|Android)/x
|
21
|
+
|
22
|
+
def self.included(base)
|
23
|
+
base.class_eval do
|
24
|
+
before_filter :set_mobile_preferences
|
25
|
+
before_filter :prepend_view_path_if_mobile
|
26
|
+
|
27
|
+
helper_method :mobile_request?
|
28
|
+
helper_method :mobile_browser?
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def set_mobile_preferences
|
33
|
+
case params[:mobile]
|
34
|
+
when '1'
|
35
|
+
cookies['mobile'] = '1'
|
36
|
+
redirect_to_current_page_without_mobile_param
|
37
|
+
when '0'
|
38
|
+
cookies['mobile'] = '0'
|
39
|
+
redirect_to_current_page_without_mobile_param
|
40
|
+
else
|
41
|
+
cookies['mobile'] ||= mobile_browser? ? '1' : '0'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def prepend_view_path_if_mobile
|
46
|
+
if mobile_request?
|
47
|
+
prepend_view_path Rails.root + 'app/mobile_views'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def redirect_to_current_page_without_mobile_param
|
52
|
+
path = request.fullpath.gsub(/mobile=[01]&?/, '').chomp('?')
|
53
|
+
full_url = request.protocol + request.host_with_port + path
|
54
|
+
redirect_to full_url
|
55
|
+
end
|
56
|
+
|
57
|
+
def mobile_request?
|
58
|
+
cookies['mobile'] == '1'
|
59
|
+
end
|
60
|
+
|
61
|
+
def mobile_browser?
|
62
|
+
request.env['HTTP_USER_AGENT'] &&
|
63
|
+
request.env['HTTP_USER_AGENT'][MOBILE_USER_AGENTS]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/lib/homura/set_locale.rb
CHANGED
@@ -10,22 +10,26 @@ module Homura
|
|
10
10
|
@available_locales ||= I18n.available_locales.map(&:to_s)
|
11
11
|
end
|
12
12
|
|
13
|
+
def locale_available?(locale)
|
14
|
+
available_locales.include?(locale)
|
15
|
+
end
|
16
|
+
|
13
17
|
def set_locale
|
14
|
-
if params['locale'] &&
|
18
|
+
if params['locale'] && locale_available?(params['locale'])
|
15
19
|
cookies.permanent['locale'] = locale
|
16
20
|
I18n.locale = locale and return
|
17
21
|
end
|
18
22
|
|
19
|
-
if cookies['locale'] &&
|
23
|
+
if cookies['locale'] && locale_available?(locale)
|
20
24
|
I18n.locale = locale and return
|
21
25
|
end
|
22
26
|
|
23
27
|
http_accept_language = request.headers['HTTP_ACCEPT_LANGUAGE'] || ''
|
24
28
|
preferred_locales = http_accept_language.split(',').map { |l|
|
25
|
-
l.split(';').first
|
29
|
+
l.split(';').first.downcase.gsub(/-[a-z0-9]+$/i) { |x| x.upcase }
|
26
30
|
}
|
27
31
|
I18n.locale = preferred_locales.find { |l|
|
28
|
-
|
32
|
+
locale_available?(l)
|
29
33
|
}
|
30
34
|
end
|
31
35
|
end
|
data/lib/homura/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: homura
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-08-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -147,6 +147,7 @@ files:
|
|
147
147
|
- lib/generators/homura/install/templates/layout.en.yml
|
148
148
|
- lib/homura.rb
|
149
149
|
- lib/homura/engine.rb
|
150
|
+
- lib/homura/mobile_detection.rb
|
150
151
|
- lib/homura/set_locale.rb
|
151
152
|
- lib/homura/version.rb
|
152
153
|
- spec/dummy/.gitignore
|
@@ -198,7 +199,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
198
199
|
version: '0'
|
199
200
|
segments:
|
200
201
|
- 0
|
201
|
-
hash:
|
202
|
+
hash: -206518130249304431
|
202
203
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
203
204
|
none: false
|
204
205
|
requirements:
|
@@ -207,7 +208,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
207
208
|
version: '0'
|
208
209
|
segments:
|
209
210
|
- 0
|
210
|
-
hash:
|
211
|
+
hash: -206518130249304431
|
211
212
|
requirements: []
|
212
213
|
rubyforge_project:
|
213
214
|
rubygems_version: 1.8.23
|