mongoid-sorted-relations 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/LICENSE +7 -0
- data/README.md +69 -0
- data/Rakefile +11 -0
- data/lib/mongoid-sorted-relations.rb +2 -0
- data/lib/mongoid/fake_criteria.rb +19 -0
- data/lib/mongoid/sorted_relations.rb +27 -0
- data/spec/mongoid/fake_criteria_spec.rb +19 -0
- data/spec/mongoid/sorted_relations_spec.rb +14 -0
- data/spec/spec_helper.rb +24 -0
- data/spec/support/models/author.rb +9 -0
- data/spec/support/models/book.rb +13 -0
- data/spec/support/models/chapter.rb +9 -0
- metadata +118 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2012 De Marque inc.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
Mongoid Sorted Relations [![Build Status](https://secure.travis-ci.org/demarque/mongoid-sorted-relations.png?branch=master)](http://travis-ci.org/demarque/mongoid-sorted-relations)
|
2
|
+
===============
|
3
|
+
|
4
|
+
Problem
|
5
|
+
-------
|
6
|
+
|
7
|
+
In Mongoid, the HABTM relations return docs in the wrong order.
|
8
|
+
This gem will give your document the ability to retrieve it's relations in the same order it was placed in.
|
9
|
+
|
10
|
+
For more details about this issue, see [#1548](https://github.com/mongoid/mongoid/issues/1548)
|
11
|
+
|
12
|
+
Install
|
13
|
+
-------
|
14
|
+
|
15
|
+
```
|
16
|
+
gem install mongoid-sorted-relations
|
17
|
+
```
|
18
|
+
|
19
|
+
Rails 3
|
20
|
+
-------
|
21
|
+
|
22
|
+
In your Gemfile:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem 'mongoid-sorted-relations'
|
26
|
+
```
|
27
|
+
|
28
|
+
Usage
|
29
|
+
-----
|
30
|
+
|
31
|
+
#### Model integration
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class Book
|
35
|
+
include Mongoid::Document
|
36
|
+
include Mongoid::SortedRelations
|
37
|
+
|
38
|
+
has_many :chapters
|
39
|
+
has_and_belongs_to_many :authors
|
40
|
+
|
41
|
+
init_sorted_relations
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
It's important to place the ```init_sorted_relations``` after the declaration of your relations.
|
46
|
+
|
47
|
+
|
48
|
+
### Getting the relations in the right order
|
49
|
+
|
50
|
+
Mongoid::SortedRelations will add a ```sorted_``` methods to all the has_many and habtm relationships.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
book = Book.new title: 'The Art of War'
|
54
|
+
book.authors << Author.create name: "Sun Tzu"
|
55
|
+
book.authors << Author.create name: "Sun Wu"
|
56
|
+
book.authors << Author.create name: "Lao Zi"
|
57
|
+
|
58
|
+
book.sorted_authors.map(&:name)
|
59
|
+
#=> ['Sun Tzu', 'Sun Wu', 'Lao Zi']
|
60
|
+
|
61
|
+
book.sorted_authors.limit(2).map(&:name)
|
62
|
+
#=> ['Sun Tzu', 'Sun Wu']
|
63
|
+
```
|
64
|
+
|
65
|
+
|
66
|
+
Copyright
|
67
|
+
---------
|
68
|
+
|
69
|
+
Copyright (c) 2012 De Marque inc. See LICENSE for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Mongoid
|
2
|
+
class FakeCriteria
|
3
|
+
def initialize(documents)
|
4
|
+
@documents = documents
|
5
|
+
end
|
6
|
+
|
7
|
+
def limit(quantity)
|
8
|
+
Mongoid::FakeCriteria.new(@documents[0..quantity-1])
|
9
|
+
end
|
10
|
+
|
11
|
+
def raw
|
12
|
+
@documents
|
13
|
+
end
|
14
|
+
|
15
|
+
def method_missing(*args, &block)
|
16
|
+
@documents.send(*args, &block)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module SortedRelations
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def init_sorted_relations
|
11
|
+
self.relations.each do |k,v|
|
12
|
+
if [:references_many, :references_and_referenced_in_many].include? v.macro
|
13
|
+
send(:define_method, "sorted_#{v.name}") { sorted_relation v }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def sorted_relation(relation)
|
20
|
+
ids = self.send(relation.key).map{ |rid| rid.to_s }
|
21
|
+
|
22
|
+
documents = self.send(relation.name).sort_by { |x| ids.index(x.id.to_s) }
|
23
|
+
|
24
|
+
return Mongoid::FakeCriteria.new(documents)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::FakeCriteria do
|
4
|
+
|
5
|
+
context "with the books ['book 1', 'book 2', 'book 3', 'book 4']" do
|
6
|
+
let(:books) { Mongoid::FakeCriteria.new(['book 1', 'book 2', 'book 3', 'book 4']) }
|
7
|
+
|
8
|
+
describe "#limit" do
|
9
|
+
the ("books.limit(2).raw") { should eql ['book 1', 'book 2'] }
|
10
|
+
the ("books.limit(0).raw") { should eql ['book 1', 'book 2', 'book 3', 'book 4'] }
|
11
|
+
the ("books.limit(6).raw") { should eql ['book 1', 'book 2', 'book 3', 'book 4'] }
|
12
|
+
end
|
13
|
+
|
14
|
+
context "#count" do
|
15
|
+
the ("books.count") { should eql 4 }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::SortedRelations do
|
4
|
+
context "having the book The Art of War" do
|
5
|
+
let(:book) { Book.new(:title => 'The Art of War') }
|
6
|
+
|
7
|
+
context "written by the authors Sun Tzu, Sun Wu and Lao Zi" do
|
8
|
+
before { book.author_ids = [Author.create(:name => 'Sun Tzu').id, Author.create(:name => 'Sun Wu').id, Author.create(:name => 'Lao Zi').id] }
|
9
|
+
before { book.save and book.reload }
|
10
|
+
|
11
|
+
the("book.sorted_authors.map(&:name)") { should eql ['Sun Tzu', 'Sun Wu', 'Lao Zi'] }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
|
4
|
+
require 'rspec'
|
5
|
+
require 'rspec-aspic'
|
6
|
+
|
7
|
+
require 'mongoid'
|
8
|
+
require File.expand_path('../../lib/mongoid-sorted-relations', __FILE__)
|
9
|
+
|
10
|
+
|
11
|
+
Mongoid.configure do |config|
|
12
|
+
config.master = Mongo::Connection.new.db('mongoid_sortable_relations_test')
|
13
|
+
config.allow_dynamic_fields = false
|
14
|
+
end
|
15
|
+
|
16
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
17
|
+
|
18
|
+
RSpec.configure do |config|
|
19
|
+
config.mock_with :rspec
|
20
|
+
config.include RSpecAspic
|
21
|
+
config.after :each do
|
22
|
+
Mongoid.master.collections.reject { |c| c.name =~ /^system\./ }.each(&:drop)
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-sorted-relations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sebastien Rosa
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongoid
|
16
|
+
requirement: &2168955220 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2168955220
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: bson_ext
|
27
|
+
requirement: &2168680680 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '1.6'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2168680680
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &2168678240 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.8.7
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2168678240
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: &2168675460 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *2168675460
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rspec-aspic
|
60
|
+
requirement: &2168674340 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 0.0.2
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *2168674340
|
69
|
+
description: Preserve the order of the mongoid documents in a referenced one-to-many
|
70
|
+
or many-to-many relationship.
|
71
|
+
email:
|
72
|
+
- sebastien@demarque.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files:
|
76
|
+
- LICENSE
|
77
|
+
- README.md
|
78
|
+
files:
|
79
|
+
- lib/mongoid/fake_criteria.rb
|
80
|
+
- lib/mongoid/sorted_relations.rb
|
81
|
+
- lib/mongoid-sorted-relations.rb
|
82
|
+
- spec/mongoid/fake_criteria_spec.rb
|
83
|
+
- spec/mongoid/sorted_relations_spec.rb
|
84
|
+
- spec/spec_helper.rb
|
85
|
+
- spec/support/models/author.rb
|
86
|
+
- spec/support/models/book.rb
|
87
|
+
- spec/support/models/chapter.rb
|
88
|
+
- LICENSE
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- Gemfile
|
92
|
+
homepage: https://github.com/demarque/mongoid-sorted-relations
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ! '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project: mongoid-sorted-relations
|
113
|
+
rubygems_version: 1.8.17
|
114
|
+
signing_key:
|
115
|
+
specification_version: 3
|
116
|
+
summary: Preserve the order of the mongoid documents in a referenced one-to-many or
|
117
|
+
many-to-many relationship.
|
118
|
+
test_files: []
|