Posted by David N. Welton
Fri, 13 Jun 2008 21:21:00 GMT
I recently redid my personal web site, at welton.it. Wanting to be quick about it, and make the look and feel a bit more uniform than it has been in the past, I hacked together some pages in Rails. Despite this being sort of a "killing a fly with a bazooka" situation, I've been doing lots with Rails, so it was quick to use. Here's the thing, though: Rails is definitely overkill, as the site is basically static. I don't need to calculate anything or fetch stuff from a database - I just wanted a reasonably good template system, and I am quite comfortable with Rails these days.
But the idea of leaving Rails running for a static site was of course no good: I basically need to cache the entire thing, so that Rails is simply not involved. How to do this as quick as possible (in between diaper changing and other baby duties!) ? Ideally, it would be possible to introspect Rails in order to know exactly which pages are present, then cache those, and avoid Rails on the server entirely (just generate them locally and put them in subversion), but that proved to be fairly hacky, so I settled for this code, which simply caches all pages which comes across, when caches_pages is placed in application.rb:
class CacheFileName
include ActionController::Caching::Pages
include ActionController::Caching::Pages::ClassMethods
def cachedname(path)
page_cache_file(path)
end
end
def caches_pages()
return unless perform_caching
after_filter do |c|
res = c.cache_page
cfn = CacheFileName.new
cf = Cachedfile.new :filename => cfn.cachedname(c.request.path)
cf.save!
end
end
It simply caches everything. To be able to easily clear out the cache if there are any changes to the site, we record the changes in the Cachedfile model, which is defined like this:
create_table "cachedfiles", :force => true do |t|
t.string "filename"
t.datetime "created_at"
t.datetime "updated_at"
end
with this model:
class Cachedfile < ActiveRecord::Base
def Cachedfile.clean_cache
Cachedfile.find(:all).each do |cf|
begin
fn = ActionController::Base.page_cache_directory + cf.filename
File.delete fn
rescue => e
logger.error "Error deleting #{fn}: #{e.inspect}"
ensure
cf.destroy
end
end
end
end
which has a class method to go through and clean out all the cached files. I call it manually from ./script/clean_cache:
#!/usr/bin/env ruby
ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'development'
require File.dirname(__FILE__) + '/../config/boot'
require "#{RAILS_ROOT}/config/environment"
require 'console_app'
Cachedfile.clean_cache
It's not a beautiful system, but it gets the job done.
Tags rails, ruby | 5 comments | no trackbacks
Posted by David N. Welton
Sun, 03 Feb 2008 19:24:00 GMT
Yoav Shapira wonders about the "rails backlash":
http://yoavs.blogspot.com/2008/02/railsshudder.html
I'm a big fan of Rails, and of course I realize that it has some flaws and is certainly not the optimal choice for all situations, but I don't buy into the bashing.
Sure, Rails was hyped, but there is no way that it could have possible reached the level of popularity it has on hype alone. We're not talking about multi-million dollar marketing budgets commanded by the likes of Sun or Microsoft, but about a video and some nice dressings by DHH and 37signals. A small group like that just doesn't have the resources to push something like Rails as far as it's gone without some substance to back it up.
Look at all the imitation. People realize that the basic idea is good, and even though the basic idea is nothing phenomenal, it hadn't really been done before. What is this idea? Take a bunch of components, and good (if not always the absolute best) practices, and integrate them nicely. Database, templating, testing, a clean application structure, ajax/javascript integration, with some code generation, all done with a language that's flexible enough to do all of the above pretty well. All those things existed, but Rails did a great job of tying them all together. And that's been imitated far and wide. That seems to indicate that, even though the imitators may change and improve bits and pieces, they're quite smitten with the idea itself. So much that they've dedicated a lot of time and effort to reproduce it. That's quite a tribute.
Look at the adoption. Moving to a new language/platform is a big step, not one to be taken lightly, and for people with lots of existing code in some other language, perhaps simply not possible. Hype will take things only so far, but Rails has been taken up by a large number of people who certainly didn't do so because it was the safe, easy path to take. Perhaps some of those people have found that Rails really wasn't for them, but that's pretty normal. No one said it was perfect. However, when you look at what it takes to make people jump ship from what they know, and are comfortable with, you realize that it really must have been that much better to make people go out on a limb.
Furthermore, it's easy to point out the failings and drawbacks of something after the fact, but much more difficult to do creative, innovative work of your own. Compounding that is the fact that criticizing others' work is often an easy and effective way of looking smart; there's an interesting study referenced here http://bobsutton.typepad.com/my_weblog/2006/08/brilliant_but_c.html that talks about this effect.
It's something I've come to dislike over the years. Our field is so new, so dynamic, and so immature, that it's usually pretty easy to take a look at something and say that it "sucks". It is much harder to write something of your own and put it out there, knowing full well that lots of people who have never done much of anything will jump on it and bemoan its "copious failings". That's not to say that it's impolite to criticize, but - do it constructively please. Imagine that you were there in the same room, talking to the person who wrote the code in question. You would still say you don't like it, but you'd probably be much more polite and helpful in your commentary. I hope so, at least.
Tags rails, ruby | no comments | no trackbacks
Posted by David N. Welton
Fri, 30 Nov 2007 09:56:00 GMT
The economics of programming languages point to Rails being significantly better than what went before it.
I got to thinking about this when reading a comment on a site I like to read, which said:
Rails in itself is, to me, not that impressive. It does a lot of things right, but it does probably just as many wrong. Not the least of which is scaling.
It seems that these sorts of "after the fact" "I know better" comments are a dime a dozen in the world of programming discussions. It's easy to come along after something's been built and puff yourself up by pointing to defects in existing systems and show that, therefore, by comparison, you're a clever fellow.
That's not my point, though - what I wish to explain is that yes, Rails really was that much better than what was around before it came onto the scene:
"Switching costs" between languages are high. Less so for really sharp programmers, but for the masses that use one or two languages, learning a new language, tools, deployment, etc... is a big step to take, with potentially high risks. Even most A-list programmers I know use a few languages at a time - it's simply easier if you're not tripping over your own feet by switching to a different system every day. "Flow" is easier to attain when you're ensconced in the thinking of one language. For companies, this effect is magnified, and switching to something new is not done lightly.
Since companies are beginning to explore Rails, successfully, I might add, you have to conclude that the big step into the unknown was worth it for some reason. Especially considering that a number of other languages rushed to copy various nice aspects of Rails, lessening the need for users of those systems to consider taking the leap.
Of course, that's not to say it's a perfect system, without reproach, or has no negative aspects, but in the spirit of honesty, and credit where credit is due, Rails really did move things a step forward, and the willingness of people to incur high switching costs to obtain its benefits is strong evidence of that.
Tags economics, rails, ruby | no comments | no trackbacks
Posted by David N. Welton
Tue, 30 Oct 2007 15:15:00 GMT
I've been working on a project that uses graphics created with Plotr, and I wanted to automate that from Rails, so I created Chartr. It's not very complete, but works well enough to create stuff like this:
Help is more than welcome! I think the API is still a bit clumsy, more chart types need adding, and there are other things that could probably be improved, but it does work.
Tags javascript, rails, ruby | no comments | no trackbacks
Posted by David N. Welton
Mon, 03 Sep 2007 12:52:00 GMT
I have a Rails application that uses Hpricot, "a fast and delightful HTML parser" for Ruby.
It seems, however, that this change (the one in xchar.rb, specifically), present in 0.6, but not 0.5 causes the inner_text method to fail less than gracefully, spitting out a ? character instead of simply letting through the unescaped text. I was depending on the latter behavior, so this change blew up my code. It's a good thing I caught it before going to production.
Other than that, I've been very happy with Hpricot, and it is indeed quite quick and useful.
Tags ruby | no comments | no trackbacks