Skip to content

Instantly share code, notes, and snippets.

@tlux
tlux / db.rake
Last active August 29, 2015 14:09
DB Drop and Truncation Tasks
namespace :db do
task drop_tables: :environment do
connection = ActiveRecord::Base.connection
connection.tables.sort.each do |table_name|
connection.execute("DROP TABLE #{table_name} CASCADE")
puts "Dropped: #{table_name}"
end
end
task truncate: :environment do
@tlux
tlux / gravatar.rb
Last active August 29, 2015 14:09
Lightweight Gravatar Implementation
class Gravatar
DEFAULTS = {
default: :not_found,
force_default: false,
size: 100,
secure: false
}.freeze
INSECURE_URL = "http://www.gravatar.com/avatar/%{hash}"
SECURE_URL = "https://secure.gravatar.com/avatar/%{hash}"
VALID_OPTIONS = :size, :default, :rating, :secure, :force_default
@tlux
tlux / submit_disabler.coffee
Last active August 29, 2015 14:10
Simple script disabling submit buttons before form submission, while still sending the submit input value to the targetted action
AJAX_LINK_SELECTOR = 'a[data-remote=true]'
AJAX_BUTTON_SELECTOR = 'button[data-remote=true][type=button]'
createArbitraryField = ($submit) ->
$form = $submit.closest('form')
$('<input />', type: 'hidden', name: $submit.attr('name'), value: $submit.val()).appendTo($form)
destroyArbitraryField = ($submit) ->
$form = $submit.closest('form')
buttonName = $submit.attr('name')
@tlux
tlux / duration.rb
Last active August 29, 2015 14:10
Struct representing a Duration
# composed_of :duration, class_name: 'Duration', mapping: %w(duration to_i), constructor: :parse, converter: :parse
class Duration
SEPARATOR = ':'
SECONDS = 0...60
MINUTES = 0...60
HOURS = 0...24
include Comparable
@tlux
tlux / rfc5322.rb
Created November 27, 2014 10:51
Representation of a User's E-Mail-Adress in RFC5322 format
class User
def full_name
"#{first_name} #{last_name}"
end
def rfc5322
if full_name.present?
%{"#{full_name}" <#{email}>}
else
email
@tlux
tlux / jquery.idleTimeoutable.coffee
Last active August 29, 2015 14:11
Make elements timeoutable when no activity has been made for a number of seconds
# Requirements:
# - jQuery
# - UnderscoreJS
#
# Usage:
# $('#element').idleTimeoutable()
# $('#element').idleTimeoutable(after: 3000) # milliseconds
# $('#element').idleTimeoutable(idleClass: 'inactive')
TIMEOUT_STORE_KEY = 'idleTimeout'
@tlux
tlux / age.rb
Created February 10, 2015 23:36
Age Calculator
class Age
attr_reader :birthday
def initialize(birthday)
@birthday = birthday.to_date
end
def age
today = Date.today
result = today.year - birthday.year
@tlux
tlux / rotate.rake
Created February 19, 2015 20:23
Simple Log Rotate for Rails Applications
namespace :log do
desc 'Compresses the current application log for the environment'
task :rotate do
store_path = "#{Rails.root}/log"
logfiles = Dir[File.join(store_path, '*.log')]
logfiles.delete_if { |l| File.size(l).zero? }
if logfiles.any?
log_filenames = logfiles.map { |l| File.basename(l) }.join(' ')
archive_filename = "logs_#{Time.now.strftime('%s')}.tgz"
`cd #{store_path} && tar -zcvf #{archive_filename} #{log_filenames}`
@tlux
tlux / array_finders.rb
Created March 3, 2015 20:57
ActiveRecord like finders for Arrays
class Array
def find_by!(attributes)
find_by(attributes) || fail('No element found')
end
def find_by(attributes)
detect do |item|
attributes.collect do |key, value|
item.public_send(key) == value
end.reduce(:&)
@tlux
tlux / turbolinks_scroll_restorer.coffee
Created March 25, 2015 11:38
Turbolinks Scroll Restorer: Restores the scroll position after a page change with Turbolinks