apotonick / disposable

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

Support for scopes #22

Closed krainboltgreene closed 8 years ago

krainboltgreene commented 9 years ago

I realize #scopes is a feature specific to ActiveRecord and this library is pretty agnostic. Here's an example:

class Cart
  has_many :items
end

class Item
  belongs_to :cart

  scope :nikes, -> { where(brand: "nike") }
end

cart.items.nikes

With this library it raises an error:

NoMethodError: undefined method `nikes' for #<Disposable::Twin::Collection:...>

I'm not sure how it would work, but I would love to see this:

class ItemTwin < Disposable::Twin
  property :brand
  property :cart

  subset :nikes, twin: ItemTwin
end
apotonick commented 9 years ago

This works, you just have to do as follows.

class Cart::Twin < Disposable::Twin
  collection :nikes, twin: ItemTwin

  def nikes
   super model.items.nikes
  end
end
krainboltgreene commented 9 years ago

Oooh, that's a great example, should be in the readme.

krainboltgreene commented 9 years ago

Actually, that doesn't work :(

Are you sure about the syntax?

apotonick commented 9 years ago

Never ever again say "this doesn't work" anymore in a programming context. Please.

What happens?

apotonick commented 9 years ago

BTW, I love your blog, you should blog more. :rocket:

krainboltgreene commented 9 years ago

Well first I noticed you used Cart::Twin, so I guessed you either made a typo or you're namespacing to cart. Second, you're defining the collection on either Cart or this new Cart::Twin object, the former results in ActiveRecord saying "Hey, nikes isn't a method I know about for Cart" and the second way results in the same error as the original ticket.

apotonick commented 9 years ago

@krainboltgreene Jump on https://gitter.im/trailblazer/chat we can figure it out there ;)

krainboltgreene commented 9 years ago

So in-between writing a monkey patch to add subsets for my application I realized another approach that is less than easy to understand but works:

class CartTwin
  property :items, twin: ItemCollectionTwin
end

class ItemCollectionTwin
  collection :nikes, twin: ItemTwin
end
apotonick commented 8 years ago

That's ingenious!

So, you basically could macro-ize that in a Scope feature model. What do you think? And that could be marked as writeable: false, as discussed in the other issue that I can't find.

apotonick commented 8 years ago

The Scope feature module being part of Disposable, of course! :beer:

lib/disposable/presentation/scope.rb or something...

krainboltgreene commented 8 years ago

See #26 #23 latest and once we finish those discussions I'll start a PR that adds this special functionality.

Which would you prefer: extension gem (disposable-activerecord or something) or directly in disposable?

apotonick commented 8 years ago

Sorryyyy for the delay! Let's do a disposable-orm gem, since scopes are pretty generic. We can discuss everything based on your code, then.

Does that sound good?

krainboltgreene commented 8 years ago

Sooooo as someone who does a lot of foss projects I hate to say this, but I think we're going to reimplement disposable's use case (soft application) in a new library internally and possibly release.

krainboltgreene commented 8 years ago

Meaning it might be useful to just close these issues.

apotonick commented 8 years ago

Oh I've heard that many times and then never seen any free OSS code based on the original stuff. Good luck!

Eager to hear your ideas, what you will change.