tworgy-rails 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/tworgy/iphoneification.rb +98 -0
- data/lib/tworgy-rails.rb +1 -0
- metadata +3 -2
@@ -0,0 +1,98 @@
|
|
1
|
+
# The following code is copies from the following repository (with a tweak or two)
|
2
|
+
# git://github.com/jameswilding/iphoneification.git
|
3
|
+
|
4
|
+
module IPhoneification
|
5
|
+
|
6
|
+
# Class methods are added to ActionController::Base so you can do:
|
7
|
+
#
|
8
|
+
# class ApplicationController < ActionController::Base
|
9
|
+
# responds_to_iphone
|
10
|
+
# ...
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# Use +responds_to_iphone!+ (with the bang) instead to make your app act
|
14
|
+
# like an Apple fanboy, always serving iPhone views even if your visitors
|
15
|
+
# are using some other browser.
|
16
|
+
module ClassMethods
|
17
|
+
|
18
|
+
# #responds_to_iphone sets up the before-filter which checks for iphone requests
|
19
|
+
# and adjusts the request format accordingly.
|
20
|
+
#
|
21
|
+
# You can then use +respond_to+ in your controllers to serve
|
22
|
+
# iPhone-optimised views:
|
23
|
+
#
|
24
|
+
# def index
|
25
|
+
# @posts = Post.all
|
26
|
+
#
|
27
|
+
# respond_to do |format|
|
28
|
+
# format.html # renders posts/index.html.erb
|
29
|
+
# format.iphone { some_special_code_for_iphone_requests } # renders posts/index.iphone.erb
|
30
|
+
# end
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# Use the extension '.iphone.erb' for your iPhone views and templates.
|
34
|
+
#
|
35
|
+
# ==== Options
|
36
|
+
# Pass in any options appropriate for +before_filter+.
|
37
|
+
def responds_to_iphone(options = {})
|
38
|
+
before_filter :adjust_format_for_iphone_requests, options
|
39
|
+
end
|
40
|
+
|
41
|
+
# Makes a controller act like _every_ request is from an iPhone.
|
42
|
+
#
|
43
|
+
# ==== Options
|
44
|
+
# Pass in any options appropriate for +before_filter+.
|
45
|
+
def responds_to_iphone!(options = {})
|
46
|
+
before_filter :ensure_format_is_iphone, options
|
47
|
+
end
|
48
|
+
|
49
|
+
# Skips the iphoneification before_filter set by #responds_to_iphone or #responds_to_iphone!.
|
50
|
+
#
|
51
|
+
# ==== Options
|
52
|
+
# Pass in any options appropriate for +before_filter+.
|
53
|
+
def skip_iphone_response(options = {})
|
54
|
+
if self.filter_chain.any? { |f| f.method == :ensure_format_is_iphone }
|
55
|
+
skip_before_filter :ensure_format_is_iphone, options
|
56
|
+
else
|
57
|
+
skip_before_filter :adjust_format_for_iphone_requests, options
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
# Just some utility methods.
|
64
|
+
module InstanceMethods
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
# Sets request.format to :iphone if request is from an iPhone.
|
69
|
+
def adjust_format_for_iphone_requests
|
70
|
+
set_format_to_iphone if iphone_request?
|
71
|
+
end
|
72
|
+
|
73
|
+
# Just sets format to :iphone, whatever.
|
74
|
+
def ensure_format_is_iphone
|
75
|
+
set_format_to_iphone # always
|
76
|
+
end
|
77
|
+
|
78
|
+
# Rails uses request.format in +respond_to+ blocks, and other places:
|
79
|
+
# this is where the magic happens.
|
80
|
+
def set_format_to_iphone
|
81
|
+
request.format = :iphone
|
82
|
+
end
|
83
|
+
|
84
|
+
# This method looks at the browser's user-agent string to determine
|
85
|
+
# whether or not the current request is from an iPhone (or iPod Touch).
|
86
|
+
def iphone_request?
|
87
|
+
(agent = request.env["HTTP_USER_AGENT"]) && agent[/(Mobile\/.+Safari)/]
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
Mime::Type.register_alias "text/html", :iphone
|
94
|
+
|
95
|
+
if defined?(ActionController::Base)
|
96
|
+
ActionController::Base.send(:extend, IPhoneification::ClassMethods)
|
97
|
+
ActionController::Base.send(:include, IPhoneification::InstanceMethods)
|
98
|
+
end
|
data/lib/tworgy-rails.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tworgy-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mat Holroyd
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-02-26 00:00:00 +11:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -37,6 +37,7 @@ files:
|
|
37
37
|
- Rakefile
|
38
38
|
- lib/tworgy-rails.rb
|
39
39
|
- lib/tworgy/active_record.rb
|
40
|
+
- lib/tworgy/iphoneification.rb
|
40
41
|
- lib/tworgy/routing.rb
|
41
42
|
- spec/spec.opts
|
42
43
|
- spec/spec_helper.rb
|