Skip to content

Instantly share code, notes, and snippets.

View terenceponce's full-sized avatar

Terence Ponce terenceponce

View GitHub Profile
@terenceponce
terenceponce / omniauth_macros.rb
Created March 20, 2012 09:15 — forked from kinopyo/omniauth_macros.rb
Integration test with Omniauth. This example is using twitter, and assume you've installed rspec and capybara. Official document is here: https://github.com/intridea/omniauth/wiki/Integration-Testing
# in spec/support/omniauth_macros.rb
module OmniauthMacros
def mock_auth_hash
# The mock_auth configuration allows you to set per-provider (or default)
# authentication hashes to return during integration testing.
OmniAuth.config.mock_auth[:twitter] = {
'provider' => 'twitter',
'uid' => '123545',
'user_info' => {
'name' => 'mockuser',
@terenceponce
terenceponce / Console
Created April 3, 2012 08:36
Unit testing validatable module of Devise
Failures:
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)>'
@terenceponce
terenceponce / _article.html.erb
Created April 6, 2012 04:59
Link appears in the browser, but the test says it's not there
<% if user_signed_in? %>
<% if current_user.id == article.author.id %>
<%= link_to 'Edit article', edit_article_path(article) %>
<% end %>
<% end %>
@terenceponce
terenceponce / _article.html.erb
Created April 10, 2012 08:56
Having trouble in testing CanCan
<% 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
terenceponce / database.yml
Created May 17, 2012 08:34
Rails 3 MySQL2 sample config
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: projectname_development
pool: 5
host: localhost
username: username
password: password
@terenceponce
terenceponce / index.html
Created July 19, 2012 05:52
jQuery Sortable issue in IE9
<!--
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>
</ul>
@terenceponce
terenceponce / problem.md
Created August 13, 2012 11:31
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
terenceponce / something.rb
Created August 14, 2012 14:06
What's the best way to retrieve tweets that are on a given timeframe?
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 = tweet.id
break
@terenceponce
terenceponce / database.yml
Created August 27, 2012 05:24
Rails 3 PostgreSQL sample config
development:
adapter: postgresql
encoding: unicode
database: applicationname_development
host: localhost
pool: 5
username: postgres
password: postgres
test:
@terenceponce
terenceponce / problem.md
Created September 5, 2012 07:30
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)