Skip to content

Instantly share code, notes, and snippets.

View justinko's full-sized avatar

Justin Ko justinko

View GitHub Profile
class PostsController
def index
@posts = Post.fetch_all
rescue Post::FetchError
redirect_to root_url
end
def recent
@posts = Post.fetch_recent
render 'index'
class PostsController
def index
@foo = i_want_this_evaluated_in_the_controller
end
end
# focused_controller
module PostsController
class Index
@justinko
justinko / gist:3295724
Created August 8, 2012 15:06
Solr with Delegation
require 'delegate'
require 'singleton'
class Solr < DelegateClass(RSolr::Client)
include Singleton
def initialize
super RSolr.connect(url: url)
end
@justinko
justinko / gist:2893133
Created June 8, 2012 02:25
App that aggregates and stores all of your OSS contributions

THE PROBLEM

As OSS participants, I think it is important to keep your contributions documented. Contribution is not limited to pushing code. Forums, documentation, IRC, StackOverflow, even twitter are all avenues to "helping out". Sadly, potential employers, and even other developers, never see all of this effort you put forth.

THE SOLUTION

The solution to this problem could be an app that aggregates all of this data into a simple "timeline". Github, Google Groups/forums, StackOverflow, IRC, etc. could all be potential sources. Data would never be deleted. This would be a free service with sponsors.


@justinko
justinko / Plea.markdown
Created May 30, 2012 19:40
Am I doing it wrong?

Dear Rubyists,

I just lost a contract because of my code in a Rails project.

The specific code in question is related to a "posting a comment" feature. Here are the details:

In this project, "posting a comment" does not simply entail inserting a row into the database. It involves a procedure to yes, insert a row, but also detect its language, check for spam, send emails, and "share" it to Twitter and Facebook. I believe this algorithm should be encapsulated. I do not believe it belongs in a controller or a model. I do not believe Active Record callbacks should be used.

The "senior developer", whom is the stake holder's right hand man, said this:

@justinko
justinko / gist:2357038
Created April 11, 2012 05:05
A potential solution (very rough!) to Rails ivars-in-views problem
ActionController::Base.class_eval do
def self.context(*actions, &block)
contexts[actions] << block
end
def self.contexts
@contexts ||= Hash.new {|h, k| h[k] = [] }
end
end
@justinko
justinko / current-output.txt
Created March 18, 2012 06:22
More compact logging for RSpec - based on feedback from Ara Howard
➜ rspec-expectations git:(fail-fast) ✗ bin/rspec spec/functional
Run options: include {:focused=>true}
All examples were filtered out; ignoring {:focused=>true}
.
Finished in 0.00592 seconds
1 example, 0 failures
Randomized with seed 41776
@justinko
justinko / application.rb
Created March 16, 2012 10:33
Setup Devise in your application.rb instead of the models
module MyApp
class Application < Rails::Application
config.to_prepare do
User.devise :database_authenticatable, :recoverable, :rememberable,
:trackable, :validatable
Admin.devise :database_authenticatable, :recoverable, :rememberable,
:trackable, :validatable
end
end
@justinko
justinko / seed.rb
Created February 27, 2012 01:24
FactoryGirl for dev seeding and tests
# lib/tasks/app.rake
namespace :app do
desc 'Seed the current env db with "dummy" data'
task seed: :environment do
require './lib/seed'
Seed.start
end
end
@justinko
justinko / gist:1916757
Created February 26, 2012 13:37
No instance variables in Rails controllers
class PostsController < ActiveRecord::Base
helper_method :posts, :post
def index; end
def create
if post.save
end
end