Chris Mar's Weblog Blog Different

Friday Oct 24, 2008

I noticed a really interesting feature coming in Rails 2.2 called "Memoization". It means caching the return value of a method. If your method is called multiple times the cache value is returned rather then the executing the method again.

This is something I do in every application for

def current_user
     @current_user ||= User.find(session[:current_user_id])
end

In the new Rails 2.2, you can have the same behavior with the "memoize" declaration

memoize :current_user
def current_user
     User.find(session[:current_user_id])
end

Regardless of how many times I call current_user, Rails will only do one DB query.

This level of caching is very common and we always use it in practice with instance variables and singleton patterns. Its nice to see a declarative way to do this in Rails. Learning a new software term like "Memoization" is a bonus.

Thursday May 29, 2008

I've been updating my Agile, Web 2.0, AJAX, Social Networking and Google Mapping skills. I've just read 5 great books in the last couple months. Here they are with a quick one sentence review. All of them have been helpful, easy to read and I would recommend them to anyone interested in updating their skills.

Beginning Google Maps Applications with Rails and Ajax This was a great introduction to the Google Maps api and some tips on retrieving data from your Rails application. If your Rails/AJAX skills are solid and you have a basic understanding of the Google API, this book will probably not add much to your skill set. This is an easy read and explained the mystery of the Google Maps API, which now doesn't seem like it was so difficult

Prototype and Scriptaculous in Action. The In Action series of books have become my favorite publisher in recent years. This is a great introduction to the main javascript libraries powering Rails AJAX methods. After reading this book, I've stopped using the Rails helpers and just make the calls myself. I still use the remote_form method to get the authentication key passed for me. Do yourself a favor, learn what Prototype and Scriptaculous can do for you and you'll find development tasks are much easier.

JavaScript: The Definitive Guide. The Rhino book is in its 5th edition. I've always been one of the Javascript haters, but after learning Prototype and Scriptaculous and playing with the Google Map API I realized I needed to embrace Javascript. If you don't want to buy an updated book, watch Douglas Crockford's Advanced Javascript video series. I'm not sure I love javascript, but the hatred has waned.

If you decide to play with AJAX and Javascript and have been frustrated in the past, get Fire Bug it makes debugging and monitoring javascript amazing.

Pro CSS Techniques is a fairly good book. It explained the current state of CSS and how most styles work and relate to each other. I was hoping for more receipe examples showing desired output and explained the technical reasons for the styles used. I was also hoping for guidelines on approaches to styling. This book does a great job of explaining selectors and styles. I can't give it a strong recommendation because it didn't provide what I needed, but my expectations are different then yours.

Advanced Rails Recipes: 84 New Ways to Build Stunning Rails Apps . This book was just released at RailsConf2008. It has recipes from a lot of the prominent rails community members. I've just started this book and expect it will make some great beach reading. The recipes are seem to be real world ready. I like reading solutions for problems; not only for the specific solution but to open my mind to new ways of thinking.

Monday Mar 17, 2008

This weekend, I finally figured out a good method of propagating events on my web page to notify other elements to update themselves. When your working on an AJAX application there is a need to update other sections of a page based on an event. This can be done manually, but I'm dynamically building my page content and the event generator does not know ahead of time who might want to be notified. So, I present my solution below, I'm sure this is old news for the pros, but it took me a while of combing through documentation to figure it out.


First Lets create something to generate an "Event".

<% form_remote_tag  :update => "my_results_div",
	:url => { :controller => 'posts', :action => 'add_post' },
	:complete => "document.fire('posts:updated')" do -%>
	...
<% end -%>

The important part is the statement document.fire('posts:updated') on the complete. This is a prototype method to fire an event. You must put your event name within a namespace like so I prefixed mine with "posts:"


Now, create a listener for the event.


document.observe('posts:updated',  function() {
	new Ajax.Updater('post_titles', '/posts/titles_list', 
	{ method: 'get',
	  evalScripts: true
	});
  });

So, now anywhere in your page you can add the above statement document.observe('posts:updated'.... When someone calls document.fire all listeners will be notified, at which time they can update themselves. The above example does an ajax update of the div 'post_titles' with the results from my controller method '/posts/titles_list'.

I broke my page into lots of little dynamic widgets, and each widget self registers for the events they are interested in. Then I have my main section of the page fire the appropriate events at the right time.

The above code was shown using Rails and Prototype, but you could just use Prototype and your own scripting language.