Skip to content

Instantly share code, notes, and snippets.

View kineticac's full-sized avatar

Arthur Chang kineticac

View GitHub Profile
@kineticac
kineticac / athlete_search.rb
Created February 26, 2013 19:02
Sunspot searchable block moved to extension module using ActiveSupport::Concern
#extend this from Athlete model
module Extensions::AthleteSearch
extend ActiveSupport::Concern
included do
searchable :auto_index => true, :auto_remove => true do
# all your searchable code
end
end
end
@kineticac
kineticac / sidekiq.rb
Created December 23, 2012 08:59
Here are my configs for sidekiq
require 'sidekiq'
require 'autoscaler/sidekiq'
require 'autoscaler/heroku_scaler'
heroku = nil
if ENV['HEROKU_APP']
heroku = Autoscaler::HerokuScaler.new
end
Sidekiq.configure_server do |config|
@kineticac
kineticac / coach.rb
Created July 12, 2011 22:20
custom unique method for coach arrays
def self.unique(coaches)
return coaches if coaches.empty?
raise ArgumentError, "requires array of coaches as an argument" if(coaches.class != Array || coaches.first.class != Coach)
coach_ids = []
unique_coaches = []
coaches.each do |coach|
unless coach_ids.include?(coach.id)
coach_ids << coach.id
unique_coaches << coach
end
@kineticac
kineticac / show.html.erb
Created July 12, 2011 22:17
Using the attr_accessor
<% for coach in @interested_coaches do %>
<%= time_ago_in_words coach.interested_at %>
<% end %>
## coach.rb
attr_accessor :interested_at
## athletes_controller.rb
coach_bookmarks = bookmarks.map do |bookmark|
coach = bookmark.coach
coach.interested_at = bookmark.created_at
coach
end
@kineticac
kineticac / athletes_controller.rb
Created July 12, 2011 22:12
eager loaded coaches go into an array
@coaches = bookmarks.map{|bookmark| bookmark.coach} + connections.map{|connection| connection.coach}
@interested_coaches = Coach.unique(@coaches) # I created a custom unique handler for Coach objects
@kineticac
kineticac / athletes_controller.rb
Created July 12, 2011 22:09
A bookmark find eager loading the coach
bookmarks = Bookmark.find(:all, :conditions => ['athlete_id = ?', @athlete.id], :include => :coach)
@kineticac
kineticac / athletes_controller.rb
Created July 12, 2011 22:08
Connection eager loading Coaches
connections = Connection.find(:all, :conditions => ['athlete_id = ?', @athlete.id], :include => :coach)
@kineticac
kineticac / query_response_handler.js
Created March 20, 2011 03:34
Wait on Facebook permissions FQL request
// assuming we already have the query created, wait on it with a callback
query.wait(function(permissions){
// master list for granted permissions
var grantedPermissions = new Array();
// your basicPerms converted into an array "read_stream,user_interests"
var permArray = new Array();
permArray = basicPerms.split(",");
@kineticac
kineticac / permission_query.js
Created March 20, 2011 03:27
This is the way to create a query to ask for a set of permissions from Facebook using the Facebook Javascript SDK
var basicPerms = "read_stream,user_interests";
var queryString = 'select ' + basicPerms + ' from permissions where uid=me()';
var query = FB.Data.query(queryString);