Skip to content

Instantly share code, notes, and snippets.

View remvee's full-sized avatar

Remco van 't 🪶 remvee

  • 52°N, 5°E
View GitHub Profile
(function($) {
$.fn.highlight = function(options) {
var $el = $("<div></div>"),
delay = (options && options["delay"]) || 0;
var container = $(this)[0];
if ($(this).parents("table").length) {
container = $(this).parents("table")[0];
}
# Queue all email with delayed job automagically.
module DelayedMailer
extend ActiveSupport::Concern
included do
alias_method_chain :deliver, :delayed_job
end
class MailJob < Struct.new(:message)
#!/usr/bin/env ruby
require File.dirname(__FILE__) + "/../config/boot"
require File.dirname(__FILE__) + "/../config/environment"
dir = File.dirname(__FILE__) + "/../db/migrate"
migrations = ActiveRecord::Migrator.new(:up, dir).pending_migrations
if migrations.present?
puts "#{migrations.size} pending migrations:"
migrations.each do |m|
class String
def to_proc
lambda{|it| self.gsub("%{it}", it)}
end
end
>> Faker::Lorem.paragraphs.map(&"<p>%{it}</p>")
=> ["<p>Voluptate occaecati quia tempora corrupti. Esse ea molestias suscipit aliquid eligendi. Deleniti reiciendis quas exercitationem quis commodi. Est eos ut dicta quia consequuntur quia accusantium.</p>", "<p>Optio deserunt aut sunt enim earum omnis. Alias aperiam provident tempora laboriosam ratione ut. Ratione qui blanditiis aut nobis in ab consectetur. Ex voluptatibus harum recusandae non quis ut. Sit est itaque distinctio dolor.</p>", "<p>Quia molestiae labore sit. Libero exercitationem minima dolorem omnis ullam quo sapiente. Quae ipsa maxime qui iure facere occaecati repudiandae. Deserunt hic est dolor sint.</p>"]
@remvee
remvee / i18n.rb
Last active December 18, 2015 03:49
Rake task to sort i18n yaml keys
namespace :i18n do
task :sort_keys do
Dir[Rails.root + "config/locales/*.yml"].each do |fname|
text = to_yaml(sort_by_key(YAML.load(File.read(fname)))).strip + "\n"
File.open(fname, "w"){|fd| fd.write(text)}
end
end
end
def sort_by_key(m)
# PNG
IO.read('image.png')[0x10..0x18].unpack('NN')
#=> [320, 200]
# GIF
IO.read('image.gif')[6..10].unpack('SS')
#=> [320, 200]
# BMP
d = IO.read('image.bmp')[14..28]
/*
Optionally load javascripts when some condition is met. This code
ensures the scripts are loaded sequencially to avoid dependency
problems.
In this case: when an element with id "yelp" exists load jquery and
execute some javascript (depending on jquery) to replace the
innerHtml of that element with "YELP".
*/
require 'spec_helper'
require 'rails/source_annotation_extractor'
describe "Scanning for pending BUG, FIXME and TODO annotations in code" do
# annotation extractor expects to be run from app root
Dir.chdir(File.dirname(__FILE__) + "/..")
SourceAnnotationExtractor.new("BUG|FIXME|TODO").find do |res|
res.keys.sort.each do |file|
require "ostruct"
require "active_support/concern"
require "active_model/validations"
require "active_model/errors"
# Soft validations are based on ActiveModel::Validations but don't
# block saving a record.
#
# class Person < ActiveRecord::Base
# include SoftValidations
@remvee
remvee / human_reabable_seconds.rb
Created November 30, 2012 12:22
Function to create strings like "6d 4h 1h 34m 23s" from an amount of seconds.
# human_readable_seconds:
# s: "%{s}s"
# ms: "%{m}m %{s}s"
# hms: "%{h}h %{m}m %{s}s"
# dhms: "%{d}d %{h}h %{m}m %{s}s"
def human_readable_seconds(seconds)
attrs = {s: 60, m: 60, h: 24, d: nil}.reduce([1, {}]) do |(multiplier,result),(key,modulo)|
if seconds / multiplier > 0
if modulo
[multiplier * modulo, {key => (seconds / multiplier) % modulo}.merge(result)]