Posted by Matt
on May 16, 2008
Neat little snippet of code in those odd times you need to loop over every model in your application programmatically.
This exact snippet loops over every model, and converts each timestamp (created_at, updated_at) to UTC. It can be quite helpful if you’re adding Time Zone support to your Rails 2.1 application and you have existing non-UTC timestamps.
Object.constants.each do |o|
klass = eval(o, TOPLEVEL_BINDING)
if klass.is_a?(Class) && klass.superclass == ActiveRecord::Base
puts "\n\nUpdating #{klass} timestamps to UTC"
klass.all.each do |obj|
offset_hours = (Time.zone.utc_offset / 1.hour) * -1
obj.created_at = obj.created_at + (obj.created_at.dst? ? offset_hours - 1 : offset_hours).hours
obj.updated_at = obj.updated_at + (obj.updated_at.dst? ? offset_hours - 1 : offset_hours).hours
obj.save
print '.'
end
end
end
*Please note: I haven’t tested this very thoroughly, but it does seem to do the trick. As we know, Dates and Times can be total PITAs!
Posted by Matt
on May 16, 2008
In the application I’m currently developing a User has_many Jobs, and each Job has a bunch of nested resources. The application calls for a Dashboard, filled with the latest changes to any Job the currently logged-in user is assigned to.
Sounds difficult huh?
def self.get_latest_items_for(current_user)
assigned_job_ids = current_user.jobs.collect(&:id)
find(:all, :conditions => ["job_id in (?)", assigned_job_ids])
end
In order to get the currently logged-in user’s list I just do:
RecentItem.get_latest_items_for(current_user)
Simple, elegant, understandable, and concise.
Posted by Matt
on May 15, 2008
I spent a greater part of this day attempting to get nginx, SSL, and EC2 to play nice together. The funny thing was that nginx (which was hosting a mongrel_cluster) would serve standard HTTP requests just fine, but HTTPS requests just kind of hung there.
I could see that nginx was in fact listening to port 443 and could connect locally, but the whole shebang fizzled out anywhere outside the local box. No firewall either.
After pulling my hair out for a bit, I happened across this article that alluded to the ‘ec2-authorize’ command.
Seems as though you have to remotely manage the “firewall” of a running instance!
Anyhow; cutting to the chase: To enable HTTPS on your EC2 instance just run:
ec2-authorize default -p 443
Posted by Matt
on May 12, 2008
One of Rails’ greatest strengths lies with the plugin system. Git is the new source control kid on the block that all the girls are talking about, but Git breaks existing systems to manage plugins, such as Piston and the great ’./script/plugin install -x’ command.
Git giveth and Git taketh away.
Git has the supernatural ability to track submodules in your application. The below command adds RESTful_ACL as a submodule to your application inside the vendor/plugins directory, checks the project out for you, and commits changes in your application (added info in .gitmodules).
git submodule add git://github.com/mdarby/restful_acl.git vendor/plugins/restful_acl
git commit -a
When you go to clone to a different machine, just run the following two commands to checkout all of your submodules:
git submodule init
git submodule update
You’re off and running!