-
-
Save dbrady/1070178 to your computer and use it in GitHub Desktop.
class StoreProductCollaborator | |
def initialize(product_or_id, store_or_id) | |
# I use this pattern everywhere. I would like to abstract it, but | |
# can't think of a good name for the abstraction. | |
@product = Product.find(product_or_id) unless product_or_id.is_a? Product | |
@store = Store.find(store_or_id) unless store_or_id.is_a? Store | |
end | |
# Intended usage examples: | |
# sp1 = StoreProductCollaborator.new(42, 13) | |
# sp2 = StoreProductCollaborator.new(@product, @store) | |
# sp3 = StoreProductCollaborator.new(42, @store) | |
# sp4 = StoreProductCollaborator.new(@product, 13) | |
# ...I can even see the shape in my mind of this code, and its | |
# value as an abstraction: if we called it foo_snark(klass) and put | |
# it on Integer AND on ActiveRecord::Base, then Integer could find | |
# the object, and ActiveRecord::Base could check to make sure that | |
# the AR object given matches the expected class. | |
# | |
# But seriously, what to call it? instantiate? to_model? | |
# to_model_instance? | |
end |
I'm sure I'm missing something. Can you elaborate on this foo_snark'ness? By the way, this is similar to how I usually do this:
class StoreProductCollaborator
def initialize(product_or_id, store_or_id)
@product = Product.for(product_or_id)
@store = Store.for(store_or_id)
end
end
class Store < ActiveRecord::Base
def self.for(store_or_id)
return store_or_id if store_or_id.is_a? Store
find(store_or_id)
end
end
class Product < ActiveRecord::Base
#...
end
dbrady: my first guess at a name would be "modelizing," but as I look at it, it's basically a fancy typecast:
"Take something that's an object, or an identifier for an object, and give me back an object." I'd say "to_model" is a pretty good name for that.
@geeksam, that's a great idea.
jtanner: Ah, yes, I remember that. That's essentially shorthand for .find(), though, isn't it? Or would Product[Product.new] still return the new product object?
geeksam: Excellent! I like Store.for but in our fixation on (integer or store).for(Store), we hit on "to_model" as well. Great minds think alike (or perhaps fools seldom differ?)
Thanks, both!
@dbrady: Surely the former. I am constantly astonished at the variety of foolishness available. ;>
jtanner: nevermind, you said the thing I asked apparently I can't read.
In a project at LMP we have a helper method on ActiveRecord::Base that does something similar. We use [] for the method, for example:
Product[@Product] # => just returns @Product
Product[1] # => calls Product.find(1)