Skip to content

Instantly share code, notes, and snippets.

@pixeltrix
pixeltrix / integration_test.rb
Created December 9, 2013 02:48
ActionDispatch::IntegrationTest showing that sessions, cookies and flash all work
require 'action_controller/railtie'
require 'minitest/autorun'
class TestApp < Rails::Application
config.eager_load = false
config.root = File.dirname(__FILE__)
config.session_store :cookie_store, key: 'session'
config.secret_key_base = 'secret'
end
@pixeltrix
pixeltrix / routes.rb
Created October 23, 2013 07:40
The `format: false` removes the optional segment from the end of the route but doesn't affect any path parameters so you need the regexp constraint to allow matching on periods.
NoFormatTest::Application.routes.draw do
root 'root#index'
scope '/foo/:bar', format: false, bar: /[^\/\?]+/ do
get '/', to: 'root#index'
get '/baz', to: 'root#index'
end
end
# Must have mysql installed with database 'activerecord_unittest' and user 'rails'
unless File.exists?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'mysql2'
GEMFILE
system 'bundle'
@pixeltrix
pixeltrix / array_coder.rb
Created March 30, 2013 07:06
Example of using a custom coder to read and write an array based attribute
class ArrayCoder
def initialize(default = [])
@default = default
end
def dump(object)
Array(object).join(',')
end
def load(string)
def maps_url(options = {})
options = options.dup
options.assert_valid_keys(:lat, :lng, :zoom, :width, :height)
options[:size] = “#{options.delete(:width)}x#{options.delete(:height)}”
options[:center] = “#{options.delete(:lat)},#{options.delete(:lng)}”
“http://maps.google.com/?#{options.to_query}”
end
@pixeltrix
pixeltrix / ruby-1.9.3p362-segfault.rb
Created January 2, 2013 11:10
Simple script to reproduce segfaults with Active Record in ruby-1.9.3p362. This style of scope is used by the kaminari gem to define its page scope - using a class method to define default_scope doesn't segfault.
require 'active_record'
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
ActiveRecord::Schema.define(:version => 1) do
create_table :posts do |t|
t.string :title
t.timestamps
end
end
@pixeltrix
pixeltrix / model_spec.rb
Last active October 7, 2015 15:08
How to override mass-assignment protection globally in specs
class TestSanitizer < ActiveModel::MassAssignmentSecurity::Sanitizer
def sanitize(klass, attributes, authorizer)
attributes
end
end
ActiveRecord::Base.mass_assignment_sanitizer = TestSanitizer.new
it "creates a model" do
Model.should_receive(:create).with(:name => "Foo Bar")
@pixeltrix
pixeltrix / gist:1816615
Created February 13, 2012 12:40
Diff to add a per item calculation for the WooCommerce Table Rates Shipping plugin
diff --git a/public/wp-content/plugins/woocommerce-shipping-table-rate/shipping-table-rate.php b/public/wp-content/plugins/woocommerce-shipping-table-rate/shipping-table-rate.php
index 8a7e082..01f5e47 100644
--- a/public/wp-content/plugins/woocommerce-shipping-table-rate/shipping-table-rate.php
+++ b/public/wp-content/plugins/woocommerce-shipping-table-rate/shipping-table-rate.php
@@ -242,6 +242,7 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
<th class="country"><?php _e('Destination countries/states', 'wc_table_rate'); ?></th>
<th><?php _e('Postcode', 'wc_table_rate'); ?>&nbsp;<a class="tips" tip="<?php _e('(optional) Comma separated list of ZIPs/Postcodes. Accepts wildcards, e.g. P* will match a postcode of PE30.', 'wc_table_rate'); ?>">[?]</a></th>
<th><?php _e('Exclude Postcode', 'wc_table_rate'); ?>&nbsp;<a class="tips" tip="<?php _e('(optional) Comma separated list of ZIPs/Postcodes to EXCLUDE. Accepts wildcards, e
@pixeltrix
pixeltrix / user.css
Created January 18, 2012 16:28
User stylesheet to re-enable Wikipedia
body.mediawiki > div:not(.suggestions) { display: block !important; }
body.mediawiki > #mw-sopaOverlay { display: none !important; }
traceroute to api.authorize.net (64.94.118.84), 30 hops max, 60 byte packets
1 212-74-42-254.static.alvechurch.core.datatechuk.net (212.74.42.254) 0.882 ms 1.469 ms 1.863 ms
2 peer-gi1-1.alvechurch.core.datatechuk.net (212.74.63.252) 1.637 ms 2.032 ms 2.218 ms
3 perr-geam-1a-ge340.network.virginmedia.net (82.36.0.213) 2.684 ms 3.129 ms 3.279 ms
4 perr-core-1a-ae0-0.network.virginmedia.net (62.255.149.133) 2.829 ms 3.162 ms 3.331 ms
5 manc-bb-1a-as1-0.network.virginmedia.net (213.105.175.181) 6.378 ms 7.119 ms 7.035 ms
6 brnt-bb-1b-ae1-0.network.virginmedia.net (212.43.163.86) 13.030 ms 10.798 ms 10.472 ms
7 brnt-tmr-1-ae5-0.network.virginmedia.net (213.105.159.50) 10.968 ms 11.101 ms 11.035 ms
8 telc-ic-1-as0-0.network.virginmedia.net (62.253.185.74) 12.460 ms 13.297 ms 13.325 ms
9 ldn-b2-link.telia.net (213.248.100.97) 12.615 ms 12.804 ms 13.058 ms