mongoid_alphadog 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +95 -0
- data/lib/mongoid/alphadog.rb +34 -0
- data/lib/mongoid_alphadog/version.rb +1 -1
- data/mongoid_alphadog.gemspec +1 -1
- data/spec/models/item.rb +8 -0
- data/spec/models/user.rb +10 -0
- data/spec/mongoid_alphadog_spec.rb +67 -0
- data/spec/spec_helper.rb +32 -0
- metadata +31 -21
data/README.markdown
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
Mongoid_Alphadog
|
2
|
+
===================
|
3
|
+
|
4
|
+
A simple little gem that makes it easy to fetch a case-insensitive alphabetized list of Mongoid documents.
|
5
|
+
|
6
|
+
Problem
|
7
|
+
-------
|
8
|
+
|
9
|
+
By design, MongoDB does not sort case-insensitive when sorting string fields. So objects with names like this:
|
10
|
+
|
11
|
+
['aardvark', 'Archie', 'banana', 'Checkers']
|
12
|
+
|
13
|
+
... will come out like this if you do order_by(:name)
|
14
|
+
|
15
|
+
['aardvark', 'banana', 'Archie', 'Checkers']
|
16
|
+
|
17
|
+
That's probably not what you're expecting, especially when using this on the front-end of your app.
|
18
|
+
|
19
|
+
Mongoid_Alphadog makes it easier and cleaner to solve this in your app using the oft-recommended workaround of a lowercased field.
|
20
|
+
|
21
|
+
Requirements
|
22
|
+
------------
|
23
|
+
|
24
|
+
- MongoDB
|
25
|
+
- Mongoid
|
26
|
+
|
27
|
+
Installation
|
28
|
+
------------
|
29
|
+
|
30
|
+
Add mongoid_alphadog to your Gemfile:
|
31
|
+
|
32
|
+
gem 'mongoid_alphadog'
|
33
|
+
|
34
|
+
Alphabetizing a Single Field
|
35
|
+
----------------------------
|
36
|
+
|
37
|
+
Set up your mongoid document class for alphabetizing on a single field:
|
38
|
+
|
39
|
+
class Item
|
40
|
+
include Mongoid::Document
|
41
|
+
include Mongoid::Alphadog
|
42
|
+
|
43
|
+
field :name, type: String
|
44
|
+
|
45
|
+
alphabetize_on :name
|
46
|
+
end
|
47
|
+
|
48
|
+
By default, you will get a free scope that you can use to quickly return your Items in a naturally-alphabetized (A to Z) order:
|
49
|
+
|
50
|
+
Item.alphabetized_by(:name)
|
51
|
+
|
52
|
+
This is, of course, a Mongoid Criteria object, as you might expect, so you can do all the normal stuff:
|
53
|
+
|
54
|
+
Item.where(:color => 'red').alphabetized_by(:name).limit(10)
|
55
|
+
|
56
|
+
... will return 10 Items that have a color of 'red' alphabetized by name ascending (A to Z)
|
57
|
+
|
58
|
+
If you need your results ordered the other way, for now you'll just have to do it manually with _order_by_. Alphadog creates the following field on your document:
|
59
|
+
|
60
|
+
<OriginalFieldName>_loweralpha
|
61
|
+
|
62
|
+
So if you wanted, say, the Items from above to be ordered from Z to A, you would do this:
|
63
|
+
|
64
|
+
Item.order_by([[:name_loweralpha, :desc]])
|
65
|
+
|
66
|
+
Alphabetizing Multiple Fields
|
67
|
+
-----------------------------
|
68
|
+
|
69
|
+
Alphadog can handle multiple fields in a document too!
|
70
|
+
|
71
|
+
class User
|
72
|
+
include Mongoid::Document
|
73
|
+
include Mongoid::Alphadog
|
74
|
+
|
75
|
+
field :first_name, type: String
|
76
|
+
field :last_name, type: String
|
77
|
+
field :favorite_color, type: String
|
78
|
+
|
79
|
+
alphabetize_on :first_name, :last_name, :favorite_color
|
80
|
+
end
|
81
|
+
|
82
|
+
The _alphabetized_by_ scope can handle multi-field ordering:
|
83
|
+
|
84
|
+
User.alphabetized_by(:first_name, :last_name, :favorite_color)
|
85
|
+
|
86
|
+
Again, this will do all of the fields in :asc order (A to Z). If you want to do it differently, you'll have to build up your own _order_by_
|
87
|
+
|
88
|
+
User.order_by([[:first_name_loweralpha, :desc], [:last_name_loweralpha, :asc], [:favorite_color_loweralpha, :desc]])
|
89
|
+
|
90
|
+
Indexes
|
91
|
+
-------
|
92
|
+
|
93
|
+
Mongoid_Alphadog automatically adds indexes on the XXX_loweralpha fields. Make sure to generate your mongodb indexes by whichever means you prefer.
|
94
|
+
|
95
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Alphadog
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
|
7
|
+
def alphabetize_on(*fields)
|
8
|
+
return false unless fields.present?
|
9
|
+
fields.map(&:to_s).each { |f|
|
10
|
+
symbol = "#{f}_loweralpha".to_sym
|
11
|
+
field(symbol, type: String)
|
12
|
+
index(symbol)
|
13
|
+
meth = "set_#{f}_loweralpha".to_sym
|
14
|
+
class_eval <<-RUBY
|
15
|
+
before_save do
|
16
|
+
self.send("#{symbol}=".to_sym, self.send(f.to_sym).downcase)
|
17
|
+
end
|
18
|
+
RUBY
|
19
|
+
}
|
20
|
+
|
21
|
+
instance_eval do
|
22
|
+
scope :alphabetized_by, lambda { |*alpha_fields|
|
23
|
+
aorder = alpha_fields.map do |alpha|
|
24
|
+
[[alpha.to_s, 'loweralpha'].join('_').to_sym, :asc]
|
25
|
+
end
|
26
|
+
order_by(aorder)
|
27
|
+
}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
data/mongoid_alphadog.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.version = MongoidAlphadog::VERSION
|
8
8
|
s.authors = ["Matt Patterson", "Lone Star Internet"]
|
9
9
|
s.email = ["mpatterson@lone-star.net"]
|
10
|
-
s.homepage = "
|
10
|
+
s.homepage = "https://github.com/LoneStarInternet/mongoid_alphadog"
|
11
11
|
s.summary = %q{Case-insensitive alphabetizing for Mongoid document models}
|
12
12
|
s.description = %q{A simple little gem that makes it easy to handle case-insensitive alphabetizing for Mongoid document models}
|
13
13
|
|
data/spec/models/item.rb
ADDED
data/spec/models/user.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Mongoid::Alphadog, "with 1 field provided" do
|
4
|
+
|
5
|
+
it "should include :name_loweralpha field" do
|
6
|
+
Item.fields['name_loweralpha'].should_not be_nil
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should include index on name_loweralpha" do
|
10
|
+
Item.index_options[:name_loweralpha].should_not be_nil
|
11
|
+
end
|
12
|
+
|
13
|
+
%w[ Rabbit xylophone antelope ].each do |name|
|
14
|
+
let name.to_s.downcase do
|
15
|
+
Item.create(:name => name)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should set the loweralpha field properly" do
|
20
|
+
rabbit.name_loweralpha.should == 'rabbit'
|
21
|
+
xylophone.name_loweralpha.should == 'xylophone'
|
22
|
+
antelope.name_loweralpha.should == 'antelope'
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should produce the right results with the alphabetized_by scope" do
|
26
|
+
provided = %w[ Rabbit Aardvark xylophone fairy Zebra antelope ]
|
27
|
+
expected = %w[ Aardvark antelope fairy Rabbit xylophone Zebra ]
|
28
|
+
provided.each { |name| Item.create(:name => name) }
|
29
|
+
Item.alphabetized_by(:name).map(&:name).should == expected
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
describe Mongoid::Alphadog, "with multiple fields provided" do
|
35
|
+
|
36
|
+
it "should include three loweralpha fields" do
|
37
|
+
User.fields['first_name_loweralpha'].should_not be_nil
|
38
|
+
User.fields['last_name_loweralpha'].should_not be_nil
|
39
|
+
User.fields['favorite_color_loweralpha'].should_not be_nil
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should include indexes" do
|
43
|
+
User.index_options[:first_name_loweralpha].should_not be_nil
|
44
|
+
User.index_options[:last_name_loweralpha].should_not be_nil
|
45
|
+
User.index_options[:favorite_color_loweralpha].should_not be_nil
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should set the loweralpha fields properly" do
|
49
|
+
user = User.create(:first_name => 'John', :last_name => 'Doe', :favorite_color => 'orange')
|
50
|
+
user.first_name.should == 'John'
|
51
|
+
user.first_name_loweralpha.should == 'john'
|
52
|
+
user.last_name_loweralpha.should == 'doe'
|
53
|
+
user.favorite_color_loweralpha.should == 'orange'
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should produce the right results with the alphabetized_by scope" do
|
57
|
+
user1 = User.create(:first_name => 'John', :last_name => 'Doe', :favorite_color => 'orange')
|
58
|
+
user2 = User.create(:first_name => 'Mary', :last_name => 'Doe', :favorite_color => 'red')
|
59
|
+
user3 = User.create(:first_name => 'Steve', :last_name => 'Jobs', :favorite_color => 'orange')
|
60
|
+
user4 = User.create(:first_name => 'Arnold', :last_name => 'Bodybuilder', :favorite_color => 'blue')
|
61
|
+
user5 = User.create(:first_name => 'frank', :last_name => 'jobs', :favorite_color => 'blue')
|
62
|
+
user6 = User.create(:first_name => 'Frank', :last_name => 'Imitator', :favorite_color => 'green')
|
63
|
+
results = User.alphabetized_by(:first_name, :last_name, :favorite_color).map(&:first_name)
|
64
|
+
results.should == %w[ Arnold Frank frank John Mary Steve ]
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require "mongoid"
|
6
|
+
require "rspec"
|
7
|
+
require "database_cleaner"
|
8
|
+
|
9
|
+
require File.expand_path("../../lib/mongoid/alphadog", __FILE__)
|
10
|
+
|
11
|
+
MODELS = File.join(File.dirname(__FILE__), "models")
|
12
|
+
|
13
|
+
Dir["#{File.dirname(__FILE__)}/models/*.rb"].each { |f| require f }
|
14
|
+
|
15
|
+
Mongoid.config.master = Mongo::Connection.new.db("mongoid-alphadog-spec")
|
16
|
+
Mongoid.logger = Logger.new($stdout)
|
17
|
+
|
18
|
+
DatabaseCleaner.orm = "mongoid"
|
19
|
+
|
20
|
+
RSpec.configure do |config|
|
21
|
+
config.before(:all) do
|
22
|
+
DatabaseCleaner.strategy = :truncation
|
23
|
+
end
|
24
|
+
|
25
|
+
config.before(:each) do
|
26
|
+
DatabaseCleaner.start
|
27
|
+
end
|
28
|
+
|
29
|
+
config.after(:each) do
|
30
|
+
DatabaseCleaner.clean
|
31
|
+
end
|
32
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_alphadog
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -14,7 +14,7 @@ date: 2012-02-15 00:00:00.000000000 Z
|
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: mongoid
|
17
|
-
requirement: &
|
17
|
+
requirement: &2153771000 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: 2.0.2
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *2153771000
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: rdoc
|
28
|
-
requirement: &
|
28
|
+
requirement: &2153770420 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: 2.5.11
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *2153770420
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: bson_ext
|
39
|
-
requirement: &
|
39
|
+
requirement: &2153769940 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ! '>='
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: 1.3.1
|
45
45
|
type: :development
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *2153769940
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: rspec-core
|
50
|
-
requirement: &
|
50
|
+
requirement: &2153769460 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ! '>='
|
@@ -55,10 +55,10 @@ dependencies:
|
|
55
55
|
version: 2.6.3
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
58
|
+
version_requirements: *2153769460
|
59
59
|
- !ruby/object:Gem::Dependency
|
60
60
|
name: rspec
|
61
|
-
requirement: &
|
61
|
+
requirement: &2153768920 !ruby/object:Gem::Requirement
|
62
62
|
none: false
|
63
63
|
requirements:
|
64
64
|
- - ! '>='
|
@@ -66,10 +66,10 @@ dependencies:
|
|
66
66
|
version: 2.6.0
|
67
67
|
type: :development
|
68
68
|
prerelease: false
|
69
|
-
version_requirements: *
|
69
|
+
version_requirements: *2153768920
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
71
|
name: database_cleaner
|
72
|
-
requirement: &
|
72
|
+
requirement: &2153768440 !ruby/object:Gem::Requirement
|
73
73
|
none: false
|
74
74
|
requirements:
|
75
75
|
- - ! '>='
|
@@ -77,10 +77,10 @@ dependencies:
|
|
77
77
|
version: 0.6.7
|
78
78
|
type: :development
|
79
79
|
prerelease: false
|
80
|
-
version_requirements: *
|
80
|
+
version_requirements: *2153768440
|
81
81
|
- !ruby/object:Gem::Dependency
|
82
82
|
name: bundler
|
83
|
-
requirement: &
|
83
|
+
requirement: &2153767920 !ruby/object:Gem::Requirement
|
84
84
|
none: false
|
85
85
|
requirements:
|
86
86
|
- - ~>
|
@@ -88,10 +88,10 @@ dependencies:
|
|
88
88
|
version: 1.0.0
|
89
89
|
type: :development
|
90
90
|
prerelease: false
|
91
|
-
version_requirements: *
|
91
|
+
version_requirements: *2153767920
|
92
92
|
- !ruby/object:Gem::Dependency
|
93
93
|
name: jeweler
|
94
|
-
requirement: &
|
94
|
+
requirement: &2153767400 !ruby/object:Gem::Requirement
|
95
95
|
none: false
|
96
96
|
requirements:
|
97
97
|
- - ~>
|
@@ -99,7 +99,7 @@ dependencies:
|
|
99
99
|
version: 1.6.0
|
100
100
|
type: :development
|
101
101
|
prerelease: false
|
102
|
-
version_requirements: *
|
102
|
+
version_requirements: *2153767400
|
103
103
|
description: A simple little gem that makes it easy to handle case-insensitive alphabetizing
|
104
104
|
for Mongoid document models
|
105
105
|
email:
|
@@ -110,11 +110,17 @@ extra_rdoc_files: []
|
|
110
110
|
files:
|
111
111
|
- .gitignore
|
112
112
|
- Gemfile
|
113
|
+
- README.markdown
|
113
114
|
- Rakefile
|
115
|
+
- lib/mongoid/alphadog.rb
|
114
116
|
- lib/mongoid_alphadog.rb
|
115
117
|
- lib/mongoid_alphadog/version.rb
|
116
118
|
- mongoid_alphadog.gemspec
|
117
|
-
|
119
|
+
- spec/models/item.rb
|
120
|
+
- spec/models/user.rb
|
121
|
+
- spec/mongoid_alphadog_spec.rb
|
122
|
+
- spec/spec_helper.rb
|
123
|
+
homepage: https://github.com/LoneStarInternet/mongoid_alphadog
|
118
124
|
licenses: []
|
119
125
|
post_install_message:
|
120
126
|
rdoc_options: []
|
@@ -128,7 +134,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
128
134
|
version: '0'
|
129
135
|
segments:
|
130
136
|
- 0
|
131
|
-
hash:
|
137
|
+
hash: 1901341022087321056
|
132
138
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
139
|
none: false
|
134
140
|
requirements:
|
@@ -137,11 +143,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
143
|
version: '0'
|
138
144
|
segments:
|
139
145
|
- 0
|
140
|
-
hash:
|
146
|
+
hash: 1901341022087321056
|
141
147
|
requirements: []
|
142
148
|
rubyforge_project: mongoid_alphadog
|
143
149
|
rubygems_version: 1.8.10
|
144
150
|
signing_key:
|
145
151
|
specification_version: 3
|
146
152
|
summary: Case-insensitive alphabetizing for Mongoid document models
|
147
|
-
test_files:
|
153
|
+
test_files:
|
154
|
+
- spec/models/item.rb
|
155
|
+
- spec/models/user.rb
|
156
|
+
- spec/mongoid_alphadog_spec.rb
|
157
|
+
- spec/spec_helper.rb
|