Skip to content

Instantly share code, notes, and snippets.

@hammerdr
Created February 26, 2012 04:58
Show Gist options
  • Save hammerdr/1913154 to your computer and use it in GitHub Desktop.
Save hammerdr/1913154 to your computer and use it in GitHub Desktop.
Rails PRO vs AR
class PRO
def self.create a, b, c
obj = build(a, b, c)
obj.save
obj
end
def self.build a, b, c
Model.new params(a, b, c)
end
def params a, b, c
{
name: name_based_on a,
description: description_based_on b,
a_type: typed_based_on c
}
end
def named_based_on a
some_logic
end
def description_based_on b
some_logic
end
def type_based_on c
some_logic
end
end
# Fast tests run against params
class PRO
def self.create a, b, c
obj = build(a, b, c)
obj.save
obj
end
def self.build a, b, c
obj = Model.new
obj.name = name_based_on a
obj.description = description_based_on b
obj.a_type = type_based_on c
obj
end
end
# Fast tests run against returned Model instance
# How pretty much everyone else does this.
class ModelBuilder
def create a, b, c
ModelRepository.save(build(a, b, c))
end
def build a, b, c
obj = Model.new
obj.name = name_based_on a
obj.description = description_based_on b
obj.a_type = type_based_on c
obj
end
end
class ModelRepository
def save obj
session.save_or_create obj
end
end
class Model #< ActiveModel::Base <- Fuck you
attr_accessor :name, :description, :a_type
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment