Skip to content

Instantly share code, notes, and snippets.

View nicholasjhenry's full-sized avatar

Nicholas Henry nicholasjhenry

View GitHub Profile
require 'test_helper'
require 'mocha'
class StatTest < ActiveSupport::TestCase
test "should belong to user" do
assert_kind_of User, stats(:one).user
end
test "should add error on user when validating with nil user" do
class WeblogController < ActionController::Base
# will render weblog/index.html.erb
# note for older versions it will be index.rhtml, but it's still erb
def index
# more code here
end
end
class AuthorsController < ActionController::Base
# will render authors.index.html.erb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang='en' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title>Firsthand Web Design</title>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
<style type="text/css">
div {
float: left
}
class CheckoutController
def new
checkout = ChecktoutContext.new
@order = checkout.order
end
end
class CheckoutContext
def initialize
@order = Order.new
@nicholasjhenry
nicholasjhenry / gist:1110289
Created July 27, 2011 20:26
Sandi Metz - Less - The Path to Better Design
# Sandi Metz - Less - The Path to Better Design
# http://vimeo.com/26330100
# http://less-goruco.heroku.com/
#
# Abstractions are more stable than concretions.
class Trip
attr_reader :bicycles, :customers, :vehicle
def prepare(preparers)
@nicholasjhenry
nicholasjhenry / gist:1226452
Created September 19, 2011 13:02
Pat Maddox's example from Streamlined Object Modeling
# http://tech.groups.yahoo.com/group/domaindrivendesign/message/6654
class Target {
public void strike(missile) {
missile.setCoordinates(self.coordinates);
missile.launch();
markDestroyed();
}
}
@nicholasjhenry
nicholasjhenry / gist:1227926
Created September 19, 2011 23:41
Streamlined Modeling Examples
class User < ActiveRecord::Base
# Collaboration Rules
def can_like?(like)
user.plugins.include?(:page_like)
end
# Business Service
def like(page)
Like.create!(:page => page, :user => self)
end
@nicholasjhenry
nicholasjhenry / bug_report.rb
Created October 18, 2011 00:49
Domain Model from SQL Antipatterns: Avoiding the Pitfalls of Database Programming
# SQL Antipatterns: http://pragprog.com/book/bksqla/sql-antipatterns
# A relationship between a model and a DAO like ActiveRecord should be HAS-A
# (aggregation) instead of (inheritance). Most frameworks that rely on
# ActiveRecord assume the IS-A solution. If you model uses DAOs instead of
# inheriting from the DAO class, then you can design the model to contain all
# data and code for the domain it's supposed to model--even if it takes
# multiple database tables to represent it.
# BugReport is a domain object encapsulating:
# * Bugs
@nicholasjhenry
nicholasjhenry / your_application.rb
Created October 22, 2011 04:56
PayRoll application, embedded in Rails, borrowing from Use Case Driven Architecture and DCI
## PayRoll gem
# lib/pay_roll.rb
module PayRoll
class << self
attr_accessor :employee_directory
def config
yield self
end
end
@nicholasjhenry
nicholasjhenry / gist:1362288
Created November 13, 2011 16:23
Recycle App with Repository #railsisnotyourapp
# RecycleApp
module RecycleApp
class << self
attr_accessor :can_repository
def config
yield self
end
end
end