Skip to content

Instantly share code, notes, and snippets.

(function(){
var copyToClipboard = str => {
var el = document.createElement('textarea'); // Create a <textarea> element
el.value = str; // Set its value to the string that you want copied
el.setAttribute('readonly', ''); // Make it readonly to be tamper-proof
el.style.position = 'absolute';
el.style.left = '-9999px'; // Move outside the screen to make it invisible
document.body.appendChild(el); // Append the <textarea> element to the HTML document
var selected =
document.getSelection().rangeCount > 0 // Check if there is any content selected previously
@rurabe
rurabe / gist:6c869ee1a3386c803b40
Last active August 29, 2015 14:22
Reorder as a concern
module IsOrderable
extend ActiveRecord::Concern
def change_position_to(n)
safe_n = self.class.sanitize(n)
current_position = self.class.sanitize(position)
connection.execute(<<-SQL
BEGIN;
UPDATE #{self.class.table_name} SET position = position + 1 WHERE parent_id = #{parent_id} AND position < #{current_position} AND position >= #{safe_n};
UPDATE #{self.class.table_name} SET position = position - 1 WHERE parent_id = #{parent_id} AND position > #{current_position} AND position <= #{safe_n};
@rurabe
rurabe / report_locations.js
Last active August 29, 2015 14:10
A jQuery extension that reports the locations of clicks in the SVG coordinate system
(function(){
var transform = function(el,pt){
return pt.matrixTransform( el.getScreenCTM().inverse() );
};
$.fn.extend({
reportLocations : function(callback){
var $this = this;
var svg = $this.closest('svg')[0];
var pt = svg.createSVGPoint();
@rurabe
rurabe / product.rb
Created July 25, 2012 18:57
#DBC AcitveRecord Validation Practice
class Product < ActiveRecord::Base
attr_accessible :description, :imageurl, :price, :size, :title
validates_presence_of :title, :price, :description, :imageurl, :size
validates :title, :length => { :maximum => 50 }, :uniqueness => { :case_sensitive => false }
validates :description, :length => { :maximum => 250 }
validates :price, :format => { :with => /[\d\,]*\.\d{2}$/ }
validates :imageurl, :format => { :with => /#{URI::regexp}\.(jpg|png|gif)$/i }
validates :size, :inclusion => { :in => %w(small medium large) }
end
<!DOCTYPE html>
<html lang='en'>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<meta charset="UTF-8">
<style>
.cell{
width:50px ;
height:50px;
padding: 0px;
@rurabe
rurabe / ar-tutor.html
Created July 19, 2012 01:31
ActiveRecord Tutor Partial Template
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AR Tutor Example</title>
<meta name="description" content="Example tabable nav with Twitter Bootstrap">
<link href="./bootstrap.css" rel="stylesheet">
<style type="text/css">
textarea {
font-family: "Monaco",monospace
@rurabe
rurabe / baseline_survey.json
Created July 16, 2012 02:13 — forked from gchow/baseline_survey.json
Goldie's Pile of Fun
{
"survey_id": 1,
"title": "Baseline Survey (Draft)",
"introduction": "Thank you for taking the time to participate in this survey of SamaHub workers. The purpose of this survey is to better understand the background and demographics of our workers in order to continue to deliver the best work opportunities through the SamaHub. \n\nThere are no ‘wrong’ answers and all information collected will be strictly private and anonymous at the individual level. The survey is comprised of a total of 55 short questions and will take approximately 25 minutes to complete. \n\nSamaHub workers will be surveyed regularly (1-2 times a year) through the SamaHub; additionally, with the permission of the respondent, a selection of SamaHub workers may also be contacted in-person or via telephone for questions.",
"sections": [
{
"section_id": "A",
"title": "Part A: Basic Information",
"questions": [
{
@rurabe
rurabe / sql_students.xml
Created July 3, 2012 18:10
SQL Exercises - Students
<?xml version="1.0" encoding="utf-8" ?>
<!-- SQL XML created by WWW SQL Designer, http://code.google.com/p/wwwsqldesigner/ -->
<!-- Active URL: http://socrates.devbootcamp.com/sql.html -->
<sql>
<datatypes db="mysql">
<group label="Numeric" color="rgb(238,238,170)">
<type label="Integer" length="0" sql="INTEGER" re="INT" quote=""/>
<type label="Decimal" length="1" sql="DECIMAL" re="DEC" quote=""/>
<type label="Single precision" length="0" sql="FLOAT" quote=""/>
<type label="Double precision" length="0" sql="DOUBLE" re="DOUBLE" quote=""/>
@rurabe
rurabe / todo.rb
Created June 21, 2012 22:39
To Do
class Task
attr_accessor :unique_id, :description, :creation_time, :completion_time
def initialize(description,task_list)
@description = description
@unique_id = task_list.task_counter
@creation_time = Time.now
@completion_time = nil
task_list.add(self)
@rurabe
rurabe / event_manager.rb
Created June 19, 2012 07:00
Event Manager
# Dependencies
require "csv"
require 'sunlight'
# Class Definition
class EventManager
INVALID_ZIPCODE = "00000"
INVALID_PHONE_NUMBER = "0000000000"