Skip to content

Instantly share code, notes, and snippets.

View nicholasjhenry's full-sized avatar

Nicholas Henry nicholasjhenry

View GitHub Profile
# this would be in a helper somewhere
def stub_find(entity)
result = mock_model(entity)
entity.stub(:find).with(result.id).and_return(result)
result
end
let(:params) do
{:these => 'params'}
end
@adomokos
adomokos / visitor_pattern_example.rb
Created May 24, 2011 17:28
The Visitor Pattern implementation in Ruby from the Wikipedia example
class CarElement
def accept(visitor)
raise NotImpelementedError.new
end
end
module Visitable
def accept(visitor)
visitor.visit(self)
end
@dhh
dhh / gist:1014971
Created June 8, 2011 18:09
Use concerns to keep your models manageable
# autoload concerns
module YourApp
class Application < Rails::Application
config.autoload_paths += %W(
#{config.root}/app/controllers/concerns
#{config.root}/app/models/concerns
)
end
end
@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)
@bencrouse
bencrouse / gist:1126643
Created August 5, 2011 00:16
DDDish Rails
#
# Controller - Application service layer until a separation has clear value
#
#
class CartController < ApplicationController
before_filter :find_variant
rescue_from Model::InvalidOperation, with: :domain_error
def add_item
@mattbrictson
mattbrictson / Gemfile
Created August 21, 2011 17:53
Configuring rails 3.1 and dragonfly to use Herkou and Amazon CloudFront
gem 'dragonfly', '~>0.9.4'
group :production do
gem 'fog' # for Amazon S3
end
@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
def revoke(user)
proxy_association.owner.tap do |project|
# You can't remove the last user with access (someone has to have access to the project!)
if project.users.many?
if user.pending? && user.projects.one?
user.destroy
else
project.users.delete(user)
user.touch
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