Skip to content

Commit

Permalink
Alias conditions as where
Browse files Browse the repository at this point in the history
  • Loading branch information
iain committed Jan 10, 2010
1 parent 780c5b6 commit ec1162b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.rdoc
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Rakefile
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion lib/basic_named_scopes.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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]

Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions spec/basic_named_scopes_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit ec1162b

Please sign in to comment.