Skip to content

Instantly share code, notes, and snippets.

View mattyoho's full-sized avatar

Matt Yoho mattyoho

  • Portland, OR, USA
View GitHub Profile
@ryanlecompte
ryanlecompte / gist:891728
Created March 29, 2011 02:52
BUG with Ruby 1.9.2 (MRI)
# The following fails with a failure to call "x=" private method
String.send(:attr_accessor, :x)
s = ""
s.x = 100
# The following works:
class String
self.send(:attr_accessor, :x)
end
s = ""
module Padrino
# Minimal version of `decent_exposure`[1], in keeping with Padrino's
# simplicity.
#
# [1] https://github.com/voxdolo/decent_exposure
#
# == Usage
#
# The first step is to register the plugin with your application:
# following is a little benchmark looking a real world example of inserting a
# bunch of records into pg and mongo using a few methods available in ruby.
#
# the code inserst a value with a simple uniq contraint and then looks it up.
#
# this is not meant to be an apples to apples benchmark, rather it's trying to
# see how the various dbs might perform in a real world situation. i know
# mongo does not fsync to disk. i know it's not fair using a transaction with
# the rdbms, but these conditions will exist in real world usage and it's that
# which i'm interested in.
Fabricate(:person) do
ssn { sequence(:ssn, 111111111) }
email { sequence(:email) { |i| "user#{i}@example.com" } }
end

Validations

Data integrity is an underrated part of proper application architecture. Many of the bugs in production systems are triggered by missing or malformed user data. If a user can possibly screw it up or screw with it, they will. Validations in the model can help!

On Syntax

Before we begin, let's talk about syntax. There are two primary syntaxes for writing validations in Rails 3:

validates_presence_of :price
dc.ui.Document = Backbone.View.extend({
initialize: function() {
this.model.bind('change', this._onDocumentChange);
this.model.bind('change:selected', this._setSelected);
this.model.bind('view:pages', this.viewPages);
this.model.notes.bind('add', this._addNote);
this.model.notes.bind('reset', this._renderNotes);
this.model.pageEntities.bind('reset', this._renderPages);
}
@catharinejm
catharinejm / gist:1196418
Created September 6, 2011 02:26
inner function closure in C
#include <stdlib.h>
#include <stdio.h>
int(*outer(int val))() {
int inner() {
return val;
}
return inner;
}
@zspencer
zspencer / your_script.sh
Created September 6, 2011 22:58
Pair with Zee
#If you're on a mac/linux, you can just copy and paste this. If you're on Windows, you need to figure out how to do this using Putty.
ssh pair@pairwith.zeespencer.com
@jcasimir
jcasimir / performance.markdown
Created September 21, 2011 13:27
Measuring Performance

Measuring Performance

Performance is often ignored in Rails development until it becomes a problem. If ignored too long, though, it can get very tricky to improve. It's valuable to regularly audit performance and look for hotspots or design choices that are slowing things down.

Inspecting the Logs

Inspecting the log will help identify the source of several performance issues the application may have.

The Rails application log outputs the time spent processing each request. It breakdowns the time spent at the database level as well processing the view code. In development mode, the logs are displayed on STDOUT where the server is being run. In a production setting the logs will be in log/production.log within the application's root directory.

@alloy
alloy / gist:1281203
Created October 12, 2011 13:17
ActiveRecord::Relation#merge example
class Edition < ActiveRecord::Base
scope :published, where(arel_table[:published_at].not_eq(nil))
scope :generated, where(arel_table[:generated_at].not_eq(nil))
scope :available, published.generated
end
class Magazine < ActiveRecord::Base
# This is where #merge works its magic, by allowing you to merge scopes from other models.
scope :with_available_editions, includes(:editions).merge(Edition.available)
end