Skip to content

Instantly share code, notes, and snippets.

@latortuga
latortuga / registration.rb
Created October 11, 2012 14:43
ActiveModel validations without persistence
# Adapted from http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/
#
# app/models/registration.rb
class Registration
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
# Other ActiveModel modules:
# AttributeMethods: Makes it easy to add attributes that are set like table_name :foo
@latortuga
latortuga / crossdomain.xml
Created December 8, 2012 20:07
Amazon S3 crossdomain.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" secure="false" />
</cross-domain-policy>
@latortuga
latortuga / gist:4241707
Created December 8, 2012 20:09
Request Signing for Direct Amazon S3 File Upload
# app/controllers/application_controller.rb
class ApplicationController
private
def generate_s3_upload_data
bucket = BUCKET # S3 bucket
access_key = S3_KEY # S3 Access Key
secret = S3_SECRET # S3 Secret Key
key = "uploadify/" # The folder in your bucket that you'd like uploaded files to land in. See note below.
expiration = 10.hours.from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')
@latortuga
latortuga / gist:4241729
Created December 8, 2012 20:13
Setup Uploadify to upload directly to S3
var uploadify_data = {
'AWSAccessKeyId': '<%= @uploadify_data[:access_key] %>',
'bucket': '<%= @uploadify_data[:bucket] %>',
'acl': 'private',
'key': '<%= @uploadify_data[:key] %>${filename}',
'signature': '<%= @uploadify_data[:signature] %>',
'policy': '<%= @uploadify_data[:policy] %>',
'success_action_status': '<%= @uploadify_data[:sas] %>',
'folder': '',
'Filename': ''
@latortuga
latortuga / 1.md
Last active October 29, 2016 14:32
rails-4-2-overview

What's new in Rails 4.2?

ActiveJob

Adapter layer on top of queueing systems like Resque and Delayed Job. Full adapter list: :backburner, :delayed_job, :qu, :que, :queue_classic, :resque, :sidekiq, :sneakers, :sucker_punch

ActiveJob::Base.queue_adapter = :inline # default queue adapter

# Declare a job:

Notes

This is the latest version of an email which I send periodically, offering customers the opportunity to pre-pay for SaaS in return for a discount. The benefits to the SaaS company are better cash flow and reduced churn rate. The benefits to the customer are, well, in the email. This genre of email has produced hundreds of thousands of dollars in pre-pays for some companies I work with, and it rarely requires any more work than this example.

I've put $79 is as a placeholder for the cost of the user's plan. We calculate that for each account, naturally, along with the billing contact's name.

Subject: Save $79 on Appointment Reminder (and get a tax write-off) Formatting: 100% plain text. Gmail automatically links up the central link. From: Patrick McKenzie (Appointment Reminder) [email protected]

@latortuga
latortuga / poodr.rb
Created February 17, 2015 21:10
ecoding5
$ ruby poodr.rb
poodr.rb:20:in `prepare_trip': undefined method `each' for nil:NilClass (NoMethodError)
from poodr.rb:6:in `block in prepare'
from poodr.rb:5:in `each'
from poodr.rb:5:in `prepare'
from poodr.rb:54:in `<main>'
@latortuga
latortuga / gist:ca7ed1d87593640f0d8f
Created April 20, 2015 18:48
Rails #18952 reproduction
require 'active_record'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.define do
create_table :somethings, force: true do |t|
t.integer :age
t.integer :count
end
end
class Something < ActiveRecord::Base
# Imagine we have a form that only sets user name for verified users
class Form
include ActiveModel::Model
attr_accessor :name
def save(user)
user.name = name if user.verified?
end
end
@latortuga
latortuga / 1minitest.md
Last active August 29, 2015 14:27
ARGH-V

Run tests via Minitest how it's supposed to work

# manual way
Minitest.run(args)

# magic way
require 'minitest/autorun'
# or
Minitest.autorun