Skip to content

Instantly share code, notes, and snippets.

Jacob Swanner jswanner

Block or report user

Report or block jswanner

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@jswanner
jswanner / address.rb
Created Jul 18, 2012
Haversine formula with Arel, with possibility of portability.
View address.rb
class Address < ActiveRecord::Base
def self.order_proximity_to(latitude, longitude)
order(Arel::Nodes::Haversine.new(arel_table[:latitude],
arel_table[:longitude],
latitude,
longitude))
end
end
@jswanner
jswanner / 0-Ruby-1.9.3-p194
Created Jul 17, 2012
Compare: gem executable, bundler binstubs, bundle exec gem executable
View 0-Ruby-1.9.3-p194
$ time rspec --help > /dev/null
real 0m0.292s
user 0m0.221s
sys 0m0.064s
$ time ./bin/rspec --help > /dev/null
real 0m1.167s
user 0m1.017s
sys 0m0.137s
@jswanner
jswanner / 0-readme.md
Created Jul 17, 2012
ruby-1.9.3-p194 cumulative performance patch.
View 0-readme.md

Patched ruby 1.9.3-p194 for 30% faster rails boot

What is this?

This script installs a patched version of ruby 1.9.3-p194 with patches for boot-time performance improvements (#66 and #68), and runtime performance improvements (#83 and #84). It also includes the new backported GC from ruby-trunk.

Huge thanks to funny-falcon for the performance patches.

@jswanner
jswanner / application.rb
Created Jun 22, 2012
Basic auth for certain RefineryCMS page.
View application.rb
module MyApplication
class Application < Rails::Application
config.to_prepare do
Refinery::PagesController.class_eval do
before_filter :require_auth, :only => [:show]
def require_auth
if params[:id] == 'secret-page'
authenticate_or_request_with_http_basic('Administration') do |username, password|
username == 'admin' && password == 'password'
end
@jswanner
jswanner / gist:2002935
Created Mar 8, 2012
Unique table alias names
View gist:2002935
terms = ['asdf', 'ghjk']
alias_numbers = (1..terms.length).to_a
tags = Tags.arel_table.alias "tags_#{alias_numbers.shift}"
View index.html.haml
%script#extras-column-template(type="text/x-jquery-tmpl")
%div.extras(data-bind="foreach: extrasAndExpenses")
%div
/ ko text: text
/ /ko
%a.delete-link X
# OR
%script#extras-column-template(type="text/x-jquery-tmpl")
View keypress.html
<!DOCTYPE html>
<html>
<head>
<title>on key press test</title>
<script>
function keyPress(e) {
var keynum, keychar;
if(window.event) { // IE
keynum = e.keyCode;
@jswanner
jswanner / gist:1324770
Created Oct 29, 2011
Get full sentence for query
View gist:1324770
> text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.'
> query = 'veniam'
> pos = text =~ /#{query}/
=> 142
> prev_period = text.rindex('.', pos) || -1
=> 123
> next_period = text.index('.', pos) || -1
=> 231
> sentence = text[prev_period+1..next_period]
=> " Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
View gist:1302999
# before
def self.front_page
where(:front_page => true).select('*, coalesce(position, 999999999) AS position').order('position ASC')
end
# after
def self.front_page
where(:front_page => true).select([
Arel.sql('*'),
Arel::Nodes::NamedFunction.new('COALESCE', [arel_table[:position], 999999999]).as('position')
@jswanner
jswanner / refineryedge.rb
Created Oct 20, 2011
Installing RefineryCMS on Edge
View refineryedge.rb
gem 'refinerycms', :git => 'git://github.com/gogogarrett/refinerycms.git', :branch => 'heroku'
run 'bundle install'
generate 'refinery:cms'
rake 'db:migrate'
append_file 'Gemfile' do
" # group :development, :test do
# gem 'refinerycms-testing', '~> 2.0'
# end
You can’t perform that action at this time.