Lifejot.com re-launched
October 24th, 2007
Man we've been busy!
We threw our hat in the Rails Rumble ring with our buddies Mike and El. We didn't win but 48 hours of pure coding was a rush and our app worked. Good times.
We've been working on a few different projects, too. Lifejot.com is our recently re-launched shopping list maker. JasonC started this one a couple years ago using .net. It was working fine but we needed to add some features and the site needed a facelift. There was no way we were going to hassle with doing it in .net. Converting this app to Rails was a piece of cake.
Lifejot lets a user create unlimited shopping lists. Usually these lists are created by store but you can create them however you like; Shopping list, Christmas list, Wish list, any list will do. Each list has drag-and-drop reordering. Most of the forms are ajax-loaded and most parts of the main list page are also ajax-refreshed. Lists can be printed, emailed, and merged or copied. It's a great improvement over v1!
We're also working on some ideas to monitize the site. I don't think we'll ever charge a user but we think there are many opportunities to partner up with services and, of course, advertisers. We'd love to hear if anyone has any other ideas.
has_many_polymorphs saves lives
April 26th, 2007
So here I am tearing through my tickets when all of a sudden my ticket shredder jams. On what? Jason wants me to do some fancy filtering of a list that requires some :joins, some :conditions, and it has to work through a has_many :through join that’s already there. Oh, and since the items are also tagged it has to use acts_as_taggable, too.
Is he asking too much? I don’t know. I guess filtering a list by a search value, date created, a related person object, and a tag isn’t asking too much. I mean it’s for our users, right?
So being the code monkey I am, I plunge forward. I cleared the jam on my shredder, lubed it up a little and started ‘er up again.
My initial solution was, as it was with everything related to search and filtering of my objects so far, to just add another custom finder to acts_as_taggable.rb. I mean I already had hacked that thing to the point that it didn’t resemble the original file much. And I know, I shouldn’t have been putting things RIGHT into that file anyway but overriding it’s methods somewhere else in case I ever updated the plugin…I know.
Here is an example of one of my acts_as_taggable hacks…
def find_tagged_with_and_conditions(list,conditions,page=1,page_size=10)
find_by_sql([
"SELECT distinct #{table_name}.* FROM #{table_name}, tags, taggings " +
"WHERE #{table_name}.#{primary_key} = taggings.taggable_id " +
"AND taggings.taggable_type = ? " +
"AND taggings.tag_id = tags.id AND tags.name IN (?) and #{conditions}" +
" order by created_at desc " +
" limit " + ((page -1) * page_size).to_s + ", " + page_size.to_s,
acts_as_taggable_options[:taggable_type], list
])
end
I know it’s terrible. I know I’m breaking all kinds of rules. But hey, it’s our first “real” rails app. We’re still trying to scrub off the ASP.Net.
It’s pretty obvious what that does. It lets me get a set of results based on a tag, some conditions, and it implements some paging. Pretty straightforward once you get past all the sacrilege of how I’m doing it. It IS semi-DRY since it will work on any model that I’ve set as acts_as_taggable. So it’s…OK.
Where I run into a problem is when I have to do that same thing but through a has_many :through join model. You see I have People and I have Invitations and they join through Invitees. Invitees has some other data in it (which is why I’m not just using HABTM). The filtering I need to do is on a list of Invitations but one of the criteria is that I need to filter by who the invitation was sent to, an Invitee. Thanks Jas!
I started looking around for a solution to tagging that didn’t use acts_as_taggable. Some say it’s deprecated. I don’t know. I ran across Evan Weavers blog entry about has_many_polymorphs and how it could be used for tagging. I read through all the comments and a couple related blog entries and it looked like it would help me get where I needed to go. I jumped on IRC#polymorphs (the IRC channel for the plugin) to ask around. cdcarter was the only one in the channel that was available. He was great moral support! He suggested that I just make a branch in Subversion and hack away on the branch with HMP (has_many_polymorphs). Sounded good to me! And hey, I had never made a branch before. I was learning all kinds of new stuff!
HMP installed easily enough. Evan’s even gone through the trouble of including a generator that will make the tagging parts for you. Since I already had some of the parts I didn’t go that route but it looks like the way to go when starting from scratch. The only glitch I ran into was that I had to add require 'active_record' to my extension lib so that it would recognize ActiveRecord::Base Not sure what is going on there but it’s working.
tag=Tag.find_by_name(@tags_filter)
@invitations=tag.invitations.find(:all,
:conditions=>@conditions,
:include=>:people,
:offset => (@page -1) * @per_page,
:limit => @per_page,
:order => 'invitations.created_at DESC')
I have some more work to do to DRY it up but it has simplified my code tremendously. Thanks Evan!
I have to also thank the guys over at The Rails Way for reminding me of the power of join models and the “right” way to use them.