Skip to content

Instantly share code, notes, and snippets.

View duncanbeevers's full-sized avatar
🔊


Duncan Beevers duncanbeevers

🔊

View GitHub Profile
@duncanbeevers
duncanbeevers / Readme.md
Created April 8, 2011 11:00
Create a jQuery Template tag method out of a template

Expose jQuery templates as helper methods

Example: <script type="text/x-jquery-tmpl" id="timeTagTemplate"> ${$data} </script> <script type="text/x-jquery-tmpl" id="commentTemplate">

${username} {{time commented_at}}

@duncanbeevers
duncanbeevers / throttle.js
Created May 11, 2011 21:56
throttle a given function
function throttle(millis, fn) {
var sched;
return function() {
if (!sched)
sched = setTimeout(function executeAndClear() { fn(); sched = false; }, millis);
};
}
@duncanbeevers
duncanbeevers / rename_rails_migrations.rb
Created June 15, 2011 17:29
Rename Rails 1 style migration names to timestamp filenames
require 'fileutils'
Dir['./db/migrate/*.rb'].each do |from|
to = File.join(File.dirname(from),
"%014d%s" % File.basename(from).match(/^(\d+)(_.*)$/)[1..-1].map.with_index do |f, i|
0 == i ? f.to_i : f
end)
FileUtils.mv(from, to) if from != to
end
!!!
%html
%head
= csrf_meta_tags
= stylesheet_link_tag "application"
%body
= yield
= javascript_include_tag "application"
= render_inline_javascript_controller_action_asset
module SessionsHelper
def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
self.current_user = user
end
def current_user=(user)
@current_user = user
end
@duncanbeevers
duncanbeevers / augment.rb
Created January 3, 2012 23:08
Augmenting an instance with methods defined in a helper module
class Loop
attr_accessor :name
def initialize name
self.name = name
end
end
module LoopDecoratorHelper
def loop_delete_link loop
"Delete #{loop.name}"
@duncanbeevers
duncanbeevers / update_or_create.rb
Created February 4, 2012 18:38
Update Or Create
# UpdateOrCreate makes it simple to to perform updating or creating models
# without a lot of boilerplate.
#
# It can be used in several forms. In its simplest form, it can be used with a
# single OrderedHash.
#
# User.update_or_create(username: 'Admin', admin: true)
#
# The first key/value pair from the provided hash is used to look up any existing record.
# In this case, it will look for a user with the username 'Admin'
namespace :db do
desc "Populate Users Table with images from /public/feedimg"
task :populate => :environment do
require "populator"
require "faker"
image_filenames = Dir.glob(File.join(Rails.root, '/public/feedimg', '*'))
User.populate 1 do |user|
user.username = Faker::Name.first_name.downcase
user.email = Faker::Internet.user_name + "@myhost.com".downcase
@duncanbeevers
duncanbeevers / db_uptodate.rake
Created February 29, 2012 11:50
A set of rake tasks to determine whether migrations need to be run without loading the full app environment
namespace :db do
task :uptodate do
if pending_migrations.blank?
puts "Migrations are up-to-date"
else
puts "The following migrations are pending:"
puts pending_migrations.map { |mp| "%s\t\t%s" % [ mp.version, mp.name ] }.join("\n")
end
end
@duncanbeevers
duncanbeevers / sc-dl-min.js
Created March 22, 2012 12:16 — forked from pheuter/sc-dl.js
Bookmarklet that generates download link for a Soundcloud upload
(function(window){var i,$sound,$buttonGroup;var $sounds=$(".sound");var clientId=require("config").get("client_id");var oauthToken=require("lib/connect").getAuthToken();var conversionHelper=require("lib/helpers/conversion-helper");var $downloadButton,size;var params,downloadUrl,onSuccess;for(i=$sounds.length-1;i>=0;i--){$sound=$($sounds[i]);var soundcloudUrl="https://soundcloud.com"+($sound.find(".soundTitle__title").attr("href")||window.location.pathname);params={url:soundcloudUrl,client_id:clientId};onSuccess=function($sound){return function(data){var params={client_id:clientId};downloadUrl=require("lib/url").stringify({query:params},data.stream_url+".mp3");$buttonGroup=$($sound.find(".sound__soundActions .sc-button-group")[0]);size=$buttonGroup.find(".sc-button:first")[0].className.match(/sc-button-((?:small)|(?:medium))/)[1];$downloadButton=$('<a class="sc-button sc-button-download sc-button-icon sc-button-responsive">Download</a>').attr({title:"Download this sound ("+conversionHelper.bytesToMB(data.origi