Skip to content

Instantly share code, notes, and snippets.

@house9
house9 / secure-controller-shared-spec.rb
Created August 28, 2015 19:48
Shared spec for checking role security for API controller
shared_examples_for "a secure v1 api" do |endpoints|
endpoints.each do |endpoint|
endpoint.each do |action, data|
http_method = data[:request] || :get
roles = data[:roles] || []
roles << 'admin'
parameters = (data[:with] || {}).merge(format: 'json')
describe "#{http_method}: #{action}" do
User::ROLES.each do |role|
@house9
house9 / pairs.rb
Last active August 29, 2015 13:56 — forked from lexun/pairs.rb
require 'ap'
$pair_size = (ENV['PAIR_SIZE'] || 2).to_i
def pairs(participants)
pairs = participants.shuffle.each_slice($pair_size).to_a
if pairs.last.size == 1
loner = pairs.pop
pairs.last << loner.first
Jill
Jack
Jim
Jane
Frank
Bob
Joe
Nancy
Alex
Somebody
set PAIR_SIZE=3
ruby pairs.rb people.txt
set /p=Hit Enter to quit...
// in case using alongside prototype
var $j = jQuery.noConflict();
$j(document).ready(function()
{
// apply the disable onclick for buttons with css class of 'disable-on-click'
$j(".disable-on-click").bind('click', function(e) {
$j(this).attr("disabled", "true").attr("value", "Please wait...");
});
}
-- mssql 2000
SELECT db_name(dbid), *
FROM master.dbo.sysaltfiles
ORDER BY size DESC
-- mssql 2005 and above (untested)
SELECT db_name(database_id) database_name, *
FROM sys.master_files
class CreateWidgets < ActiveRecord::Migration
def self.up
create_table :widgets do |t|
t.string :name, :limit => 255, :null => false
t.timestamps
end
end
def self.down
drop_table :widgets
# before the work-around
class Widget
end
# after the work-around
require 'base64'
class Widget
# override the rails attribute (getter)
def name
Base64::decode64(self[:name])
# this worked as expected on mysql
# on postgresql it created a new widget with id of 0
def some_method
widget = Widget.find_or_initialize_by_id(params[:widget_id])
# ...
end
# workaround code for postgresql
def some_method
widget = Widget.find_or_initialize_by_id(params[:widget_id])