Created
February 26, 2012 04:58
-
-
Save hammerdr/1913154 to your computer and use it in GitHub Desktop.
Rails PRO vs AR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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