Skip to content

Instantly share code, notes, and snippets.

View knzconnor's full-sized avatar

Kenzi Connor knzconnor

View GitHub Profile
@knzconnor
knzconnor / uri_validator.rb
Last active December 14, 2015 16:09 — forked from bluemont/url_validator.rb
Totally untested, just forked and tweaked for discussion
require 'addressable/uri'
#Accepts options[:message] and options[:allowed_protocols]
class UriValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
uri = parse_uri(value)
if !uri
record.errors[attribute] << generic_failure_message
elsif !allowed_protocols.include?(uri.scheme)
#In case it wasn't obvious, this was a joke about Rails anti-patterns
def address(separator, seperator2=' ')
fields_array = %w(address1 address2 city state zip phone).map {|t| [:billing,t].join('_').to_sym}
(0..10).inject("") { |string, i|
if i.odd?
field = :separator
field = (field.to_s + '2').intern if i == 7
else
field = fields_array[i/2]
end
def address(separator, seperator2=' ')
fields_array = %w(address1 address2 city state zip phone).map {|t| [:billing,t].join('_').to_sym}
(0..10).inject("") { |string, i|
if i.odd?
field = :seperator
field = (field.to_s + '2').intern if i == 7
else
field = fields_array[i/2]
end
temp = send(field) rescue nil
def address(separator, seperator2=' ')
fields_array = [:billing_address1, :billing_address2, :billing_city, :billing_state, :billing_zip, :billing_phone]
(0..10).inject("") { |string, i|
if i.odd?
field = :seperator
field = (field.to_s + '2').intern if i == 7
else
field = fields_array[i/2]
end
temp = send(field) rescue nil
def address(separator)
"#{billing_address1}#{separator}#{billing_address2}#{separator}#{billing_city}#{separator}#{billing_state} #{billing_zip}#{separator}#{billing_phone}"
end
require "test_helper"
require "yaml"
class FixtureTest < ActiveSupport::TestCase
fixtures :all
Dir[File.join Rails.root, "test", "fixtures", "**", "*.yml"].each do |file|
if fixtures = YAML.load(IO.read(file))
kind = File.basename file, ".yml"
if a3
name, params, ret = a1, a1, a2
else
name, params, ret = nil, a2, a3
end
def funky_foo(shakalaka, &block)
unless some_condition
puts "chicken dinner"
return
end
# lines of code
# and more lines of code
# ...
# 50 lines of code
#Or just check-out: http://github.com/timocratic/test_benchmark/tree/master
#It's only one file too: http://github.com/timocratic/test_benchmark/blob/832b2217451b49ed218d2db31dfad2b81a2399eb/lib/test_benchmark.rb
#Go ahead and steal anything you want from it - or even better fork it and add what features you like

Note to Self

Test

Tests build confidence. Write 'em. They'll save your ass, and they'll let you take a chainsaw to your code without being afraid of unintended consequences.

Automate