Rails Edge Timezones February 9th, 2008

UPDATE: in_current_time_zone was removed and in_time_zone now defaults to the current timezone
When I work on feedish I generally start coding sessions by using piston to make sure I'm on the bleeding edge. Which is dangerous sometimes, but gives me a chance to try out new stuff in a pretty low pressure environment (since feedish has a user base of, um 2 at the moment). I'll open it up for more people eventually, just not quite there yet.

So yesterday I decided to take advantage of being on edge and use the new timezones code, since I had been dreading and putting off doing timezone stuff for a while. Ryan Daigle had a nice write up of the change, but I found I still had a couple questions when I got down to it. So I just wanted to include my notes on switching from the plugins to the built in edge stuff to help ease the transition for others.

First things first, make sure you have the latest tzinfo gem installed:

sudo gem install tzinfo
Now if you have the tzinfo and tzinfo_timezone plugins installed, delete them. Otherwise you get confusing errors.

Now then, you'll probably still want to make sure your database is using UTC, so make sure this is in your environment.rb:
ENV['TZ'] = 'UTC'
While you are in there you'll want to set the default timezone for your application as well:
config.time_zone = "Central Time (US & Canada)"
Now activerecord will be off and running, so any records you load will automagically convert times to the default zone.

Times that don't come from activerecord are not so nice though. You'll need to take care of these yourself. Two things to remember here. How to get now and how to convert regular times to the current time zone, those would be here:
# Time.now converted to current timezone
Time.zone.now
# Convert a time, in this case now, to the current timezone (same result as above)
Time.now.in_time_zone
Finally, to customize the time zone on a per request basis you can add this to your application controller:
# Change to the Hawaii time zone (if only it were that easy to travel there)
Time.zone = "Hawaii"
That should pretty much take care of it, enjoy.

tags: rails

backgroundrb + capistrano ftw January 21st, 2008

UPDATE: Latest version of script includes the port number(2000 by default) in the pid name.

I've had some ongoing issues getting capistrano and backgroundrb to play nice. So I thought I would do something about it today. I found a nice starting point from some one else. But I wanted to use invoke_command, so that it would be (hopefully) a bit more universal and use/not use sudo as neccesary. Also, if there is no pid file backgroundrb stop doesn't do anything other than fail and stop the execution of the rest of the chain. So I wanted to make it skip over the stop command if the pid file did not exist. So, without further adou here is what I ended up with.

  namespace :backgroundrb do
    desc "stop the backgroundrb server"
    task :stop, :roles => :app do
      invoke_command "sh -c 'if [ -a #{current_path}/log/backgroundrb_2000.pid ]; then #{current_path}/script/backgroundrb stop; fi;'", :via => run_method
    end
    
    desc "start the backgroundrb server"
    task :start, :roles => :app do
      invoke_command "nohup #{current_path}/script/backgroundrb start -d", :via => run_method
    end
    
    desc "restart the backgroundrb server"
    task :restart, :roles => :app do
      deploy.backgroundrb.stop
      deploy.backgroundrb.start
    end
  end
Comments and suggestions on how to improve this or make it more universal are greatly appreciated. Hope it can save somebody else some of my headaches.

tags: rails

Rails Changesets November 26th, 2007

This is kind of dumb, on my part, I admit. But maybe it will let somebody else save face a bit.

For the longest time I had wished I could have a changeset only version of the rails trac timeline, and had thought I would need to right something to filter it for me. Turns out the options are all there and I just overlooked them. Here is the link changesets only rails trac timeline. Now I can stay on top of changes to edge without needing to wade through all of the tickets.

tags: rails

backgroundrb - failed to find slave socket October 13th, 2007

Ugh. Another madness inducing bug that I post in hopes that others will have easier Googling.

I was trying to spawn backgroundrb processes. Scheduled processes worked, but when I spawned new ones they sometimes do, sometimes don't work. And I was seeing the "failed to find slave socket" error. Couldn't figure it out and Google wasn't immediately helpful. But here is what it appears to break down to.

When it creates socket files in /tmp to it truncates the worker key onto the end of your workers name. Well, there is a limit to how long that file name can be before it blows up. Looks like the name of your worker class, including underscores, needs to be 25 or less characters or it will create an invalid file name and blow up.

See also this ticket, which doesn't mention this error specifically but pointed me to the apparent answer. Hope it saves others my headache for the last week or so.

tags: rails

Reemployment August 10th, 2007

I have been busy, and hence quiet lately.

I was hired by Ramped Media to work on imthere.com. We are rolling out all kinds of new features and I think it is going to be a really great site. Really exciting time to be joining them, and its keeping me pretty busy, hence the recent silence.

In other news, some users were kind enough to point out some bugs in the user-owned tagging portions of acts_as_taggable_redux. I think they have all been smoothed out, so it is hopefully an even more complete and robust tagging solution than before. As per usual if issues come up, email me and I'll sort them out. Thanks.

tags: rails

Edgy July 24th, 2007

Holy crap, my patch made in into rails edge. I'm in core now! It is good to be in core...

tags: rails

acts_as_authenticated spasms July 10th, 2007

Real obscure error...

The mephisto blog system uses http-authentication from acs_as_authenticated to protect the overview feed(the feed details all activity on the blog).

I was modifying my dispatch.fcgi file to pass the authentication headers but it kept failing mysteriously. acts_as_authenticated splits the decoded data as "user:pass", but doesn't consider leftovers, like say a ":" in your password(as there was in mine). Then you get failures and confusion.

So don't put a colon in your password, and I will submit a patch.

[Update] Patch submitted.

tags: rails

Gamehelper Relaunch July 10th, 2007

I attended this years Game Developer's Conference back in March. While there, I met the Editor in Chief of gamehelper, Joe Markert. We chatted about web development and he asked me about helping with the final push to beta. A couple days ago we hit that milestone and the site is now live: www.gamehelper.com.

I like seeing how others approach problems and learn a lot from their examples, so I gained a lot of experience by wading into an existing code base and helping with bug fixes and enhancements. The beta site is running solid, just a bit more tire kicking left to work out the kinks.

tags: rails

Aggregation Station June 29th, 2007

Duplicating efforts is bad, so I often start by googling, "rails functionality_i_want". Often helpful, but not always. "ruby functionality_i_want" is easily overlooked. I might end up doing ActiveRecord mapping, but there is much greater diversity in this space. That is how I found FeedNormalizer, my news feed parsing library of choice.

FeedNormalizer is consistant across feed types, allows modifying and adding parsers without changing your application, and can sanitize elements with html for you.

So how do you use this miracle brew?

  1. Install
    gem install feed-normalizer
    
  2. Off to the races
    require 'open-uri'
    require 'rubygems'
    require 'feed-normalizer'
    
    file = open('http://monki.geemus.com/feed/')
    feed = FeedNormalizer::FeedNormalizer.parse file
    # Pull whatever fields you might need
    p feed.title
    p feed.entries
    # etc
    
    Quick and easy. See this intro for details about how this works.

  3. As a fun addition, you can use headers check for updates before downloading the whole feed.
    # open the feed
    data = open('http://monki.geemus.com/feed/')
    # grab the relevant meta data
    last_modified = data.meta['last-modified']
    etag = data.meta['etag']
    
    # use the feed
    feed = FeedNormalizer::FeedNormalizer.parse data
    # etc
    
    # now ask nicely for updates
    begin
      data = open('http://monki.geemus.com/feed/', 
                   {'If-Modified-Since' => last_modified, 
                   'If-None-Match' => etag})
    rescue OpenURI::HTTPError
      # No updates, so feed was not downloaded.
    else
      # Updates
      feed = FeedNormalizer::FeedNormalizer.parse data
    end
    
Pretty slick, saves publisher bandwidth and your processing time. Everybody wins!

Go forth and aggregate!

tags: rails

user owned tagging June 15th, 2007

Tagging usually involves many users individually tagging things. Often it is important to track who tagged what. I wanted to make this easy to do, so I have added user owned tagging capabilities to acts_as_taggable_redux.

The plugin can continue to be used like it was before, but you can also pass user_id to relevant functions to assign an owner. See the README for more detailed examples, and let me know how it can be improved on.

tags: rails

Singular Resources May 18th, 2007

This just bit me in the butt and I had trouble figuring out why.

If you want to use singular resources in rails (ie session) then you need to specify them as:

map.resource :session
NOTE: the resource is singular.

If you don't you'll get an error akin to:
session_url failed to generate from {:controller=>"session", :action=>"show"} - you may have ambiguous routes, or you may need to supply additional parameters for this route. content_url has the following required parameters: ["session", :id] - are they all satisfied?

It was driving me nuts. Easy to overlook and hard to find info from the googles.

tags: rails

acts_as_taggable_redux plugin released May 15th, 2007

I finished up the tagging plugin I have been working on today.

It builds on top of work that others have done and I hope improves on things in a few ways. Notably it fleshes out the rails core proof of concept acts_as_taggable and makes it much easier to use. It also adds a migration generator and tag_cloud functionality and a helper.

I will probably continue to work on it as inspiration strikes for improvement, but it works well now. You can check it out here: geemus.devjavu.com.

Shoot me an email with any questions/comments or concerns.

tags: rails

Slash and Burn May 9th, 2007

Time to rebuild this from the ruins.

My mind wanders and I ramble, but it seems like I have been gaining more clarity and focus.

I love working with Ruby on Rails. So it seems only fitting to post about my work with it here.

Thats the plan, we'll see how I do.

tags: rails