Module: Quartermaster::Helper

Defined in:
lib/quartermaster.rb

Instance Method Summary collapse

Instance Method Details

#browser_from_user_agent(user_agent = request.user_agent) ⇒ Object

Returns an array containing the base browser name and a version-specific name (except for Konqueror) for the given user_agent which defaults to Rails’ request.user_agent



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/quartermaster.rb', line 26

def browser_from_user_agent( user_agent = request.user_agent )
  @browser_from_user_agent ||= case user_agent.to_s.downcase
    when /opera[ \/](\d)/     then "opera opera#{$1}" # Have to check for opera before msie
    when /msie (\d)/          then "ie ie#{$1}" # F U ie5.5 - todo: Support minor versions
    when /firefox\/(\d)/      then "firefox firefox#{$1}"
    when /safari\/([2-9])/   then "safari safari#{$1.to_i - 2}" # Safari uses the webkit build number. Only works with Safari 1.3+
    when /rv:([^)]+)\) gecko/ then "gecko gecko#{$1}"
    when /applewebkit\/(\d)/  then "webkit webkit#{$1}"
    when /konqueror/          then 'konqueror'
    else ''
  end.split(' ')
end

#javascript_include_tag_for_browser(options = {}) ⇒ Object

Very similar to stylesheet_link_tag_for_browser except for javascripts



15
16
17
18
# File 'lib/quartermaster.rb', line 15

def javascript_include_tag_for_browser( options = {} )
  agent_attributes = [os_from_user_agent] + browser_from_user_agent
  agent_attributes.select{ |attribute| File.exists?( File.join(self.class::JAVASCRIPTS_DIR, 'browsers', "#{attribute}.js" )) }.collect{ |attribute| javascript_include_tag( "browsers/#{attribute}", options )}.to_s
end

#os_from_user_agent(user_agent = request.user_agent) ⇒ Object

Returns a short version of the OS name for the given user_agent which defaults to Rails’ request.user_agent



40
41
42
43
44
45
46
47
# File 'lib/quartermaster.rb', line 40

def os_from_user_agent( user_agent = request.user_agent )
  @os_from_user_agent ||= case user_agent.to_s.downcase
    when /win/         then 'win'
    when /mac/         then 'mac'
    when /(linux|x11)/ then 'linux'
    else ''
  end
end

Uses the output of browser_from_user_agent to figure out what stylesheets to look for and then uses stylesheet_link_tag to produce the link tags. Stylesheets must appear in STYLESHEETS_DIR/browsers/ (STYLESHEETS_DIR is public/stylesheets by default) and be named the same as the output of browser_from_user_agents. If both firefox.css and firefox2.css, for example, are present and Firefox 2 makes a request, both stylesheets will be loaded. Takes any options that can be passed to stylesheet_link_tag



9
10
11
12
# File 'lib/quartermaster.rb', line 9

def stylesheet_link_tag_for_browser( options = {} )
  agent_attributes = [os_from_user_agent] + browser_from_user_agent
  agent_attributes.select{ |attribute| File.exists?( File.join(self.class::STYLESHEETS_DIR, 'browsers', "#{attribute}.css" )) }.collect{ |attribute| stylesheet_link_tag( "browsers/#{attribute}", options )}.to_s
end

#user_agent_for_cssObject

Returns a space-separated string of the OS and the short user agents for the requesting browser



21
22
23
# File 'lib/quartermaster.rb', line 21

def user_agent_for_css
  "#{os_from_user_agent} #{browser_from_user_agent.join(' ')}".chomp(' ')
end