Skip to content

Instantly share code, notes, and snippets.

View dkln's full-sized avatar
🤩

Diederick Lawson dkln

🤩
View GitHub Profile
@dkln
dkln / ar_to_hash.rb
Created March 11, 2011 10:45
Convert any Activerecord to a hash with all it's related objects
class Serializer < Struct.new(:object)
def to_hash
@hash ||= hash_object(object)
end
private
def hash_object(object)
hash = {}
@dkln
dkln / schema.xml
Created March 10, 2011 13:56
Ideal Dutch configuration for Solr Sunspot
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
@dkln
dkln / hash_diff.rb
Created February 21, 2011 16:24
Diff's two hashes with eachother
def diff_hash(from, to)
differences = {}
to.each do |key, value|
if value.is_a? Hash
differences[key] = diff_hash_changed_items(from[key], value)
elsif value.is_a?(Array)
differences[key] = diff_array(value, from[key])
@dkln
dkln / nested_forms.js
Created January 21, 2011 14:47
Easy adding/removing in nested forms for simple_form (javascript)
var Forms = {
generatedId: function() {
return String(new Date().getTime());
},
applyAddFieldsBehaviour: function() {
var self = this;
$('.add_fields[data-content]').each(self._applyAddFieldBehaviour);
},
@dkln
dkln / nested_forms.html.haml
Created January 21, 2011 14:47
Easy adding/removing in nested forms for simple_form
%fieldset
%h3= PressRelease.model_name.human :count => 2
= nested_fields_for form, :press_releases do |nested_form|
%fieldset
= nested_form.input :description
= nested_form.input :url
= nav_for_existing_record nested_form
@dkln
dkln / forms_helper.rb
Created January 21, 2011 14:45
Easy adding/removing in nested forms for simple_form
module FormsHelper
def nested_fields_for(form, association, &block)
field_html = form.simple_fields_for(association, &block)
new_html = form.simple_fields_for(association, new_association_object(form, association), :child_index => 'new', &block)
(field_html + nav_for_new_record(new_html)).html_safe
end
def nav_for_new_record(html_for_add)
@dkln
dkln / ie7_button_hack_fix.js
Created December 20, 2010 10:36
Makes buttons behave normally in IE7
function buttonfix() {
var buttons = document.getElementsByTagName('button');
for (var i=0; i < buttons.length; i++) {
if (buttons[i].onclick) { continue; }
buttons[i].onclick = function () {
for (j=0; j<this.form.elements.length; j++) {
@dkln
dkln / IE7_borderspacing_hack.js
Created December 17, 2010 13:19
IE7 border spacing hack
// IE7 hacks
// cellspacing hack
if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 7) {
$(function() {
$('table').each( function() {
var table = $(this);
if(spacing = table.css('border-spacing')) {
table.attr('cellspacing', parseInt(spacing));
@dkln
dkln / metrics.rb
Created December 1, 2010 15:41
Including cucumber stories in your metric_fu Rcov reports
# .metrics
cucumber_path = `which cucumber`.strip
MetricFu::Configuration.run do |config|
config.rcov[:rcov_opts] << '--aggregate tmp/metric_fu/scratch/rcov/coverage.data'
config.rcov[:rcov_opts] << '&&'
config.rcov[:rcov_opts] << "rcov #{cucumber_path}"
config.rcov[:rcov_opts] << '--no-html'
config.rcov[:rcov_opts] << '--text-coverage'
config.rcov[:rcov_opts] << '--no-color'
require 'rubygems'
require 'hirb'
# parse MERB log file
file = ARGV.first
exit unless File.exists?(file)
actions = {}
controller = nil
action = nil