Skip to content

Commit

Permalink
Merge pull request benlangfeld#37 from jvanbaarsen/action-optin
Browse files Browse the repository at this point in the history
Implemented has_mobile_fu_for :actions
  • Loading branch information
benlangfeld committed Jun 4, 2013
2 parents e5c91ed + cfc83a5 commit b7a341c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ class ApplicationController < ActionController::Base
end
```

If you dont want to have all the methods respond to :mobile and :tablet, you can opt-in this actions
using the following class method: `has_mobile_fu_for :action`
Example:

```ruby
class YourAwesomeClass < ActionController::Base
has_mobile_fu
has_mobile_fu_for :index

def index
# Mobile format will be set as normal here if user is on a mobile device
end

def another_method
# Mobile format will not be set, even if user is on a mobile device
end
```

Mobile Fu automatically adds a new `:mobile` and `:tablet` to `text/html` mime type
alias for Rails apps. If you already have a custom `:mobile` alias registered in
`config/initializers/mime_types.rb`, you can remove that.
Expand Down
31 changes: 29 additions & 2 deletions lib/mobile-fu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ def has_mobile_fu(set_request_format = true)
def has_no_mobile_fu_for(*actions)
@mobile_exempt_actions = actions
end

# Add this to your controllers to only let those actions use the mobile format
# this method has priority over the #has_no_mobile_fu_for
# class AwesomeController < ApplicationController
# has_mobile_fu_for :index
#
# def index
# # Mobile format will be set as normal here if user is on a mobile device
# end
#
# def show
# # Mobile format will not be set, even if user is on a mobile device
# end
# end
def has_mobile_fu_for(*actions)
@mobile_include_actions = actions
end
end

module InstanceMethods
Expand Down Expand Up @@ -111,10 +128,10 @@ def force_tablet_format
# 'Tablet' view.

def set_mobile_format
if request.format.html? && !mobile_exempt? && is_mobile_device? && !request.xhr?
if request.format.html? && !mobile_action? && is_mobile_device? && !request.xhr?
request.format = :mobile unless session[:mobile_view] == false
session[:mobile_view] = true if session[:mobile_view].nil?
elsif request.format.html? && !mobile_exempt? && is_tablet_device? && !request.xhr?
elsif request.format.html? && !mobile_action? && is_tablet_device? && !request.xhr?
request.format = :tablet unless session[:tablet_view] == false
session[:tablet_view] = true if session[:tablet_view].nil?
end
Expand Down Expand Up @@ -158,6 +175,16 @@ def is_device?(type)
request.user_agent.to_s.downcase.include? type.to_s.downcase
end

# Returns true if current action is supposed to use mobile format
# See #has_mobile_fu_for
def mobile_action?
if self.class.instance_variable_get("@mobile_include_actions").nil? #Now we know we dont have any includes, maybe excludes?
return mobile_exempt?
else
self.class.instance_variable_get("@mobile_include_actions").try(:include?, params[:action].to_sym)
end
end

# Returns true if current action isn't supposed to use mobile format
# See #has_no_mobile_fu_for

Expand Down

0 comments on commit b7a341c

Please sign in to comment.