Skip to content

Instantly share code, notes, and snippets.

View duncanbeevers's full-sized avatar
🔊


Duncan Beevers duncanbeevers

🔊

View GitHub Profile
@duncanbeevers
duncanbeevers / jquery.angleTo.js.coffee
Created April 14, 2012 00:48
Find the angle between the center of an element and a coordinate pair, or the center of another element
(($) ->
# Finds the angle between the center of the first element
# and the provided point, or the center of another element
degreesPerRadian = 180 / Math.PI
$.fn.angleTo = () ->
args = arguments
if 1 == args.length
target = $ args[0]
position = target.offset()
targetX = position.left + target.outerWidth() / 2
class ApplicationModel
extend Feedable
end
@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
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 / 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'
@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}"
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
!!!
%html
%head
= csrf_meta_tags
= stylesheet_link_tag "application"
%body
= yield
= javascript_include_tag "application"
= render_inline_javascript_controller_action_asset
@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
@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);
};
}