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
@remvee
remvee / .dir-locals.el
Created August 9, 2012 11:54
Quick and dirty white space cleanup on save for selected projects with emacs.
;; variables local to this directory and its children
((nil . ((auto-whitespace-cleanup . t))))
@remvee
remvee / gist:2586086
Created May 3, 2012 14:38
Net::HTTP HEAD request to test if resource is available
def resource_available?(url)
uri = URI(url)
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == "https") do |http|
http.head(uri.request_uri)
end.code =~ /^[123]/
end
@remvee
remvee / application_controller.rb
Last active October 2, 2015 23:58
Sticky params
class ApplicationController < ActionController::Base
protected
# A version of the params map which copies members to session (per
# controller) on access. Use this to make parameters sticky; for
# instance for a page number or search fields.
def sticky_params
params.dup.tap do |m|
m.instance_variable_set("@http_session", session)
m.instance_variable_set("@http_context", self.class.name)
@remvee
remvee / mocha_helper.rb
Created December 29, 2011 10:29
Helper to gut a const/class in a spec to avoid having "expects" "never" on all methods
module MochaHelper
def with_mocked_const(name, &block)
original = Object.const_get(name)
Object.instance_eval do
remove_const(name)
const_set(name, Mocha::Mock.new(name))
end
yield
@remvee
remvee / *scratch*
Created August 17, 2011 07:55
Use wrap-basic-authentication for parts of you application.
(comment ; given
(defn authenticated? [username password]
..))
(comment ; with compojure
(defroutes public-handler
(GET ..))
(defroutes protected-handler
(POST ..))
@remvee
remvee / gist:1092491
Created July 19, 2011 14:14
AssociationCollection != Array
# Ehm..
Company.first.employees.class # => Array
Array === Company.first.employees # => false
Enumerable === Company.first.employees # => false
# But how?
ActiveRecord::Associations::AssociationCollection === Company.first.employees # => true
@remvee
remvee / slides.js
Created June 20, 2011 12:21
Very basic slide show using jquery.
/*
Simple continuous slide show using JQuery.
Usage:
<div id="slides">
<div class="slide">First</div>
<div class="slide">Second</div>
<div class="slide">Third</div>
</div>
@remvee
remvee / gist:1013936
Created June 8, 2011 07:03
mysql WTF?!
mysql> INSERT INTO tags (name) VALUES ('test ');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM tags WHERE name = 'test';
+----+-------+
| id | name |
+----+-------+
| 1 | test |
+----+-------+
1 row in set (0.00 sec)
@remvee
remvee / inheritable_views.rb
Created May 31, 2011 11:48
inheritable rails views
# Make controller render and render partial fall back to ancestor
# controllers. Very hackish because it overrides rails private
# methods. Tested on rails 2.3.5 till 2.3.11.
module InheritableViewsHelper
def render_partial_with_inheritance(options = {})
if [String, Symbol, NilClass].find{|k| k === options[:partial]}
if options[:partial].include?('/')
render_partial_without_inheritance(options)
else
err = nil
@remvee
remvee / translation_missing_asserts.rb
Created April 27, 2011 09:03
Extend rails functional test case with asserts for missing translations.
# Extend functional test case with asserts for missing translations.
# These asserts are automatically included on +assert_response+ and
# +should_respond_with+.
class ActionController::TestCase
def assert_no_missing_translations
assert(@response.body !~ %r{ class=.*translation_missing.*?(>|&gt;)(.+?)(<|&lt;\\?)/span},
"missing translation in body: #{$2.inspect}")
assert(@response.body !~ /^translation missing: (.*)$/,
"missing translation in body: #{$1.inspect}")