Permalink
Browse files

Alias conditions as where

  • Loading branch information...
1 parent 780c5b6 commit ec1162b370261f662ac17b73c9e9908e308989a9 @iain committed Jan 10, 2010
Showing with 18 additions and 2 deletions.
  1. +2 −0 README.rdoc
  2. +2 −1 Rakefile
  3. +8 −1 lib/basic_named_scopes.rb
  4. +6 −0 spec/basic_named_scopes_spec.rb
View
@@ -14,6 +14,8 @@ You can now write:
All named scopes are called the same, except for +include+, which is now
called +with+, because +include+ is a reserved method.
+Also, the scope +conditions+ is aliased as +where+, just as in ActiveRecord 3.
+
Reuse them by making class methods:
class Post < ActiveRecord::Base
View
@@ -11,7 +11,8 @@ begin
gem.homepage = "http://github.com/iain/basic_named_scopes"
gem.authors = ["Iain Hecker"]
gem.add_development_dependency "rspec", ">= 1.2.9"
- gem.add_development_dependency "temping", ">= 1.1.0"
+ gem.add_development_dependency "temping", ">= 1.3.0"
+ gem.add_development_dependency "activerecord", "< 3.0.pre"
end
Jeweler::GemcutterTasks.new
rescue LoadError
@@ -12,6 +12,8 @@
# All named scopes are called the same, except for +include+, which is now
# called +with+, because +include+ is a reserved method.
#
+# Also, the scope +conditions+ is aliased as +where+, just as in ActiveRecord 3.
+#
# Reuse them by making class methods:
#
# class Post < ActiveRecord::Base
@@ -49,6 +51,9 @@ module BasicNamedScopes
# These are the normal parameters that will be turned into named scopes.
FIND_PARAMETERS = [:conditions, :order, :group, :having, :limit, :offset, :joins, :select, :from]
+ # These are aliased parameters. The keys are scope names, the values are the option they represent.
+ FIND_ALIASES = { :where => :conditions, :with => :include }
+
# These are the parameters that want a boolean.
FIND_BOOLEAN_SWITCHES = [:readonly, :lock]
@@ -62,10 +67,12 @@ def extended(model)
end
def apply_basic_named_scopes(model)
- model.named_scope(:with, expand_into_array(:include))
FIND_PARAMETERS.each do |parameter|
model.named_scope(parameter, expand_into_array(parameter))
end
+ FIND_ALIASES.each do |name, parameter|
+ model.named_scope(name, expand_into_array(parameter))
+ end
FIND_BOOLEAN_SWITCHES.each do |parameter|
model.named_scope(parameter, default_to_true(parameter))
end
@@ -50,6 +50,12 @@
Post.with(:author).to_a
end
+ it "should use a scope named 'where' but internally use 'conditions' as parameter" do
+ subject = Post.where("published = ?", true)
+ subject.class.should be_scope
+ subject.should == [ @published ]
+ end
+
[:conditions, :order, :group, :having, :limit, :offset, :joins, :with, :select, :from].each do |option|
it "should known #{option} ActiveRecord::Base.find" do
Post.send(option).class.should be_scope

0 comments on commit ec1162b

Please sign in to comment.