Counting rows and modifying MySQL to work with Postgres or Heroku

Now I am moving on to Open Translation Project. I’ve done some translation work before, including one of Paul Graham’s essay – Why to not not start a startup. BTW, he finally made a link from the essay to my translation. I used Google Translate as base, but I couldn’t believe how bad the translation was. Yahoo’s Babel Fish was a little better, but not as much. That’s where I got the idea of creating this possibly massive project.

Anyhow, I wanted to find a way of selecting an article or blog that was translated the most. I had one model that stored basic information of original article/blog. Then its children are translations. So, I need to count rows of children with the same parent. In MySQL, I had the following statement in Rails.

@top_origs = OrigPost.find(:all,
                              :select => 'orig_posts.*, count(posts.id) as post_count',
                              :joins => 'left outer join posts on posts.orig_post_id = orig_posts.id',
                              :group => 'orig_posts.id',
                              :order => 'post_count DESC',
                              :limit => 5)

Continue reading

How to integrate Clickpass (and OpenID) with a Rails app

I saw Clickpass in action at the Hacker News, and I thought it was another great way to reduce another login account. I wanted to implement it for my Open Translation project.

However, when I tried to find tutorials for using Clickpass with a Rails app, I couldn’t find any! How could it be! The pesudo code example Clickpass provided was for Java, I think, and thus it wasn’t any help to me. I was completely lost.

Then I realized that in the core underlying architecture of Clickpass is OpenID. Then it all made sense to me. I found a great tutorial on OpenID and Authlogic on a Railscasts episode (I am not using Authlogic for my site, though, but for the Open Translation project, I probably will.). So the following is a mixture of the Railscasts episode, ruby-openid gem, open_id_authentication plugin and Clickpass setup.

1. Install the gem and the plugin.

sudo gem install ruby-openid

Continue reading

How to integrate Facebook Feed with a Rails app

On Monday, I’ve gotten Facebook Feed publishing to work with my site, and it took about two hours including TV watching time. I could’ve done it faster if I actually paid a full attention. It was all possible, thanks to Chris Schmitt, who has an excellent tutorial on his site.

1. Simply you first need to create a sub-class inherited from Facebooker::Rails::Publisher inside a controller. In my case, I wanted to publish a feed when a new playground is added and an existing playground is edited, so it made a sense to put it in playgrounds controller. publish_pg takes objects and sets parameters, and publish_pg_template creates a feed message based on those parameters.

class PlaygroundsController < ApplicationController

[SNIP - other actions]

  class FacebookPublisher < Facebooker::Rails::Publisher

    def publish_pg_template
      one_line_story_template "{*actor*} created/updated: {*pg_name*}"
      short_story_template "{*actor*} created/updated: {*pg_name*} in {*pg_city*}, {*pg_state*}",
                           "Check out what {*actor*} said, and rate or add comments to help other parents!"
    end

    def publish_pg(pg, facebook_session)
      send_as :user_action
      from facebook_session.user
      data :actor => facebook_session.user.first_name, :pg_name => pg.name, :pg_city => pg.city, :pg_state => pg.state, :pg_id => pg.id
    end

  end

end

Continue reading

Deploying a Rails app on Heroku (paperclip, gems, yml)

I just moved my PlaygroundsRUs site from AWS to Heroku, and I couldn’t be happier. My first full month bill from Amazon was about $75, and $74 of that (99% of the total cost) was for running an instance. Harlan told me about Heroku after he deployed his ForkThis demo on there.

Most of the transition was smooth, but there were a few hiccups on the way. One of them still hasn’t been resolved (one of the plugins is having conflict with PostgresSQL, which is used by Heroku). I will enumerate what I had to go through so that it might be easier for you.

1. Secret YAML files

I have a public GitHub account for deploying on AWS using ec2onrails. Since it’s public and anyone can see it, I had to omit sensitive

.yml

files in config directory that contained passwords and keys. It’s easily done by specifying those files in

.gitignore

and listing them in

:nonvc_configs

in

config/deploy.rb

used by ec2onrails.

I found out that for Heroku, the same can be achieved by creating another branch, including those files, merging with master, and pushing it to Heroku. So, the following lines should do the trick.

git checkout production
[remove those yml files from .gitignore]
git merge master
git push heroku production:master

Continue reading

How to integrate Google Maps in a Rails app

The major part of my experiment was going to be the integration with Google Maps. At first, I was a bit apprehensive about it, but Ruby on Rails being a community oriented language, I found a couple of quite useful plug-ins. With them, finding a location based on address or geocode or finding nearby places based on certain radius of a location were not too hard.

There is actually an excellent book also, called Beginning Google maps applications with Rails and Ajax. But the problem with the book is that it was geared towards more for java developers using rails. Thus, its major functions were javascripts and it showed how to talk to rails app. In my case, since I wanted simple features and I didn’t want to take time to learn javascript, I decided to find a simpler way.

And I found that for things I wanted to do, I could do with YM4R/GM and Geokit. There are other excellent tutorials out there like the one by Jason Gilmore on Developer.com.

Let’s begin.

1. Sign-up for Google Maps API by going to here. For development environment, you can use either http://localhost:3000 or http://127.0.0.1:300 as the website URL. Copy the API key.

When you move to production environment, make sure you come back here to get another key for production URL.

Continue reading

How to integrate Facebook Connect with a Rails app

Update: I have written up another post for using Devise and Omniauth, and you can find it here. Facebooker is no longer maintained.

Top of my to-do list was to integrate Facebook Connect with my Rails app, since 1) there are A LOT of people using Facebook and 2) having to register to post or edit could be an obstacle in getting more users to use my site.

I looked on the web for a while, and found a great example by Stuart Eccles at Made by Many. It’s an awesome tutorial, but it’s for restful_authentication. I don’t use it, so I had to modify it a little bit. Also, I added a step to ask a user to pick a username. So the following instruction is basically modification of Stuart’s.

Without further ado, let’s begin.

1. Setup Facebook Application page

1.1. Go to this page, and enter a name for your application. I named mine “Playgrounds_R_Us”.

1.2. Make a note of Application ID, API Key, and Secret. You need it for facebooker.yml later.

1.3. Next is Authentication section. Here what’s important is Post-Authroize and Post-Remove Callback URL. They refer to a web page a user will be taken to after logging into and logging out of Facebook account. While testing, I left it at “http://127.0.0.1:3000”.

1.4. I also used the same URL (http://127.0.0.1:3000) for Canvas Callback URL in Canvas section and Connect URL in Connect section.

1.5. Then you are pretty much set with configuration on Facebook side.

2. Get facebooker plugin and install it. From your rails app root directory,

ruby script/plugin install git://github.com/mmangino/facebooker.git

Continue reading

Alpha version of Playgrounds R Us is launched

I’ve been working on it for three months or so, and it’s gotten to a point where it’s usable. It’s my first Ruby on Rails application, and this was a lot better and easier than when I tried to learn it a few years ago. That’s not to say it was that easy. I certainly have stumbled a lot, but web has been good to me. Any road blocks, I can always search for a solution online. Also many plugins have made my life very easy. I’ve resolved many issues, and I wish I could have made a record of how I resolved them. In fact, I will try to do that in my next several postings. I have found those instructional blog posts very helpful, and hopefully I will contribute some back to the community.

Website: Playgrounds R Us

Plugins used:

Gems used: