Skip to content

Instantly share code, notes, and snippets.


Terence Ponce terenceponce

View GitHub Profile
terenceponce /
Created Sep 26, 2012
Setting up Thinking-Sphinx on Mac OS X using Homebrew

Out of the box, Homebrew does a default installation on Sphinx:

$ brew install sphinx

However, if you're using MySQL, the thinking-sphinx gem won't work because it needs to use MySQL libraries.

If you managed to screw up the first time, uninstall sphinx first:

$ brew remove sphinx

terenceponce /
Created Sep 5, 2012
Implementing a decent trending algorithm in Rails

I'm trying to implement a trending feature. The trending feature is based on searches that have become viral in a span of 4 hours. It will get the 6 latest popular searches.

Of course, to pull that off, I created a model called Search that has a keyword field. Every search done on the application will be stored as one row in the Search table.

At the moment, this is what I'm doing to retrieve the keywords to be classified as trending:

@popular_search = Search.where('created_at >= ?', 4.hours.ago).group(:keyword).order('count_keyword DESC').limit(6).count(:keyword)
terenceponce / database.yml
Created Aug 27, 2012
Rails 3 PostgreSQL sample config
View database.yml
adapter: postgresql
encoding: unicode
database: applicationname_development
host: localhost
pool: 5
username: postgres
password: postgres
terenceponce / something.rb
Created Aug 14, 2012
What's the best way to retrieve tweets that are on a given timeframe?
View something.rb
def live_tweets
tweets = Twitter.user_timeline(self.twitter_username, :include_entities => true)
max_id = nil
since_id = nil
last_id = nil
tweets.each do |tweet|
if tweet.created_at.utc <= self.time_end.utc
max_id =
terenceponce /
Created Aug 13, 2012
Twitter gem error: unauthorized

I can't follow/unfollow users with the Twitter gem because Twitter could not authenticate me.

I've written the Twitter app credentials in config/config.yml and I've loaded it in config/initializers/app_config.rb:

APP_CONFIG = YAML.load_file(Rails.root.join('config', 'config.yml'))

After I do Twitter.follow(username), I get a Twitter::Error::Unauthorized exception

Did I miss anything?

terenceponce / index.html
Created Jul 19, 2012
jQuery Sortable issue in IE9
View index.html
Error: Object doesn't support property or method 'attr'
<div class='popover' id='playlist_menu'>
<ul class='toplay' style='overflow:auto'>
<li class='playlist_item' data-id='27' data-order='1' id='videos_27'></li>
<li class='playlist_item' data-id='1' data-order='2' id='videos_1'></li>
<li class='playlist_item' data-id='52' data-order='3' id='videos_52'></li>
terenceponce / database.yml
Created May 17, 2012
Rails 3 MySQL2 sample config
View database.yml
adapter: mysql2
encoding: utf8
reconnect: false
database: projectname_development
pool: 5
host: localhost
username: username
password: password
terenceponce / _article.html.erb
Created Apr 10, 2012
Having trouble in testing CanCan
View _article.html.erb
<% if can? :update, article %>
<%= link_to 'Edit', edit_article_path(article) %>
<% end %>
<% if can? :destroy, article %>
<%= link_to 'Destroy', article_path(article), :confirm => 'Are you sure?', :method => :delete %>
<% end %>
terenceponce / _article.html.erb
Created Apr 6, 2012
Link appears in the browser, but the test says it's not there
View _article.html.erb
<% if user_signed_in? %>
<% if == %>
<%= link_to 'Edit article', edit_article_path(article) %>
<% end %>
<% end %>
terenceponce / Console
Created Apr 3, 2012
Unit testing validatable module of Devise
View Console
1) User when email address is already taken
Failure/Error: it { should_not be_valid }
expected valid? to return false, got true
# ./spec/models/user_spec.rb:73:in `block (3 levels) in <top (required)>'