Skip to content

Instantly share code, notes, and snippets.

@salmanx
Created December 5, 2019 05:39
Show Gist options
  • Save salmanx/0bdbf1fc0323c1fe94caf350e9047ee4 to your computer and use it in GitHub Desktop.
Save salmanx/0bdbf1fc0323c1fe94caf350e9047ee4 to your computer and use it in GitHub Desktop.
Helper classes for tracking browser in rails application
module BrowserClassHelper
def browser_class
if ie11?
"is-ie11"
else
"is-" + request.browser.downcase
end
end
def ie11?
request.browser == "Internet Explorer" && request.browser_version.include?("11.")
end
def os_class
return "is-windows" + request.os.sub!(/Windows /, "") if need_remove_windows?
return "is-windows" + request.os if Woothee.parse(request.user_agent)[:vendor] == "Microsoft"
return "is-mac" if request.os == "Mac OSX"
return "is-ios" if request.from_ios?
return "is-android" if android?
return "is-windows-phone" if request.from_windows_phone?
"is-other-os"
end
def css_hack_class
browser_class + " " + os_class
end
private
def need_remove_windows?
Woothee.parse(request.user_agent)[:vendor] == "Microsoft" && request.os.include?("Windows")
end
def android?
request.from_android? || request.from_android_tablet?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment