Skip to content

Instantly share code, notes, and snippets.

@visnup
visnup / date_time_selector_with_meridian.rb
Created December 14, 2008 20:14
Time select elements in rails use 0-23 for the hour. I'm used to 1-12 AM/PM.
#
# <%= form.select_time :start, :meridian => true %>
#
module ActionView
module Helpers
class DateTimeSelector
def select_time_with_meridian
html = select_time_without_meridian
if @options[:meridian]
id = input_id_from_type(:hour)
@visnup
visnup / more dynamic object literals.js
Created December 19, 2008 23:17
dynamic js object literals
// a helper function to create more dynamic object literals
// $h('key'+i, value) => { key1: value }
var $h = function() {
var constructor = function() {
var k = null, args = arguments[0];
for (var i = 0; i < args.length; i++) {
if (k == null) {
k = args[i];
} else {
this[k] = args[i];
@visnup
visnup / in place edit that doesn't submit.js
Created December 20, 2008 22:53
an in place editor that doesn't submit
var editor = new Ajax.InPlaceEditor('chart_name', '', {
okControl: false,
cancelControl: false,
submitOnBlur: true,
highlightcolor: 'transparent'
});
// override the bound submit handler function to prevent
// the control from actually submitting to a url and instead
// do something more useful with the edit.
editor._boundSubmitHandler = function(e) {
@visnup
visnup / validates_length_of_less_than_max_allowed_packet.rb
Created December 21, 2008 03:31
validates_length_of_less_than_max_allowed_packet
def self.max_allowed_packet
r = connection.execute "SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet'"
r.fetch_row.last.to_i rescue 128.megabytes
end
validates_length_of :data, :in => 1 .. max_allowed_packet,
:allow_blank => true
validates_length_of :file, :in => 1 .. max_allowed_packet,
:allow_blank => true
<script type="text/javascript">
document.observe('dom:loaded', function() {
var li = new Element('li').update('li');
$('a').update(li);
var killIE = true;
if (killIE) {
li.update('new name');
} else {
$('a').update();
@visnup
visnup / drag events in mobile safari
Created February 22, 2009 05:10
minimal changes to detect touch/drag events in mobile safari
--- a/public/javascripts/application.js
+++ b/public/javascripts/application.js
@@ -70,9 +70,15 @@
if (this.input && $F(this.input))
this.positionThumb(parseFloat($F(this.input)) * this.width / 100);
- this.div.observe('mousedown', this.onMouseDown.bindAsEventListener(this));
- this.div.observe('mouseup', this.onMouseUp.bindAsEventListener(this));
- this.div.observe('mousemove', this.onMouseMove.bindAsEventListener(this));
+ if (Prototype.Browser.MobileSafari) {
@visnup
visnup / say.rb
Created February 26, 2009 08:54
xmpp4r + say
# login to jabber/gtalk and have your computer say whatever people are saying to you
require 'rubygems'
require 'xmpp4r/client'
include Jabber
# settings
if ARGV.length != 2
puts "Run with ./echo_thread.rb user@server/resource password"
exit 1
@visnup
visnup / fish_prompt.fish
Last active March 6, 2016 00:40
bash prompt
function fish_prompt --description 'Write out the prompt'
set git_sha (git rev-parse --short HEAD 2>/dev/null)
set git_branch (git rev-parse --abbrev-ref HEAD 2>/dev/null)
set git " $git_sha $git_branch "
if [ $git = ' ' ]
set git ''
end
set length (echo [ 00:00 AM -$git(prompt_pwd) ] | wc -c | tr -d ' ')
set fill (printf '%*s\n' (math (tput cols)-$length) '' | tr ' ' –)
require 'rubygems'
require 'scrubyt'
google_data = Scrubyt::Extractor.define do
fetch 'http://www.google.com/search?hl=en&q="guarded by a dragon"'
excerpt "//div[@class=s]"
next_page '//td/a', :limit => ARGV.shift.to_i || 3
end
@visnup
visnup / homework.rb
Created June 26, 2009 18:51 — forked from huned/homework
I took 15 minutes, but mine is 4 lines
require 'rubygems'
require 'open-uri'
require 'json'
url = 'http://twitter.com/statuses/user_timeline.json?id=visnup&count=3200'
dates = open(url) { |f| JSON.parse f.read }.map { |e| Date.parse e['created_at'] }
sum = dates.inject(Hash.new(0)) { |s, t| s[t] += 1; s }
sum.keys.sort.each { |k| puts "#{k} (#{sum[k]}) #{'=' * sum[k]}" }