has_mobile_views 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +23 -0
- data/lib/has_mobile_views.rb +44 -0
- data/lib/has_mobile_views_helper.rb +18 -0
- data/lib/tasks/has_mobile_views.rake +4 -0
- data/rails/init.rb +2 -0
- data/test/has_mobile_views_test.rb +29 -0
- data/test/test_helper.rb +6 -0
- metadata +74 -0
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the has_mobile_views plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the has_mobile_views plugin.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'HasMobileViews'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module ActionController
|
2
|
+
module HasMobileViews
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def has_mobile_views opts = {}
|
9
|
+
class_eval do
|
10
|
+
send :include, InstanceMethods
|
11
|
+
|
12
|
+
helper_method :mobile_request?
|
13
|
+
helper_method :mobile_browser?
|
14
|
+
|
15
|
+
before_filter :prepend_view_path_if_mobile
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module InstanceMethods
|
21
|
+
def prepend_view_path_if_mobile
|
22
|
+
if mobile_request?
|
23
|
+
prepend_view_path 'app/mobile_views'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def mobile_request?
|
28
|
+
session[:mobile_view] = mobile_browser? if session[:mobile_view].nil?
|
29
|
+
if params[:force_view] == 'mobile' && !session[:mobile_view]
|
30
|
+
session[:mobile_view] = true
|
31
|
+
elsif params[:force_view] == 'normal' && session[:mobile_view]
|
32
|
+
session[:mobile_view] = false
|
33
|
+
end
|
34
|
+
session[:mobile_view]
|
35
|
+
end
|
36
|
+
|
37
|
+
def mobile_browser?
|
38
|
+
request.env["HTTP_USER_AGENT"] && !!request.env["HTTP_USER_AGENT"][/(iPhone|iPod|iPad|Android)/]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
ActionController::Base.send(:include, ActionController::HasMobileViews)
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module HasMobileViewsHelper
|
2
|
+
|
3
|
+
# Helper to render the link to get from the mobile version to the normal one or vice-versa
|
4
|
+
# opts[:switch_to_normal_text] can be used to override the text for the "switch to normal" case
|
5
|
+
# opts[:switch_to_mobile_text] can be used to override the text for the "switch to mobile" case
|
6
|
+
# opts[:html] will we passed to the link_to helper
|
7
|
+
def switch_view_mode_link opts = {}
|
8
|
+
if session[:mobile_view]
|
9
|
+
text = opts[:switch_to_normal_text] || "Switch to normal version"
|
10
|
+
link_to text, params.update(:force_view => 'normal'), opts[:html]
|
11
|
+
else
|
12
|
+
text = opts[:switch_to_normal_text] || "Switch to mobile version"
|
13
|
+
link_to text, params.update(:force_view => 'mobile'), opts[:html]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
ActionView::Base.send(:include, HasMobileViewsHelper)
|
data/rails/init.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'has_mobile_views'
|
3
|
+
|
4
|
+
class MobileController < ActionController::Base
|
5
|
+
has_mobile_views
|
6
|
+
def index
|
7
|
+
render :nothing => true
|
8
|
+
end
|
9
|
+
|
10
|
+
def rescue_action_in_public e
|
11
|
+
raise e
|
12
|
+
end
|
13
|
+
end
|
14
|
+
ActionController::Routing::Routes.draw {|map| map.resources :mobile }
|
15
|
+
|
16
|
+
class HasMobileViewsTest < ActionController::TestCase
|
17
|
+
tests MobileController
|
18
|
+
|
19
|
+
test "should set the mode properly" do
|
20
|
+
get :index
|
21
|
+
assert_equal false, session[:mobile_view]
|
22
|
+
assert !@controller.view_paths.include?("app/mobile_views")
|
23
|
+
|
24
|
+
get :index, :force_view => 'mobile'
|
25
|
+
assert_equal true, session[:mobile_view]
|
26
|
+
assert @controller.view_paths.include?("app/mobile_views")
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_mobile_views
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- "Andr\xC3\xA9 Duffeck"
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-26 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: " This Rails plugin allows for rendering special templates for mobile devices, using the existing views and partials as a fallback.\n"
|
23
|
+
email:
|
24
|
+
- aduffeck@suse.de
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- lib/has_mobile_views.rb
|
33
|
+
- lib/has_mobile_views_helper.rb
|
34
|
+
- lib/tasks/has_mobile_views.rake
|
35
|
+
- Rakefile
|
36
|
+
- rails/init.rb
|
37
|
+
- test/has_mobile_views_test.rb
|
38
|
+
- test/test_helper.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/aduffeck/has_mobile_views
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.7
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: A Rails plugin which allows for rendering special templates for mobile devices.
|
73
|
+
test_files:
|
74
|
+
- test/has_mobile_views_test.rb
|