Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
# A couple of Searchlogic searches. Both are identical from a code
# perspective. With one, the user supplies a '.' (period) as part of the
# search query. This search blows up because it's generating an invalid SQL
# statement. I say it shouldn't. The error in question is:
#
# Mysql::Error: Not unique table/alias: 'users':
#
# SELECT `companies`.`id` AS t0_r0,
@jasoncodes
jasoncodes / disable_mass_assignment.rb
Created August 17, 2010 09:52
Noisy protected attributes (mass-assignment security)
ActiveRecord::Base.send(:attr_accessible, nil)
module ActionView::Helpers::FormTagHelper
def date_field(object_name, method, options = {})
ActionView::Helpers::InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("date", options.merge(:size => nil))
end
end
class ActionView::Helpers::FormBuilder
def date_field(method, options = {})
@template.send(
"date_field",
@jasoncodes
jasoncodes / example.rb
Created January 19, 2011 02:39
Example usage of `composed_of`.
class Example < ActiveRecord::Base
composed_of :input_voltage,
:class_name => 'VoltageRange',
:mapping => [%w(input_voltage_min min), %w(input_voltage_max max)],
:allow_nil => true,
:converter => :new
composed_of :output_voltage,
:class_name => 'VoltageRange',
@jasoncodes
jasoncodes / README
Created February 10, 2011 00:02
IPv6 router announcement with DNS
iOS 4.2.1 detects shows the announced DNS settings in Settings, Wi-Fi Networks within a second of starting `radvd`. The search domain does not appear to be supported.
@jasoncodes
jasoncodes / go.rb
Created March 12, 2011 03:16
API Terms of Service
#!/usr/bin/env ruby -w
require 'shellwords'
require 'fileutils'
# generate plain text versions
Dir['*.html'].each do |html_filename|
open("|elinks -dump -dump-width 9000 -no-numbering -no-references #{Shellwords.escape(html_filename)}") do |io|
@jasoncodes
jasoncodes / pivotal_tracker-fight_wrinkles.user.js
Created May 15, 2011 23:46
Pivotal Tracker Userscript to Fight UI Wrinkles
// ==UserScript==
// @name Pivotal Tracker: Fight Wrinkles
// @description Fixes a few small UI annoyances with Pivotal Tracker.
// @author Jason Weathered
// @namespace http://jasoncodes.com/
// @match https://www.pivotaltracker.com/*
// @include https://www.pivotaltracker.com/*
// @version 1.0.0
// ==/UserScript==
@jasoncodes
jasoncodes / enum_group_by_many.rb
Created December 12, 2011 23:47
Enumerable#group_by_many
Enumerable.class_eval do
def group_by_many
{}.tap do |groups|
each do |value|
keys = yield value
keys.each do |key|
group = groups[key] ||= []
group << value
end
end
@jasoncodes
jasoncodes / gist:1938675
Created February 29, 2012 06:59
Default `psql` to current Rails application
function __database_yml {
if [[ -f config/database.yml ]]; then
ruby -ryaml -rerb -e "puts YAML::load(ERB.new(IO.read('config/database.yml')).result)['${RAILS_ENV:-development}']['$1']"
fi
}
function psql
{
if [[ "$(__database_yml adapter)" == 'postgresql' ]]; then
PGDATABASE="$(__database_yml database)" "$(/usr/bin/which psql)" "$@"
@jasoncodes
jasoncodes / gist:3065508
Created July 7, 2012 08:36
Named route name from path
path = '/products/42/edit'
params = Rails.application.routes.recognize_path path
route = nil; Rails.application.routes.formatter.send(:match_route, nil, params) { |r| route ||= r }
p route.name # returns "edit_product"