apotonick / disposable

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

Order collection #25

Closed TheSmartnik closed 9 years ago

TheSmartnik commented 9 years ago

Hi Nick, I wonder whether it is possible or not to somehow order collection. Something like:

class AlbumTwin < Disposable::Twin
  collection :songs, order: [:by_duration, :by_created] do
    property :name
    property :duration
  end
end

class Album < ActiveRecord::Base
  scope :by_duration, -> { order(by_duration: :asc) }
  scope :by_created, -> { order(by_created: :desc) }
end

Well, I mean, I know that it is not in current API right now, but maybe I could somehow implement desired behavior?

Tab10id commented 9 years ago

Haha! I think about it today too=) But it's not about only order. In scope we can do any strange things

scope :active, -> { where(active: true) }

or

scope :strange_scope, { Thing.where(id: 42) }

It's all about mapping

TheSmartnik commented 9 years ago

Oh, right. To support any scopes would be even better. Although, I'm afraid it may lead to some unexpected behavior in case of strange_scope

apotonick commented 9 years ago

This already works!!! :smile: You simply have to name your collection after the scope.

class AlbumTwin < Disposable::Twin
  collection :by_duration { .. }

Twins are not ORM mappers, they are data mappers that simply wrap arbitrary objects and collections into predictable in-memory objects.

Also, this is a duplicate with https://github.com/apotonick/disposable/issues/22.