• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Using Google Ajax Libraries API with Ruby on Rails ...

The new Google Ajax Libraries API can help you offload bandwidth from your site. Their servers are configured with gzip, expires headers and e-tags, which leads to good scores from YSlow.

Intro
The new Google Ajax Libraries API can help you offload bandwidth from your site. Their servers are configured with gzip, expires headers and e-tags, which leads to good scores from YSlow. I will show two ways to use Google is new API to offload Ruby on Rails javascript (prototype and scriptaculous). In both solutions, your existing calls to javascript_include_tag can be left the same, because all of the magic is handled beneath the covers.

Implementation #1: Create a module
This first example is a module that chains the expand_javascript_sources method found in ActionView::Helpers::AssetTagHelper. If prototype or a scriptaculous file is loaded via javascript_include_tag, we will replace the normal functionality with a path to the file on Google is servers. Start by creating the file /config/initializers/google_asset_tag_helper.rb. Paste in the following:

 1 module ActionView
 2   module Helpers
 3     module GoogleAssetTagHelper
 4       GOOGLE_PATHS = {
 5         prototype => http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype,
 6         controls  => http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.1/controls,
 7         wouldragdrop  => http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.1/dragdrop,
 8         effects   => http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.1/effects
 9       }
10
11       def self.included(base)
12         base.send :alias_method_chain, :expand_javascript_sources, :google unless ActionController::Base.consider_all_requests_local
13       end
14
15       def expand_javascript_sources_with_google(sources)
16         google_sources, normal_sources = sources.partition { |source| GOOGLE_PATHS.include? source }
17         GOOGLE_PATHS.values_at(*google_sources) + expand_javascript_sources_without_google(normal_sources)
18       end
19     end
20   end
21 end
22
23 ActionView::Base.class_eval do
24   include ActionView::Helpers::GoogleAssetTagHelper
25 end

 

This defines GoogleAssetTagHelper, which chains expand_javascript_sources. The module is included into ActionView::Base. Notice that on line 12, the original method is only aliased if ActionController::Base.consider_all_requests_local is false. Since this is set to true in development mode, it is a sufficient way to determine whether or not Google AJAX Library should be used. If using the remote libraries in development is desirable, the condition can be safely removed.


Implementation #2: Assign lambda to ActionController::Base.asset_host
An alternative solution uses ActionController::Base.asset_host to determine the root path to the javascript files. In /config/environments/production.rb, you find:

1 # config.action_controller.asset_host = "http://some.asset.host/"


As of Rails 2.0.2, asset_host can a string or Proc. Since we want to conditionally change the asset_host based on what file is being include, a Proc is needed:

 1 google_paths = {
 2   prototype => http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/,
 3   controls  => http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.1/,
 4   wouldragdrop  => http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.1/,
 5   effects   => http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.1/
 6 }
 7
 8 config.action_controller.asset_host = Proc.new do |source, request|
 9   google_asset = google_paths.keys.detect { |asset| source.starts_with?("/javascripts/#{asset}") }
10   google_asset ? google_paths[google_asset] : "#{request.protocol}#{request.host_with_port}"
11 end


In this example, the code assumes that you do not already have an asset_host defined. If you do, simply replace the false expression on line 10 with your original asset_host string.

(The variable google_paths is defined inside of production.rb for brevity. However, I recommend defining it as a constant elsewhere in your own project.)

Using the javascript_include_tag
As mentioned in the introduction, your existing javascript_include_tag calls remain the same:


1 javascript_include_tag prototype, effects, controls, application

 View Full Story.
Posted at 09:40:52 am | Permalink | Posted in Google  Ruby  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Be the first ... |Add your comment.

Your Comment ...

  Name (required)

  Email (required, hidden)

  Website


Top Stuff

e-messenger

MessengerFX

eBuddy

ILoveIM

AIM Express

Top 20 Ruby CMS


Our Partners

Facebook Applications

Ajax Projects

Web 2.0 Sites

Webloglines

Human Development Handbook

Software Development Company

Ajaxlines

Stock Exchange Chat


About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (114)
  • Ajax (10)
  • Ajax Games (9)
  • Articles (94)
  • Bookmarking (35)
  • Calendar (19)
  • Chat (40)
  • ColdFusion (3)
  • CSS (48)
  • Email (23)
  • Facebook (41)
  • Flash (17)
  • Google (30)
  • Html (16)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (40)
  • Javascript (190)
  • jQuery (3)
  • JSON (24)
  • Perl (2)
  • PHP (97)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (1)
  • Ruby (15)
  • Storage (4)
  • Toolkits (94)
  • Tutorials (203)
  • UI (12)
  • Utilities (173)
  • Web2.0 (18)
  • XmlHttpRequest (22)
  • YUI (4)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange