apotonick / disposable

Decorators on top of your ORM layer.
MIT License
171 stars 39 forks source link

Collection property that doesn't write itself, but still recursively calls save #24

Open krainboltgreene opened 9 years ago

krainboltgreene commented 9 years ago

I'm using a collection property to handle scopes in ActiveRecord. Example:

class Cart
   has_many :items
end

class Item
  belongs_to :product, polymorphic: true
  belongs_to :cart

  scope :shoes, -> { where(product_type: "Shoe") }
end

class Shoe
  has_many :items
end

And my twins:

class CartTwin
  property :items, twin: ItemCollectionTwin
end

class ItemCollectionTwin
  collection :shoes, twin: ShoeItemTwin
end

class ShoeItemTwin
  property :product, twin: ShoeTwin
  property :cart, twin: CartTwin
end

class ShoeTwin
  property :items, twin: ItemCollectionTwin
end

So this all works from a read standpoint. It fails as soon as I sync from either end node. The problem is that *CollectionTwin want to do shoes=, but it's a scope and not a column. If i do collection :shoes, twin: ShoeItemTwin, writeable: false it "syncs" but stops saving recursively at this point.

I need to be able to say "This property/collection has no write, but it still contains something that needs to be synced".

krainboltgreene commented 9 years ago

I've managed to monkey patch Sync and Save enough that it works correctly. Essentially I make Sync only try to setter= if it exists, and Save only model.save if save exists.

Realistically this would be built into the top level concept of a subset.