[ANN] ActiveJax

ActiveJax is a [RoR](http://www.rubyonrails.org/ “Ruby on Rails”) plugin. It’s goal is to be an effective [ActiveRecord](http://wiki.rubyonrails.org/rails/pages/ActiveRecord “ActiveRecord in Ruby on Rails”) -> [Prototypejs](http://www.prototypejs.org/ “Prototype JavaScript framework: Easy Ajax and DOM manipulation for dynamic web applications”) bridge, allowing you to directly call your finders from javascript. Alright let’s see some code:

Ruby Model:

class Author < ActiveRecord::Base
active_jax
def find_by_name(n)
find(:all, :conditions => {:name => n})
end
end

Java Script

ActiveJax.Author.find_by_name("Nicholas Schlueter").
each(function(author) {
alert(author.name);
});

As you can see you just call the finder by name in the javascript.

It can also do belongs_to associations:

Ruby Model:

class Author < ActiveRecord::Base
active_jax :include => :publisher
belongs_to :publisher
def find_by_name(n)
find(:all, :conditions => {:name => n})
end
end

Java Script

ActiveJax.Author.find_by_name("Nicholas Schlueter").
each(function(author) {
alert(author.publisher.name);
});

Hold on, isn’t that pretty dangerous. It’s true, exposing your model can be dangerous. There is some security built in. Building on our previous example:

Ruby Model:

class Author < ActiveRecord::Base
active_jax :include => :publisher, :excluded_columns => :email
belongs_to :publisher
def find_by_name(n)
find(:all, :conditions => {:name => n})
end
end

Java Script

ActiveJax.Author.find_by_name("Nicholas Schlueter").
each(function(author) {
alert(author.publisher.name);
});

Hurray! The email address is no longer published to all would be spammers (bastards).

In order for all this to work you have to do 1 more thing, include the following somewhere in your html doc:

<%= active\_jax\_include %>

There is also a way to enable controller actions, but to be honest it is only partially thought through.

Anyone interested in learning more is encouraged to download the ActiveJax Sample Application and/or install the plugin and try it out.

./script/plugin install http://svn.simpltry.com/plugins/active_jax

In the future more information will be available at [http://rails.simpltry.com/active_jax.html](http://rails.simpltry.com/active_jax.html). Please be aware this isn’t production tested or efficient, I will be accepting patches and updating this as needed.

Find Controller Actions

Working on a super secret stealth project last night, I needed an **Array** of all the actions on a specific controller. I figured it would be a piece of cake given the introspection support in rails. And after scouring the rails source code for *minutes*, I concluded that it wasn’t supported with a single method. And it becomes even trickier when you try to get it from a class method. Take this example:

Class AuthorController < ApplicationController
  acts_as_super_secret
    def index
      render text => "find me"
    end
end

There is no possible way from inside the implementation of **acts\_as\_super\_secret** to know that the **index** action is available on the controller. This is due to the fact that ruby calls **acts\_as\_super\_secret** as it is parsing the class. So, you need to get a little fancier. It is a little beyond the scope of this post to give the whole solution, but here is the code I used to find the actions on a controller:

AuthorController.new.public_methods -
ApplicationController.new.public_methods

That needs to be run after the class is fully loaded. I am sure you could pull it out into something more efficient and generic:

def ApplicationController < ActionController::Base
def actions
self.public_methods - self.class.superclass.new.public_methods
end
end

Now you can call actions on or from any controller to get a list of actions available in that controller.

Prototype updated each

I have been using [prototype](http://www.prototypejs.org/ “Prototype JavaScript framework: Easy Ajax and DOM manipulation for dynamic web applications”) 1.6 for a while now and the more I use it the more I like it. I want to point out something that is a great improvement.

Prototype 1.5

[1,2,3].each(function(i) {
. . .
}.bind(blah));

Prototype 1.6

[1,2,3].each(function(i) {
. . .
}, blah);

Ok, that doesn’t look like much, and to some people it probably isn’t, but I like the way it cleans up the code. This is especially true when it comes to chaining enumerables.

In all fairness, I don’t think the [prototype core team](http://www.prototypejs.org/core “Prototype JavaScript framework: The Prototype Core Team”) came up with this idiom. I think this has been in [Mootools](http://mootools.net/ “mootools - home”) for over a year, but hey it is still welcome.

AssetPackager

It is a well known fact that reducing the number of http requests can drastically increase your page load times. One place where you can really make a big difference is your js/css files. In the past, I have written my own combiners, but those days are long gone. I have formally switched [dcrails](http://dcrails.com/ “DCRails.com || Making the Metrorail fun!”) to a well written, easy to use, rails plugin, [AssetPackager](http://synthesis.sbecker.net/pages/asset_packager “Scott Becker - AssetPackager – JavaScript and CSS Asset Compression for Production Rails Apps”).

This packager combines my js files together and then compresses them using Douglas Crockford’s [jsminify](http://www.crockford.com/javascript/jsmin.html “JSMIN, The JavaScript Minifier”) algorithm. In production it serves the compressed, combined version and in development it serves the files separately for easier development/debugging.

At my real job we need to be able to serve the merged files in qa. I submitted a patch which was, much to my delight, mostly applied. Simply add this to your environments.rb or a file in your config/initializers directory:

Synthesis::AssetPackage.merge_environments = ["qa", "production"]

Easy!

Feeds/Syndication