Giter VIP home page Giter VIP logo

mobile-fu's Introduction

Mobile Fu

Want to automatically detect mobile devices that access your Rails application? Mobile Fu allows you to do just that. People can access your site from a Palm, Blackberry, iPhone, iPad, Nokia, etc. and it will automatically adjust the format of the request from :html to :mobile or :tablet.

Installation

Simply add gem 'mobile-fu' to your Gemfile and run bundle install.

Usage

Add this this one line to the controller.

class ApplicationController < ActionController::Base
  has_mobile_fu
end

Once this is in place, any request that comes from a mobile device will be set as :mobile format. It is up to you to determine how you want to handle these requests. It is also up to you to create the .mobile.erb versions of your views that are to be requested.

If you do not want to set the format to :mobile or :tablet and only use the helper functions, pass false as an argument.

class ApplicationController < ActionController::Base
  has_mobile_fu false
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:

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.

I recommend that you setup a before_filter that will redirect to a specific page depending on whether or not it is a mobile request. How can you check this?

is_mobile_device? # => Returns true or false depending on the device or

is_tablet_device? # => Returns true if the device is a tablet

You can also determine which format is currently set in by calling the following:

in_mobile_view? # => Returns true or false depending on current req. format or

in_tablet_view? # => Returns true if the current req. format is for tablet view

Also, if you want the ability to allow a user to switch between 'mobile' and 'standard' format (:html), you can just adjust the mobile_view session variable in a custom controller action.

session[:mobile_view] # => Set to true if request format is :mobile and false
                           if set to :html

session[:tablet_view] # => Set to true if request format is :tablet and false
                           if set to :html

If you want to use the default response templates, like index.html.erb, instead of the index.tablet.erb you can exclude the tablet rendering from beeing used: you can create a before_filter and put it before the has_mobile_fu call

before_filter :force_tablet_html
has_mobile_fu

def force_tablet_html
	session[:tablet_view] = false
end

Same will work for mobile, just change the tablet values to mobile

So, different devices need different styling. Don't worry, we've got this baked in to Mobile Fu.

If you are including a css or sass file via stylesheet_link_tag, all you have to do is add _device to the name of one of your files to override your styling for a certain device. The stylesheet that is loaded is dependant on which device is making the request.

e.g., Accessing a page from a Blackberry.

stylesheet_link_tag 'mobile.css'

This loads mobile.css, and mobile_blackberry.css if the file exists.

Supported stylesheet override device extensions at the moment are:

  • blackberry
  • iphone (iphone,ipod)
  • ipad
  • android
  • mobileexplorer
  • nokia
  • palm

The stylesheet awesomeness was derived from Michael Bleigh's browserized styles

Inspiration for Mobile Fu came from Noel Rappin's rails_iui

Hopefully this should help you create some awesome mobile applications.

Testing Mobile Interface

If you want to force the mobile interface for testing, you can either use a mobile device emulator, or you can call force_mobile_format in a before filter.

class ApplicationController < ActionController::Base
  has_mobile_fu
  before_filter :force_mobile_format
end

You can also force the tablet view by calling force_tablet_format instead

class ApplicationController < ActionController::Base
  has_mobile_fu
  before_filter :force_tablet_format
end

Copyright (c) 2008 Brendan G. Lim, Intridea, Inc., released under the MIT license

mobile-fu's People

Contributors

abarriga avatar alekseyg avatar arsduo avatar atyndall avatar benlangfeld avatar bratsche avatar brendanlim avatar cactis avatar courtland avatar cowboyd avatar darrencauthon avatar denstepa avatar etal2 avatar jaswope avatar jimryan avatar joel1di1 avatar johnnyshields avatar jpmcgrath avatar jvanbaarsen avatar mmlac avatar noctivityinc avatar revgum avatar rmm5t avatar sentulasia avatar umairsiddique avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mobile-fu's Issues

Treat tablets as mobile

I need a way to treat tablets as mobile. I have an ugly hack to get this to work for me.

I do not like the idea of multiple unnecessary views. Tablets are just like mobile. They should not be treated otherwise. Will figure out how to fix sensibly.

If you have suggestions, let me know!

Thank you!

Ipad : not in mobile

Hi Anthor
I want to run mobile_fu on ipad like desktop
How can I do it
Thanks
Will

doesn't work with devise ,+ omniauth + google_oauth2

using ( Devise, OmniAuth, google_oauth2 )
when performing the callback ( with invalid credentials) , obviously the has_mobile_fu filter is called...
this doesn't happen when commenting out the has_mobile_fu ( doesn't happen with Facebook login)

 class ApplicationController < ActionController::Base
    has_mobile_fu
    ...

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  skip_before_filter :verify_authenticity_token, :only => [:google_oauth2]
  def google_oauth2
    ..
  def failure

=== console output =======

 NoMethodError

undefined method `to_sym' for nil:NilClass

Rails.root: /Users/yves/github/local/resone
Application Trace | Framework Trace | Full Trace

/Users/yves/.rvm/gems/ruby-1.9.3-p194@rails32/bundler/gems/mobile-fu-d44247921283/lib/mobile-fu.rb:165:in `mobile_exempt?'
/Users/yves/.rvm/gems/ruby-1.9.3-p194@rails32/bundler/gems/mobile-fu-d44247921283/lib/mobile-fu.rb:114:in `set_mobile_format'
/Users/yves/.rvm/gems/ruby-1.9.3-p194@rails32/bundler/gems/mobile-fu-d44247921283/lib/mobile-fu.rb:89:in `set_request_format'

rspec fails with uninitialized constant MobileFu::MobilizedStyles (NameError)

ruby-mobile-fu-1.1.1$ rspec 
/media/forge/debian/diaspora/ruby-mobile-fu-1.1.1/spec/mobilized_styles_spec.rb:3:in `<top (required)>': uninitialized constant MobileFu::MobilizedStyles (NameError)
        from /var/lib/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load'
        from /var/lib/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `block in load_spec_files'
        from /var/lib/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
        from /var/lib/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
        from /var/lib/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
        from /var/lib/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run'
        from /var/lib/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun'

Method names

Methods like is_mobile_device? should be renamed to mobile_device?

Can I open a pull request with this? and what will be a cleaver way to have backwards compatibility?

session[:tablet_view] = false does not work

Hi there,
as it looks like, mobile-fu ignores

before_filter :force_tablet_html
has_mobile_fu

def force_tablet_html
    session[:tablet_view] = false
end

as described in the readme.
I am using Rails 5.0.0.1 if it helps.. .

Google crawler is presented with desktop site when requesting mobile

I am using mobile-fu in a web site that shows two different user interfaces to desktop and mobile users. I've noticed that for some reason, Google only recognizes the desktop version of the site—mobile-friendly sites are marked as such in the search results, and this one isn't.

Using Google Webmaster Tools, I've been able to confirm that indeed Google only receives the desktop version of the site, even when it places the request as a mobile device. Digging into this issue, the problem seems to be because of the Accept header that the crawler sends.

I've captured the request sent by the crawler, and it's like this:

GET / HTTP/1.1
Host: www.host.com
Accept: */*
Cache-Control: no-cache
Connection: Keep-alive
From: googlebot(at)googlebot.com
Accept-Encoding: gzip,deflate
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)

Now, the culprit seems to be the set_mobile_format function in Mobile Fu. In order to set the view as mobile, four conditions must be met—being the first one that the request is for html content, which is not the case for this request made by the Google crawler, as the content of the Accept header is */*.

Before patching this on my side, I would like to ask about two of these four conditions just to make sure I don't run into a bigger problem:

  • Why is the mobile view only set for requests for html content? Is it just to avoid issues with, for example, API-type requests such as json or xml, or is there another reason?
  • Why is the mobile view not set for xhr requests? Since the first condition ensures that the request is for html content, it'd make sense to me that returned content would be different for desktop browsers and mobile devices.

Would you be interested in a patch for this? Since these conditions seem to be so deliberated, I don't know if you'd consider this issue a defect.

Thank you in advance for any light you can shed about this.

LoadError in 1.3.0

After I do bundle update (1.2.2 -> 1.3.0)
It raised this error
1.2.2 is fine

/Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `require': cannot load such file -- mobile-fu/tablet (LoadError)
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `block in require'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:214:in `load_dependency'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `require'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/mobile-fu-1.3.0/lib/mobile-fu.rb:1:in `<top (required)>'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/bundler-1.5.2/lib/bundler/runtime.rb:76:in `require'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/bundler-1.5.2/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/bundler-1.5.2/lib/bundler/runtime.rb:72:in `each'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/bundler-1.5.2/lib/bundler/runtime.rb:72:in `block in require'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/bundler-1.5.2/lib/bundler/runtime.rb:61:in `each'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/bundler-1.5.2/lib/bundler/runtime.rb:61:in `require'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/bundler-1.5.2/lib/bundler.rb:131:in `require'
09:59:37 web.1    |     from /Users/PikachuEXE/Projects/spacious-rails/config/application.rb:13:in `<top (required)>'
09:59:37 web.1    |     from /Users/PikachuEXE/Projects/spacious-rails/config/environment.rb:2:in `require'
09:59:37 web.1    |     from /Users/PikachuEXE/Projects/spacious-rails/config/environment.rb:2:in `<top (required)>'
09:59:37 web.1    |     from config.ru:4:in `require'
09:59:37 web.1    |     from config.ru:4:in `block in <main>'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
09:59:37 web.1    |     from config.ru:1:in `new'
09:59:37 web.1    |     from config.ru:1:in `<main>'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.8.0/lib/unicorn.rb:48:in `eval'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.8.0/lib/unicorn.rb:48:in `block in builder'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.8.0/lib/unicorn/http_server.rb:752:in `call'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.8.0/lib/unicorn/http_server.rb:752:in `build_app!'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.8.0/lib/unicorn/http_server.rb:137:in `start'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.8.0/bin/unicorn:126:in `<top (required)>'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/bin/unicorn:23:in `load'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/bin/unicorn:23:in `<main>'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `eval'
09:59:37 web.1    |     from /Users/PikachuEXE/.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `<main>'

Splitting :mobile and :tablet

Hi i am a ror programmer but i never code any gem before this... I want to split user agent :mobile and :tablet... but i need guidance on where to start...

Add has_metric_fu false to docs

Please make people aware that they can set a boolean flag on the has_metric_foo that allows to not set the handlers but give me all the helpers.

Invalid gemspec on master

I noticed that current master gemspec is not valid:

mobile-fu at /../bundle/ruby/1.9.1/bundler/gems/mobile-fu-09bff000ab40 did not have a valid gemspec.
This prevents bundler from installing bins or native extensions, but that may not affect its functionality.
The validation message from Rubygems was: ["spec/mobilized_styles_spec.rb"] are not files

Exclude tablets

Is there a way to exclude tablet views from rendering?

I want them to render the normal *.html.erb files, and only use the mobile.html.erb for mobile devices.

jQuery mobile navigation fails

I've used this version of mobile-fu & jQuery Mobile to create a mobile version of my app. However because of the request.xhr usage, the mobile version never gets set on the ajax page navigation methods.

If I just comment out the request.xhr section in set_mobile_format my site works as expected.

 if !mobile_exempt? && is_mobile_device? #&& !request.xhr?

Thoughts?

Is this still being maintained / production safe?

I'm seeing that code hasn't been touched for about 9 months, does it still detect the last mobile browsers?
I know that it might seem a little weird but I am willing to use mobile-fu for a Rails app which would only work on mobile devices, so I'd like to use it to detect just that and then based on the result render the content or redirect... (if there is a better way to implement this functionality please share :) )...
Thanks!

A method to opt-in actions

At the moment we have the option to opt-out actions using: has_no_mobile_fu_for
What do you guys think about a method that you can say, exclude all action except these, like so:
has_mobile_fu_for :index, :show

Update rack-mobile-detect.

rack-mobile-detect project seems to be abandoned, and its useragent parsing seems to cover much less mobile devices than Mobile-Detect regexps. I could do a fork of rack-mobile-detect and add there a Mobile-Detect patterns update task similar to the one in this project, but this is probably too much for a middleware. The fact is that I don't use its middleware capabilities, but I would like to have more precise mobile detection. Any thoughts on what we can do in this situation? We could add an alternative Mobile-Detect based is_mobile_device? method to mobile-fu, but having two methods with the same purpose looks somewhat strange.

is_mobile_device and is_tablet_device? broken in Ruby 1.9.3

In Ruby 1.9.3 the =~ returns the position of the match, not a true or false. I believe this is breaking the is_tablet_device? and is_mobile_device? methods.

I created a local init file to override these methods with the following

module ActionController
  module MobileFu
    module InstanceMethods
      def is_tablet_device?
        !!(request.user_agent.to_s.downcase =~ Regexp.new(ActionController::MobileFu::TABLET_USER_AGENTS))
      end

       def is_mobile_device?
        !is_tablet_device? && !!mobile_device
      end
    end
  end
end

Don't ask me why I had to repeat the is_mobile_device? method in my init file, all I know is that when I did not include it it still returned TRUE for an iPad but once it was there it returned the correct values.

Anyone else seeing this?

windows phone error

We all have one weird friend, and mine uses a windows phone, and pointed out to me that the mobile site doesn't work, and more than that prompts him to "Search for app in the store". I have found that if I remove " || is_device?('mobileexplorer')" from mobile_fu_helper.rb it will load the full site. This mean that something in the way it detects "mobileexplorer is not working". Any help is appreciated. Thanks for all your work on mobile-fu!

Propose to make 'has_mobile_fu' with current 'false' option as default behavior

Consider the case that I have a non-mobile enabled application, which is set to respond only to html, :js, and json (for example)

If I install the mobile-fu gem an put has_mobile_fu in my ApplicationController (expecting some sensible default behavior) my app is becomes broken on mobile devices!

Something like the below would make more sense.

has_mobile_fu     #=> behaves like current "has_mobile_fu false"
has_mobile_fu mime: true   #=> behaves like current "has_mobile"

NoMethodError: undefined method `html?' for nil:NilClass

There is an issue in set_mobile_format where an exception will be thrown if request.format is nil:

Backtrace:

mobile-fu (1.3.1) lib/mobile-fu.rb:129:in `set_mobile_format'
mobile-fu (1.3.1) lib/mobile-fu.rb:104:in `set_request_format'
activesupport (3.2.22.1) lib/active_support/callbacks.rb:462:in `_run__547905431638846484__process_action__2542152902247738565__callbacks'
activesupport (3.2.22.1) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.22.1) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.22.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
[...]

This is easy to replicate by sending a request with a weird extension like: http://mysite.com/asdf.htm

In my application it made sense to normalize unknown formats to html to circumvent the issue, but the gem code probably should be modified so as not to assume that request.format is present:

class ApplicationController < ActionController::Base

  has_mobile_fu

  def set_mobile_format
    if request.format.nil?
      request.format = :html
    end
    super
  end

end

Add web view as mobile

Some important mobile native apps (I.E. Facebook and Twitter) are displaying linked contents into a web view instead opening a browser window. Should it be possible to detect them as mobile or tablet?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.