Skip to content

Instantly share code, notes, and snippets.

@delba
delba / application.js
Last active December 17, 2015 04:19
Setting relative date with moment.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require moment
//= require_tree .
@delba
delba / index.html
Created May 9, 2013 21:25
Using tabindex on inputs
<!DOCTYPE html>
<html>
<head>
<title>Tab Index</title>
</head>
<body>
<h1><a href="/">Title</a></h1>
<input type="search" tabindex="1" />
</body>
</html>
@delba
delba / Gemfile
Last active December 17, 2015 04:59
Using Server-send Events with Rails
gem 'puma'
gem 'redis'
@delba
delba / index.js.coffee
Last active December 17, 2015 05:18
Replacing current page with ajax response
xhr = new XMLHttpRequest
xhr.onreadystatechange = (e) ->
if @readyState is 4 and @status is 200
document.body.innerHTML = @responseXML.body.innerHTML
xhr.open 'GET', '/page'
xhr.responseType = 'document'
xhr.send()
@delba
delba / cameras.coffee
Last active December 17, 2015 06:18
Mapping Objects in CoffeeScript
data =
X100:
description: "A really cool camera"
stock: 5
X1:
description: "An awesome camera"
stock: 6
# Multiline: parentheses not required
cameras = for own name, info of data
@delba
delba / documents_controller.rb
Last active December 17, 2015 06:19
Uploading a file
class DocumentsController < ApplicationController
def create
FileUtils.mv document.tempfile, upload_path
end
private
def document
params[:document]
end
@delba
delba / file.js.coffee
Last active December 17, 2015 06:19
File uploading with ajax
reader = new FileReader
reader.onload = (e) ->
@file.data = @result
$('#file_field').on 'change', (e) ->
reader.file = e.target.files[0]
reader.readFileAsDataURL reader.file
###
@delba
delba / data_uri.rb
Last active December 17, 2015 08:59
Inline image with ruby
# data:[<MIME-type>][;charset=<encoding>][;base64],<data>
require 'base64'
file = File.new('my_file.png')
filename = File.basename(file)
mime_type = %x[file --brief --mime-type #{filename}].chomp
data = Base64.encode64(file.read).delete("\n")
@delba
delba / event.rb
Last active December 17, 2015 09:19
Creating an event with recurring dates
class Event < ActiveRecord::Base
attr_reader :start_date, :end_date, :days
has_many :performances
def build_performances
(start_date..end_date).each do |date|
next unless days.include? date.wday
self.performances << Performance.new(date: date)
end
@delba
delba / application_controller.rb
Created May 15, 2013 22:50
Raising an exception if an action isn't explicitly authorized
class ApplicationController < ActionController::Base
# ...
include AuthorizationSystem
after_filter :validate_authorization_checked
end