Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveSupport OptionMerger to merge lambdas

#726

Right now it is imposible to use with_options when we have named_scope with lambdas example:

with_options(:include => [:foo]) do |options|

options.named_scope :order_by, lamdba { |order| { :order => order }

end

This patch will allow ActiveSupport.OptionMerger to deep_merge lambda expressions.

Reported by Paweł Kondzior · July 30th, 2008 @ 12:59 PM

State: committed
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Paweł Kondzior
  2. Piotr Usewicz
  3. hosiawak
  4. Paweł Kondzior
    Paweł Kondzior
    • Assigned user set to josh

    August 1st, 2008 @ 11:17 AM

  5. Paweł Kondzior
  6. Paweł Kondzior
    Paweł Kondzior

    maybe first example doesn't show what benefits we have from this patch.

    class Article
      belongs_to :author
      has_many :comments
    
      with_options(:include => [:author, :comments], :order => 'created_at DESC') do |base|
        base.named_scope :order_by, lambda { |order| { :order => order } }
        base.nameD_scope :created_by, lambda { |author| { :conditions => ['created_by = ?', author.id } }
      end
    end
    

    It is much Dry to use with_otpions and just type Article.order_by("authors.name DESC") than doin Article.base.order_by("authors.name DESC")

    August 4th, 2008 @ 09:48 AM

  7. Jacek Becela
    Jacek Becela

    After I started using named_scopes, all my models contain a few or often more than a dozen named_scopes with lambdas and I could really use such a feature.

    This is better than chaining some common named_scope with every other scope, like say: Article.with_author_and_comments.order_by(something).

    On the other hand, there is another problem that this patch would not solve, and I'd love it had:

    Say you have a deleted_at column which is a common pattern for having "undo" in your app, etc.

    I'd like to have a DEFAULT named_scope so Article.all and Article.order_by(something) would always add SQL fragment "deleted_at is null". A default named_scope.

    Implementation would of course go into named_scope and not into OptionMerger.

    November 15th, 2008 @ 12:52 PM

  8. Paweł Kondzior
    Paweł Kondzior

    Jacek: look at this ticket: http://dev.rubyonrails.org/ticke... and http://github.com/rubaidh/defaul...

    I think too that it would be very cool to have default_scope inside AR.

    November 15th, 2008 @ 03:41 PM

  9. DHH
    DHH
    • State changed from new to committed

    November 15th, 2008 @ 03:49 PM