Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

:scopes => :shallow

  • Loading branch information...
commit 5eb2d802cb0cd2bf7c2082444814d7cc56c11395 1 parent 09718d4
Marcos Piccinini authored
14 README.rdoc
View
@@ -135,14 +135,16 @@ You can skip i18n lookup with :i18n => false
=== Scopes
-If you provide the scopes option, some fancy named scopes will be added:
-In our User example, gender has male/female options, so you can do:
+With the ':scopes => true' option, you may filter/read/write easily:
- # AR
- User.male # => User.all(:conditions => { :gender => :male })
+ User.sex(:female).each ... # => User.where({ :gender => :female })
- # Mongoid
- User.female # => User.where({ :gender => :female })
+
+Now, if you provide the ':scopes => :shallow' option, fancy named scopes
+will be added to the class directly. In our User example, gender has
+male/female options, so you can do:
+
+ User.female.each ... # => User.where({ :gender => :female })
You can chain named scopes as well:
16 lib/symbolize/active_record.rb
View
@@ -106,13 +106,17 @@ def symbolize *attr_names
end
if scopes
- values.each do |value|
- name = value[0]
- if name.respond_to?(:to_sym)
- scope name.to_sym, :conditions => { attr_name => name.to_s }
- # Figure out if this as another option, or default...
- # scope_comm.call "not_#{attr_name}".to_sym, :conditions => { attr_name != name }
+ if scopes == :shallow
+ values.each do |value|
+ name = value[0]
+ if name.respond_to?(:to_sym)
+ scope name.to_sym, where(attr_name => name.to_s)
+ # Figure out if this as another option, or default...
+ # scope_comm.call "not_#{attr_name}".to_sym, :conditions => { attr_name != name }
+ end
end
+ else
+ scope attr_name, ->(enum) { where(attr_name => enum) }
end
end
11 lib/symbolize/mongoid.rb
View
@@ -108,8 +108,15 @@ def symbolize *attr_names
end
if scopes
- # scoped scopes
- scope attr_name, ->(enum) { where(attr_name => enum) }
+ if scopes == :shallow
+ values.each do |k, v|
+ if k.respond_to?(:to_sym)
+ scope k.to_sym, where({ attr_name => k })
+ end
+ end
+ else # scoped scopes
+ scope attr_name, ->(enum) { where(attr_name => enum) }
+ end
end
if validation
4 spec/symbolize/active_record_spec.rb
View
@@ -7,7 +7,7 @@ class User < ActiveRecord::Base
symbolize :other
symbolize :language, :in => [:pt, :en]
symbolize :sex, :in => [true, false], :scopes => true
- symbolize :status , :in => [:active, :inactive], :i18n => false, :capitalize => true, :scopes => true, :methods => true
+ symbolize :status , :in => [:active, :inactive], :i18n => false, :capitalize => true, :scopes => :shallow, :methods => true
symbolize :so, :allow_blank => true, :in => {
:linux => 'Linux',
:mac => 'Mac OS X',
@@ -347,7 +347,7 @@ class << ActiveRecord::Base
end
it "should have other to test better" do
- User.linux.should == [@anna]
+ User.so(:linux).should == [@anna]
end
# it "should have 'with' helper" do
19 spec/symbolize/mongoid_spec.rb
View
@@ -12,7 +12,7 @@ class Person
symbolize :language, :in => [:pt, :en]
symbolize :sex, :type => Boolean, :scopes => true, :i18n => true
- symbolize :status , :in => [:active, :inactive], :i18n => false, :capitalize => true, :scopes => true
+ symbolize :status , :in => [:active, :inactive], :i18n => false, :capitalize => true, :scopes => :shallow
symbolize :so, :allow_blank => true, :in => {
:linux => 'Linux',
:mac => 'Mac OS X',
@@ -359,10 +359,21 @@ class Project
Project.count.should eql(1)
end
+ it "should now shallow scoped scopes" do
+ Person.create(:name => 'Bob' , :other => :bar, :status => :active, :so => :linux, :gui => :gtk, :language => :en, :sex => false, :cool => false)
+ Person.should_not respond_to(:status)
+ end
+
it "should set some scopes" do
- Person.create(:name => 'Bob' , :other => :bar, :status => :active, :so => :mac, :gui => :gtk, :language => :en, :sex => false, :cool => false)
- Person.status(:active).should be_a(Mongoid::Criteria)
- Person.status(:active).count.should eq(1)
+ Person.create(:name => 'Bob' , :other => :bar, :status => :active, :so => :linux, :gui => :gtk, :language => :en, :sex => false, :cool => false)
+ Person.so(:linux).should be_a(Mongoid::Criteria)
+ Person.so(:linux).count.should eq(1)
+ end
+
+ it "should work with a shallow scope too" do
+ Person.create!(:name => 'Bob' , :other => :bar, :status => :active, :so => :linux, :gui => :gtk, :language => :en, :sex => false, :cool => false)
+ Person.active.should be_a(Mongoid::Criteria)
+ Person.active.count.should eq(1)
end
end

2 comments on commit 5eb2d80

Kornelius Kalnbach

This change breaks Ruby 1.8.7 compatibility (beacuse of the ->(enum) { … } syntax). Not sure whether this was indended. Would be great to mention this in the README or fix it :)

Marcos Piccinini
Owner

Hello korny, thanks for the heads up. Guess no need to use the new syntax, just one spot, will use old Proc.new.

Please sign in to comment.
Something went wrong with that request. Please try again.