Wednesday, July 2, 2008

Revelations from Rails Source 3

This is part three of my series of lessons learned from reading the rails source code
<<PART 2PART 4>>

Woke up this morning, did some more reading through the ActionController module. Here's a few points of interest:

---------------------------------------------------------------

7


asterisk operator for the array:

Here's a trick I hadn't seen before, but might be handy. Ever had an array of numbers (or words) and wanted to print them out as a comma delimited list? You COULD do this:


arr = ["Red","White","Blue"]
str = ""
i = 0
while i < arr.length do
str << arr[i]
str << "," if i < (arr.length - 1)
i += 1
end
puts str


or, using the asterisk method of array, you could do this:


str = ["Red","White","Blue"] * ","
puts str


I know which one I prefer.

EDIT: As cypher pointed out in his comment below, the "join" method of the array class can be used to do the same thing. It should be noted, though, that you can pass an integer argument to the "*" method, and it will produce an array that is a concatenation of the original arrays contents repeated as many times as the passed in integer (join does not replicate that behavior). Thanks, Cypher!

---------------------------------------------------------------

8


class variable access:

much like attr_accessor for instance variables, and mattr_accessor for module-level variables, "cattr_accessor" (available through active support) is an available helper for use with classes when you want to expose their variables. For example:

class MyClass
@@data_var = "value"
end

currently the "data_var" class variable is only accessible inside the class. If you want to expose it for checking and modifiction, you can do the following:

class MyClass
@@data_var = "value"
cattr_accessor :data_var
end

Now you could access it from outside through the line "MyClass.data_var" or "MyClass.data_var = X".
---------------------------------------------------------------

9


Offloading static content hosting to another server:

So your rails servers are pretty tasked the way things are, and you'd like to not waste their time serving up images, stylesheets, and javascript files. Unfortunately, that's a pain to accomplish, right? I mean, you'd have to go change every URL in your view code to grab resources from the new server.

Not true, so long as you've followed good practices up until now. If you've been using "<%= stylesheet_link_tag %>" for your CSS pages, and "<%= javascript_include_tag %>" for your javascript files, and "<%= image_tag %>" for your images, then you're already set up for an easy transition.

ActionController::Base has a class variable called "asset_host" (exposed by cattr_accessor :asset_host, as discussed in tidbit 8 above), which defaults to an empty string. If you set it to be "http://www.your_static_server.com" (maybe in environment.rb), the tags mentioned above for images, javascript, and CSS will automatically prepend that address to every call for static data, thus allowing your rails servers to focus on what they do best. The line would look like this:

ActionController::Base.asset_host = "http://www.your_static_server.com"

2 comments:

cypher said...

@7: There's also a method called "join" that does the exact same thing. Depending on the context you might want to use join or the *-operator.

Ethan Vizitei said...

@cypher: Good point. For concatenating the array with a seperator, join does work just as well. I have updated the post to include that info. Thanks!