Repository is archived
No commit activity in last 3 years
No release in over 3 years
modify behavior of ActiveRecord::Base::default_scope to merge rather than overwrite
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 2.0.0.beta.8
 Project Readme

merged_default_scopes

require 'merged_default_scopes' 

module SomePlugin
  def self.included(mod)
    mod.default_scope :conditions => {:deleted_at => nil}
  end
end

class SomeModel < ActiveRecord::Base
  include SomePlugin

  default_scope :order => :name
end

SomeModel.__send__(:scope, :find)
# => {:order => :name, :conditions => {:deleted_at => nil}}
#
# ... and all is right with the world

clear_default_scope

require 'merged_default_scopes' 

class SomeModel < ActiveRecord::Base
  include SomePluginThatMightSetDefaultScope
  include AnotherPluginThatMightSetDefaultScope

  clear_default_scope
  default_scope :order => :name
end

SomeModel.__send__(:scope, :find)
# => {:order => :name}
#
# ... and all is right with the world, assuming you want the old
# behaviour of default_scope, which is to overwrite previously set
# default scopes

Motivation

Sometimes plugins use default_scope to do what they do. We need a means of adding to the default scope without blowing it away.

This is going to be the behaviour of default_scope and clear_default_scope in rails-3 (as of http://github.com/rails/rails/commit/35a114a8941cb22d29a536f1215a23a8cf7c4756), so this gem serves as a back-port to rails-2.

Install

$ gem install merged_default_scopes

Contribute

  • Fork the project
  • Make the code better with a new feature, bug fix, or refactoring.
    • New features and bug fixes should have specs.
  • Send us a pull request.

Learn more

Copyright

Copyright (c) 2010 David Chelimsky and Brian Tatnall. See LICENSE for details.