Skip to content

Instantly share code, notes, and snippets.

@adzap
adzap / keybase.md
Created July 3, 2018 03:04
keybase.md

Keybase proof

I hereby claim:

  • I am adzap on github.
  • I am adammeehan (https://keybase.io/adammeehan) on keybase.
  • I have a public key ASDO7r7yT7yV3BxMp7CKd-yyR5xH4MPNweYLlUyETfC4hAo

To claim this, I am signing this object:

@adzap
adzap / audited-transaction_id.rb
Created October 30, 2012 22:12
Add transaction ID to audit records
# Using XID gem https://github.com/adzap/xid
# with Audited gem https://github.com/collectiveidea/audited/commits/master
# This adds the transaction ID to the audit record so you can group all changes together which happened in the same transaction, # which is hopefully the same use action. This obviously needs you add a transaction_id column to the audits table.
Audited::Adapters::ActiveRecord::Audit.class_eval do
before_create :set_transaction_id
def set_transaction_id
self.transaction_id = ActiveRecord::Base.connection.transaction_id
@adzap
adzap / valid.rb
Created October 12, 2012 00:39 — forked from cmaitchison/valid.rb
Testing fail?
# By leaning hard on the declarative nature of let(), you get to use
# a pattern where there is a single shared example whose
# expected result is defined at a lower level in the code.
# It is not really a pattern that is intuitive to everyone when
# they read this sort of code for the first time, so I am not about
# to claim it is better than other approaches, only different.
# There is no need for explicit tests to assert that the correct
# arguments are passed to the validators in this example, because
# if the arguments are not correct the doubles will not return the
@adzap
adzap / valid.rb
Created October 11, 2012 05:25 — forked from notahat/valid.rb
Testing fail?
# I have a class that delegates functionality to a couple of objects that it
# constructs. I want to test this in isolation. I want to make sure that the
# objects are constructed with the right arguments. I also want to make sure
# that the results from the objects are handled correctly.
#
# I'm finding it hard to structure the code and test in a way that isn't
# cumbersome. What's below works, but it feels like a lot of stubbing and setup
# for something the should be simpler.
#
# Anyone got a better approach for this?
@adzap
adzap / valid.rb
Created October 11, 2012 05:12 — forked from notahat/valid.rb
Testing fail?
# I have a class that delegates functionality to a couple of objects that it
# constructs. I want to test this in isolation. I want to make sure that the
# objects are constructed with the right arguments. I also want to make sure
# that the results from the objects are handled correctly.
#
# I'm finding it hard to structure the code and test in a way that isn't
# cumbersome. What's below works, but it feels like a lot of stubbing and setup
# for something the should be simpler.
#
# Anyone got a better approach for this?
@adzap
adzap / freemium_upgrade_models.txt
Created April 11, 2012 01:58
Freemium Upgrade Models
Freemium Upgrade Models
more of the same (larger volume of "things", storage space, users, projects, files etc.)
remove advertising
remove/reduce rate limit (API, reqs/sec)
support
trial period
unlock features (internal modules, features exposed)
higher quality (videos, music streaming)
physical copy (print version of ebook)
@adzap
adzap / test_class_attribute.rb
Created December 13, 2010 01:44
Demonstrate issue with class_attribute not cloning hash value in subclasses
require 'rubygems'
require 'active_support/all'
class Base
class_attribute :settings
class_inheritable_accessor :old_settings
self.settings = { :foo => 'bar' }
self.old_settings = { :foo => 'bar' }
end
@adzap
adzap / html_escaping.rb
Created July 23, 2010 21:25
Rails 3 HTML escaping when using content_tag
# Rails 3 beta 4
# Line 74 in actionpack/lib/action_view/helpers/tag_helper.rb
def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
if block_given?
options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
content_tag_string(name, capture(&block), options, escape)
else
content_tag_string(name, content_or_options_with_block, options, escape)
end
# Get your spec rake tasks working in RSpec 2.0
require 'rspec/core/rake_task'
desc 'Default: run specs.'
task :default => :spec
desc "Run specs"
RSpec::Core::RakeTask.new do |t|
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
class Signup < ActiveRecord::Base
validates_presence_of :name, :email_address, :mobile_phone, :username, :password
# knocked off variation of something toolmantim posted years ago
def valid_for_attributes?(*attrs)
return true if valid?
attrs.collect! {|attr| attr.to_s }
!errors.any? {|attr, error| attrs.include?(attr) }
end