mobile-enhancements 0.0.4 → 0.0.5

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.
@@ -1,11 +1,12 @@
1
1
  require "mobile_enhancements/route_helpers"
2
2
  require "mobile_enhancements/helper_delegation"
3
+ require "mobile_enhancements/url_helper"
3
4
  require "rails/railtie"
4
5
 
5
6
  module MobileEnhancements
6
7
  class Railtie < Rails::Railtie
7
8
 
8
- config.to_prepare do
9
+ config.after_initialize do
9
10
  # setup our route helpers
10
11
  ActionDispatch::Routing::Mapper.send(:include, MobileEnhancements::RouteHelpers)
11
12
  # setup the path helpers
@@ -21,10 +22,8 @@ module MobileEnhancements
21
22
  # setup the format calculation
22
23
  ActionController::Base.class_eval do
23
24
  before_filter do |controller|
24
- if controller.mobile_request?
25
- controller.request.format = controller.determine_format
26
- controller.params["mobile"] = MobileEnhancements.configuration.mobile_path_prefix
27
- end
25
+ controller.request.format = controller.determine_format
26
+ controller.params["mobile"] = controller.mobile_param_value
28
27
  end
29
28
  end
30
29
  end
@@ -29,6 +29,11 @@ module MobileEnhancements
29
29
  mobile_request? ? mobile_format.to_sym : request.format.to_sym
30
30
  end
31
31
 
32
+ # returns what the value of the mobile parameter should be
33
+ def mobile_param_value
34
+ mobile_request? ? mobile_path_prefix : nil
35
+ end
36
+
32
37
  # strips any mobile prefix from the url
33
38
  def desktop_url(url = request.url)
34
39
  desktop_path(url)
@@ -1,18 +1,34 @@
1
1
  module MobileEnhancements
2
2
  module UrlHelper
3
3
  def url_for(*args)
4
+ url = super
4
5
  # ignore our direct calls for desktop/mobile URLs
5
6
  if args.size == 1 && args.first.frozen?
6
- super
7
+ url
7
8
  # if it's from a mobile
8
9
  elsif mobile_request?
9
10
  # mobilify the url
10
- mobile_url(super)
11
+ murl = mobile_url(url)
12
+ # verify or resort to original
13
+ verify_path(murl) || url
11
14
  # if it's a desktop request
12
15
  else
13
- # process as normal
14
- super
16
+ # desktopify the url
17
+ durl = desktop_url(super)
18
+ # verify or resort to original
19
+ verify_path(durl) || durl
15
20
  end
16
21
  end
22
+
23
+ # ensures a URL/path exists
24
+ def verify_path(url)
25
+ path = strip_domain(url)
26
+ Rails.application.routes.recognize_path(path) && url rescue false
27
+ end
28
+
29
+ # converts a URL/path to a path
30
+ def strip_domain(url)
31
+ url.gsub(/([a-z]+\:)?\/\/.*?\//i, "/")
32
+ end
17
33
  end
18
34
  end
@@ -1,3 +1,3 @@
1
1
  module MobileEnhancements
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -47,6 +47,14 @@ describe TestController do
47
47
  expect(response).to redirect_to "http://test.host/#{MobileEnhancements.configuration.mobile_path_prefix}/two"
48
48
  end
49
49
  end
50
+
51
+ describe "GET /#{MobileEnhancements.configuration.mobile_path_prefix}/mobile_to_desktop_path" do
52
+ before { get :mobile_to_desktop_path, mobile: MobileEnhancements.configuration.mobile_path_prefix }
53
+
54
+ it "should render a desktop URL" do
55
+ expect(response.body).to eq "/one"
56
+ end
57
+ end
50
58
  end
51
59
 
52
60
  context "via the desktop UI" do
@@ -19,4 +19,6 @@ class TestController < ActionController::Base
19
19
  def fixed_redirect_checking
20
20
  redirect_to(mobile_url(optional_mobile_url))
21
21
  end
22
+
23
+ def mobile_to_desktop_path; end
22
24
  end
@@ -0,0 +1 @@
1
+ <%= url_for(controller: "test", action: "one") %>
@@ -11,6 +11,7 @@ Rails.application.routes.draw do
11
11
 
12
12
  mobile_only do
13
13
  get "three", to: "test#three"
14
+ get "nine", to: "test#mobile_to_desktop_path"
14
15
  root to: "test#four"
15
16
  end
16
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobile-enhancements
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
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-20 00:00:00.000000000 Z
12
+ date: 2012-12-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -152,6 +152,7 @@ files:
152
152
  - spec/internal/app/controllers/test_controller.rb
153
153
  - spec/internal/app/views/layouts/application.html.erb
154
154
  - spec/internal/app/views/layouts/application.mobile.erb
155
+ - spec/internal/app/views/test/mobile_to_desktop_path.mobile.erb
155
156
  - spec/internal/app/views/test/path_rendering.html.erb
156
157
  - spec/internal/app/views/test/path_rendering.mobile.erb
157
158
  - spec/internal/config/initializers/mobile_enhancements.rb
@@ -175,12 +176,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
175
176
  - - ! '>='
176
177
  - !ruby/object:Gem::Version
177
178
  version: '0'
179
+ segments:
180
+ - 0
181
+ hash: -576039057823727365
178
182
  required_rubygems_version: !ruby/object:Gem::Requirement
179
183
  none: false
180
184
  requirements:
181
185
  - - ! '>='
182
186
  - !ruby/object:Gem::Version
183
187
  version: '0'
188
+ segments:
189
+ - 0
190
+ hash: -576039057823727365
184
191
  requirements: []
185
192
  rubyforge_project:
186
193
  rubygems_version: 1.8.24
@@ -192,6 +199,7 @@ test_files:
192
199
  - spec/internal/app/controllers/test_controller.rb
193
200
  - spec/internal/app/views/layouts/application.html.erb
194
201
  - spec/internal/app/views/layouts/application.mobile.erb
202
+ - spec/internal/app/views/test/mobile_to_desktop_path.mobile.erb
195
203
  - spec/internal/app/views/test/path_rendering.html.erb
196
204
  - spec/internal/app/views/test/path_rendering.mobile.erb
197
205
  - spec/internal/config/initializers/mobile_enhancements.rb