Say a user can add a product to an order.
Now I have no idea if in the future they're gonna want to add things other than "products" into a order. I can image something like this could happen:
So to make this work, I'd make LineItem polymorphic:
So is making LineItem polymorphic from the start YAGNI or is not doing so violating Open/Closed (since I'll need to alter LineItem as soon as they want to add something to an order that's not a Product)?
Thx!
Code:
class Order < ActiveRecord::Base
has_many :line_items
end
class LineItem < ActiveRecord::Base
attr_accessible :price, :quantity
belongs_to :order
belongs_to :product
end
class Product < ActiveRecord::Base
attr_accessible :price
has_many :line_items
end
Now I have no idea if in the future they're gonna want to add things other than "products" into a order. I can image something like this could happen:
Code:
class PowerFeature < ActiveRecord::Base
attr_accessible :price
has_many :line_items
end
So to make this work, I'd make LineItem polymorphic:
Code:
class LineItem < ActiveRecord::Base
attr_accessible :price, :quantity
belongs_to :purchasable, polymophic: true
end
class Product < ActiveRecord::Base
attr_accessible :price
has_many :line_items, as: :purchasable
end
class PowerFeature < ActiveRecord::Base
attr_accessible :price
has_many :line_items, as: :purchasable
end
So is making LineItem polymorphic from the start YAGNI or is not doing so violating Open/Closed (since I'll need to alter LineItem as soon as they want to add something to an order that's not a Product)?
Thx!