Loading...
This takes attribute-based ‘onpaste’ events that work in IE and Safari and makes them (mostly) work in Firefox as well. More Detail Here
function checkForPaste(event) { var e = event.element(); if ((e.previousValue && e.value.length > e.previousValue.length + 1) || (!e.previousValue && e.value.length > 1)) { if (e.onpaste) { e.onpaste(e) } else if (e.readAttribute("onpaste")) { eval(e.readAttribute("onpaste")); } } e.previousValue = e.value; } function firefoxOnPaste() { $$('textarea').each(function(e) { if (e.onpaste || e.readAttribute("onpaste")) { Event.observe(e,'input',checkForPaste); } }); } if (Prototype.Browser.Gecko) { document.observe('dom:loaded', firefoxOnPaste); }
This simple command will remove all SVN metafiles from your current directory. Comes in handy!
find . -name .svn -print0 | xargs -0 rm -rf
This quick little method makes it really easy to pull up a specific user in your app, just call User[:login] and you have them! I use it primarily in script/console.
class User < ActiveRecord::Base def User.[](key) if key.is_a?(Integer) User.find_by_id(key) else User.find_by_login(key.to_s) end end end
With this you can specify a ‘reference’ field and create a record or update a record if it matches the referenced field. Very useful for seed data in conjunction with db-populate
class << ActiveRecord::Base def create_or_update(options = {}) self.create_or_update_by(:id, options) end def create_or_update_by(field, options = {}) find_value = options.delete(field) record = find(:first, :conditions => {field => find_value}) || self.new record.send field.to_s + "=", find_value record.attributes = options record.save! record end def method_missing_with_create_or_update(method_name, *args) if match = method_name.to_s.match(/create_or_update_by_([a-z0-9_]+)/) field = match[1].to_sym create_or_update_by(field,*args) else method_missing_without_create_or_update(method_name, *args) end end alias_method_chain :method_missing, :create_or_update end
This will create a generic method on all ActiveRecords that allows you to call Model.new_today for the newest records from today.
class ActiveRecord::Base def self.new_today if self.new.respond_to?(:created_at) self.count(:all, :conditions => ["created_at > ?", Time.today], :order => "created_at DESC") else nil end end end
Use Juggernaut to push upload progress messages for uploads instead of polling the app.
require 'mongrel' require 'gem_plugin' require 'juggernaut' class Upload < GemPlugin::Plugin "/handlers" include Mongrel::HttpHandlerPlugin def initialize(options = {}) @path_info = Array(options[:path_info]) @request_notify = true Mongrel.const_set :Uploads, Mongrel::UploadProgress.new Mongrel::Uploads.debug = true if options[:debug] end def request_begins(params) upload_notify(:add, params, params[Mongrel::Const::CONTENT_LENGTH].to_i) end def request_progress(params, clen, total) upload_notify(:mark, params, clen, total) end def process(request, response) upload_notify(:finish, request.params) end private def upload_notify(action, params, *args) return unless @path_info.include?(params['PATH_INFO']) && params[Mongrel::Const::REQUEST_METHOD] == 'POST' && upload_id = Mongrel::HttpRequest.query_parse(params['QUERY_STRING'])['upload_id'] Mongrel::Uploads.send(action, upload_id, *args) end end class Mongrel::UploadProgress attr_accessor :debug def initialize end def add(upid, size) Juggernaut.send_to(upid, "UploadProgress.update(#{size}, 0);") puts "#{upid}: Added" if @debug end def mark(upid, len, size) Juggernaut.send_to(upid, "UploadProgress.update(#{size}, #{size - len});") puts "#{upid}: Marking" if @debug end def finish(upid) Juggernaut.send_to(upid, "UploadProgress.finish();") puts "#{upid}: Finished" if @debug end end