Skip to content

Instantly share code, notes, and snippets.

View coffeeaddict's full-sized avatar

Hartog C. de Mik coffeeaddict

View GitHub Profile
class SomeController < ApplicationController
before_filter :check_from_invoice
before_filter :session_is_empty, :only => [:create]
def check_from_invoice
if some_condition
skip_before_filter :session_is_empty
end
end
@coffeeaddict
coffeeaddict / rails_test
Created December 14, 2010 14:54
Test a unit/functional file or test with little effort
#!/usr/bin/env ruby
#
# = USAGE
# rails_test (f|functional|u|unit|p|performance|i|integration) test_case_name test_name
#
# = SYNOPSIS
#
# rails_test f customers
# # will run functional/customers_controller.rb
#
@coffeeaddict
coffeeaddict / login_controller.rb
Created December 7, 2010 14:52
modular controller
class LoginController < ApplicationController
skip_before_filter :check_login
skip_before_filter :check_rights
include LoginMethods
end
> LoginController.action_methods
=> #<Set: { ... }> # only action methods from ApplicationController show up here
class A
has_many :abcees
has_many :bees, :through => :abcees
has_many :cees, :through => :abcees
end
class Bee
end
class Cee
@coffeeaddict
coffeeaddict / game.rb
Created October 25, 2010 10:29
Bulls and Cows - an attempt to get a sane solution for http://rosettacode.org/wiki/Bulls_and_cows/Player
class Game
attr_reader :secret
def initialize
@secret = Secret.new
end
def console_play
bulls = -1
while bulls != 4
@coffeeaddict
coffeeaddict / i18n_scoped.rb
Created October 21, 2010 10:21
Scopes for I18n
# = I18n scoped
#
# Set a temporal default scope for I18n
#
# == Synopsis
#
# Given a locale YAML that looks like this:
#
# customers:
# firstname: Customer firstname
class CreateprogramDependencies < ActiveRecord::Migration
def self.up
create_table :program_dependencies do |t|
t.references :dependency, :polymorphic => true
t.references :program
end
end
def self.down
drop_table :program_dependencies
@coffeeaddict
coffeeaddict / passphrase_generator.rb
Created August 23, 2010 19:07
Generate passwords
class PassphraseGenerator
WORDS_LIST = "/usr/share/dict/words"
@@words = nil
CONJUNCTIONS = %w(and or but you me we him + the -)
FUZZ = %w(! @ # $ % ^ & * - + ~ ` , . < > / ?);
# make a phrase of two words and a conjuction
#
# fuzz the words and return the fuzzed phrase and the original phrase
#
# make an alternative for send_later that accepts a run_at time
#
module Delayed
module MessageSending
def send_later_at(at, method, *args)
Delayed::Job.enqueue(
Delayed::PerformableMethod.new(self, method.to_sym, args),
0, #priority
at #run_at
)
class Foo
# same as attr_accessor :variant
def variant
return @variant
end
def variant=(arg)
@variant = arg
end