symbolize 4.3.2 → 4.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +8 -6
- data/lib/symbolize/active_record.rb +10 -6
- data/lib/symbolize/mongoid.rb +9 -2
- data/lib/symbolize/version.rb +1 -1
- data/spec/symbolize/active_record_spec.rb +2 -2
- data/spec/symbolize/mongoid_spec.rb +15 -4
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -135,14 +135,16 @@ You can skip i18n lookup with :i18n => false
|
|
135
135
|
|
136
136
|
=== Scopes
|
137
137
|
|
138
|
-
|
139
|
-
In our User example, gender has male/female options, so you can do:
|
138
|
+
With the ':scopes => true' option, you may filter/read/write easily:
|
140
139
|
|
141
|
-
#
|
142
|
-
User.male # => User.all(:conditions => { :gender => :male })
|
140
|
+
User.sex(:female).each ... # => User.where({ :gender => :female })
|
143
141
|
|
144
|
-
|
145
|
-
|
142
|
+
|
143
|
+
Now, if you provide the ':scopes => :shallow' option, fancy named scopes
|
144
|
+
will be added to the class directly. In our User example, gender has
|
145
|
+
male/female options, so you can do:
|
146
|
+
|
147
|
+
User.female.each ... # => User.where({ :gender => :female })
|
146
148
|
|
147
149
|
|
148
150
|
You can chain named scopes as well:
|
@@ -106,13 +106,17 @@ module Symbolize::ActiveRecord
|
|
106
106
|
end
|
107
107
|
|
108
108
|
if scopes
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
109
|
+
if scopes == :shallow
|
110
|
+
values.each do |value|
|
111
|
+
name = value[0]
|
112
|
+
if name.respond_to?(:to_sym)
|
113
|
+
scope name.to_sym, where(attr_name => name.to_s)
|
114
|
+
# Figure out if this as another option, or default...
|
115
|
+
# scope_comm.call "not_#{attr_name}".to_sym, :conditions => { attr_name != name }
|
116
|
+
end
|
115
117
|
end
|
118
|
+
else
|
119
|
+
scope attr_name, ->(enum) { where(attr_name => enum) }
|
116
120
|
end
|
117
121
|
end
|
118
122
|
|
data/lib/symbolize/mongoid.rb
CHANGED
@@ -108,8 +108,15 @@ module Mongoid
|
|
108
108
|
end
|
109
109
|
|
110
110
|
if scopes
|
111
|
-
|
112
|
-
|
111
|
+
if scopes == :shallow
|
112
|
+
values.each do |k, v|
|
113
|
+
if k.respond_to?(:to_sym)
|
114
|
+
scope k.to_sym, where({ attr_name => k })
|
115
|
+
end
|
116
|
+
end
|
117
|
+
else # scoped scopes
|
118
|
+
scope attr_name, ->(enum) { where(attr_name => enum) }
|
119
|
+
end
|
113
120
|
end
|
114
121
|
|
115
122
|
if validation
|
data/lib/symbolize/version.rb
CHANGED
@@ -7,7 +7,7 @@ class User < ActiveRecord::Base
|
|
7
7
|
symbolize :other
|
8
8
|
symbolize :language, :in => [:pt, :en]
|
9
9
|
symbolize :sex, :in => [true, false], :scopes => true
|
10
|
-
symbolize :status , :in => [:active, :inactive], :i18n => false, :capitalize => true, :scopes =>
|
10
|
+
symbolize :status , :in => [:active, :inactive], :i18n => false, :capitalize => true, :scopes => :shallow, :methods => true
|
11
11
|
symbolize :so, :allow_blank => true, :in => {
|
12
12
|
:linux => 'Linux',
|
13
13
|
:mac => 'Mac OS X',
|
@@ -347,7 +347,7 @@ describe "Symbolize" do
|
|
347
347
|
end
|
348
348
|
|
349
349
|
it "should have other to test better" do
|
350
|
-
User.linux.should == [@anna]
|
350
|
+
User.so(:linux).should == [@anna]
|
351
351
|
end
|
352
352
|
|
353
353
|
# it "should have 'with' helper" do
|
@@ -12,7 +12,7 @@ class Person
|
|
12
12
|
|
13
13
|
symbolize :language, :in => [:pt, :en]
|
14
14
|
symbolize :sex, :type => Boolean, :scopes => true, :i18n => true
|
15
|
-
symbolize :status , :in => [:active, :inactive], :i18n => false, :capitalize => true, :scopes =>
|
15
|
+
symbolize :status , :in => [:active, :inactive], :i18n => false, :capitalize => true, :scopes => :shallow
|
16
16
|
symbolize :so, :allow_blank => true, :in => {
|
17
17
|
:linux => 'Linux',
|
18
18
|
:mac => 'Mac OS X',
|
@@ -359,10 +359,21 @@ describe "Symbolize" do
|
|
359
359
|
Project.count.should eql(1)
|
360
360
|
end
|
361
361
|
|
362
|
+
it "should now shallow scoped scopes" do
|
363
|
+
Person.create(:name => 'Bob' , :other => :bar, :status => :active, :so => :linux, :gui => :gtk, :language => :en, :sex => false, :cool => false)
|
364
|
+
Person.should_not respond_to(:status)
|
365
|
+
end
|
366
|
+
|
362
367
|
it "should set some scopes" do
|
363
|
-
Person.create(:name => 'Bob' , :other => :bar, :status => :active, :so => :
|
364
|
-
Person.
|
365
|
-
Person.
|
368
|
+
Person.create(:name => 'Bob' , :other => :bar, :status => :active, :so => :linux, :gui => :gtk, :language => :en, :sex => false, :cool => false)
|
369
|
+
Person.so(:linux).should be_a(Mongoid::Criteria)
|
370
|
+
Person.so(:linux).count.should eq(1)
|
371
|
+
end
|
372
|
+
|
373
|
+
it "should work with a shallow scope too" do
|
374
|
+
Person.create!(:name => 'Bob' , :other => :bar, :status => :active, :so => :linux, :gui => :gtk, :language => :en, :sex => false, :cool => false)
|
375
|
+
Person.active.should be_a(Mongoid::Criteria)
|
376
|
+
Person.active.count.should eq(1)
|
366
377
|
end
|
367
378
|
|
368
379
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: symbolize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.3.
|
4
|
+
version: 4.3.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02
|
12
|
+
date: 2013-03-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: i18n
|
@@ -158,7 +158,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
158
158
|
version: '0'
|
159
159
|
segments:
|
160
160
|
- 0
|
161
|
-
hash:
|
161
|
+
hash: 1985039689548470707
|
162
162
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
163
163
|
none: false
|
164
164
|
requirements:
|