mongoid-sortable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid-sortable.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Elliott Pogue
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Mongoid::Sortable
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mongoid-sortable'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mongoid-sortable
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,71 @@
1
+ require 'mongoid-sortable/version'
2
+ require 'active_support/concern'
3
+
4
+ module Mongoid
5
+ module Sortable
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ cattr_accessor :mongoid_sortable_options
10
+ self.mongoid_sortable_options = {}
11
+
12
+ field :position, type: Integer, default: 1
13
+
14
+ before_create :set_position
15
+ after_destroy :set_sibling_positions
16
+ end
17
+
18
+ module ClassMethods
19
+ def sortable(options = {})
20
+ options = {
21
+ scope: []
22
+ }.merge(options)
23
+
24
+ options[:scope].map!(&:to_sym)
25
+
26
+ self.mongoid_sortable_options = options
27
+ end
28
+ end
29
+
30
+ def previous
31
+ position_scope.lt(position: position).order_by('position desc').first
32
+ end
33
+
34
+ def next
35
+ position_scope.gt(position: position).order_by('position asc').first
36
+ end
37
+
38
+ def reorder(ids)
39
+ ids.map!(&:to_s) # ensure entities are strings
40
+ ids.each_with_index do |id, index|
41
+ position_scope.find(id).set(:position, index + 1)
42
+ end
43
+ end
44
+
45
+ def position_at(new_position)
46
+ position_scope.gt(position: position).each{|d| d.inc(:position, -1) }
47
+ set(:position, nil)
48
+ position_scope.gte(position: new_position).each{|d| d.inc(:position, 1) }
49
+ set(:position, new_position)
50
+ end
51
+
52
+ def set_position
53
+ self.position = position_scope.count + (embedded? ? 0 : 1)
54
+ end
55
+
56
+ def set_sibling_positions
57
+ position_scope.gt(position: position).each{|d| d.inc(:position, -1) }
58
+ end
59
+
60
+ def relation
61
+ (embedded? ? send(self.metadata.inverse).send(self.metadata.name) : self.class).unscoped.scoped
62
+ end
63
+
64
+ def position_scope(options = {})
65
+ scopes = Array(mongoid_sortable_options[:scope])
66
+ scopes.inject(relation) do |criteria, scope_field|
67
+ criteria.where(scope_field => self.send(scope_field))
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module Sortable
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/mongoid-sortable/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Elliott Pogue"]
6
+ gem.email = ["epogue@gmail.com"]
7
+ # gem.description = %q{TODO: Write a gem description}
8
+ gem.summary = %q{A Mongoid 3.0 module for sorting}
9
+ gem.homepage = "https://github.com/epogue/mongoid-sortable"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "mongoid-sortable"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Mongoid::Sortable::VERSION
17
+
18
+ gem.add_dependency 'mongoid', '~> 3.0.0.rc'
19
+
20
+ gem.add_development_dependency 'rspec'
21
+ gem.add_development_dependency 'database_cleaner'
22
+ gem.add_development_dependency 'factory_girl', '~> 3.0'
23
+ end
@@ -0,0 +1,8 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: mongoid_sortable_test
5
+ hosts:
6
+ - localhost:27017
7
+ options:
8
+ consistency: :strong
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe "MongoidSortable" do
4
+ describe "sortable items" do
5
+ let(:parent) { FactoryGirl.create(:parent_document) }
6
+ let!(:document1) { FactoryGirl.create(:document, parent_document: parent) }
7
+ let!(:document2) { FactoryGirl.create(:document, parent_document: parent) }
8
+ let!(:document3) { FactoryGirl.create(:document, parent_document: parent) }
9
+
10
+ it_should_behave_like 'a sortable item'
11
+
12
+ it "should properly sort" do
13
+ parent.documents.order_by('position asc').to_a.should == [document1, document2, document3]
14
+ end
15
+
16
+ context "with distinct parents" do
17
+ let(:parent1) { FactoryGirl.create(:parent_document) }
18
+ let(:parent2) { FactoryGirl.create(:parent_document) }
19
+ let!(:p1_doc1) { FactoryGirl.create(:document, parent_document: parent1) }
20
+ let!(:p1_doc2) { FactoryGirl.create(:document, parent_document: parent1) }
21
+ let!(:p2_doc1) { FactoryGirl.create(:document, parent_document: parent2) }
22
+ let!(:p2_doc2) { FactoryGirl.create(:document, parent_document: parent2) }
23
+
24
+ it "should have their proper positions" do
25
+ p1_doc1.position.should == 1
26
+ p1_doc2.position.should == 2
27
+ p2_doc1.position.should == 1
28
+ p2_doc2.position.should == 2
29
+ end
30
+
31
+ it "should scope previous/next" do
32
+ p1_doc1.next.should == p1_doc2
33
+ p1_doc2.previous.should == p1_doc1
34
+ p2_doc1.next.should == p2_doc2
35
+ p2_doc2.previous.should == p2_doc1
36
+ end
37
+
38
+ it "should scope position_at" do
39
+ p1_doc1.position_at(2)
40
+ p1_doc1.reload.position.should == 2
41
+ p1_doc2.reload.position.should == 1
42
+ p2_doc1.reload.position.should == 1
43
+ p2_doc2.reload.position.should == 2
44
+ end
45
+ end
46
+ end
47
+
48
+ describe "sortable embedded documents" do
49
+ let(:parent) { FactoryGirl.create(:document) }
50
+ let!(:document1) { FactoryGirl.create(:embedded_document, document: parent) }
51
+ let!(:document2) { FactoryGirl.create(:embedded_document, document: parent) }
52
+ let!(:document3) { FactoryGirl.create(:embedded_document, document: parent) }
53
+
54
+ it_should_behave_like 'a sortable item'
55
+
56
+ it "should properly sort" do
57
+ parent.embedded_documents.order_by('position asc').to_a.should == [document1, document2, document3]
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,21 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+ plugin_test_dir = File.dirname(__FILE__)
3
+
4
+ require 'rubygems'
5
+ require 'bundler/setup'
6
+
7
+ require 'rspec'
8
+ require 'logger'
9
+
10
+ require 'active_support'
11
+ require 'active_model'
12
+ require 'mongoid'
13
+
14
+ Dir["./spec/support/**/*.rb"].each {|f| require f}
15
+
16
+ ENV["MONGOID_ENV"] = "test"
17
+ Mongoid.load!(plugin_test_dir + "/db/mongoid.yml")
18
+
19
+ RSpec.configure do |config|
20
+ config.treat_symbols_as_metadata_keys_with_true_values = true
21
+ end
@@ -0,0 +1,21 @@
1
+ require 'factory_girl'
2
+
3
+ FactoryGirl.define do
4
+ factory :parent_document do
5
+ trait :with_documents do
6
+ after(:create) do |parent, evaluator|
7
+ 3.times { FactoryGirl.create(:document, parent_document: parent) }
8
+ end
9
+ end
10
+ end
11
+
12
+ factory :document do
13
+ trait :with_embedded_docs do
14
+ after(:create) do |parent, evaluator|
15
+ 3.times { FactoryGirl.create(:embedded_document, document: parent) }
16
+ end
17
+ end
18
+ end
19
+
20
+ factory :embedded_document
21
+ end
@@ -0,0 +1,24 @@
1
+ require 'mongoid-sortable'
2
+
3
+ class ParentDocument
4
+ include Mongoid::Document
5
+
6
+ has_many :documents
7
+ end
8
+
9
+ class Document
10
+ include Mongoid::Document
11
+ include Mongoid::Sortable
12
+
13
+ belongs_to :parent_document
14
+ embeds_many :embedded_documents
15
+ sortable scope: [:parent_document_id]
16
+ end
17
+
18
+ class EmbeddedDocument
19
+ include Mongoid::Document
20
+ include Mongoid::Sortable
21
+
22
+ embedded_in :document
23
+ sortable
24
+ end
@@ -0,0 +1,16 @@
1
+ require 'database_cleaner'
2
+
3
+ RSpec.configure do |config|
4
+ config.before(:suite) do
5
+ DatabaseCleaner[:mongoid].strategy = :truncation
6
+ DatabaseCleaner.clean_with(:truncation)
7
+ end
8
+
9
+ config.before(:each) do
10
+ DatabaseCleaner[:mongoid].start
11
+ end
12
+
13
+ config.after(:each) do
14
+ DatabaseCleaner[:mongoid].clean
15
+ end
16
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for 'a sortable item' do
4
+ it { document1.position.should == 1 }
5
+ it { document2.position.should == 2 }
6
+ it { document3.position.should == 3 }
7
+
8
+ it "should get the next item" do
9
+ document2.next.should == document3
10
+ end
11
+
12
+ it "should get the previous item" do
13
+ document2.previous.should == document1
14
+ end
15
+
16
+ it "should be positioned at new position" do
17
+ document1.position_at(2)
18
+ document1.reload.position.should == 2
19
+ end
20
+
21
+ it "should reposition others when repositioned" do
22
+ document1.position_at(2)
23
+ document2.reload.position.should == 1
24
+ document3.reload.position.should == 3
25
+ end
26
+
27
+ it "should reposition others when destroyed" do
28
+ document1.destroy
29
+ document2.reload.position.should == 1
30
+ document3.reload.position.should == 2
31
+ end
32
+
33
+ it "should reorder items when given an array" do
34
+ the_order = [document2.id.to_s, document3.id.to_s, document1.id.to_s]
35
+ document1.reorder(the_order)
36
+ document1.reload.position.should == 3
37
+ document2.reload.position.should == 1
38
+ document3.reload.position.should == 2
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-sortable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Elliott Pogue
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0.rc
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0.rc
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: database_cleaner
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: factory_girl
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '3.0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '3.0'
78
+ description:
79
+ email:
80
+ - epogue@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - Gemfile
88
+ - LICENSE
89
+ - README.md
90
+ - Rakefile
91
+ - lib/mongoid-sortable.rb
92
+ - lib/mongoid-sortable/version.rb
93
+ - mongoid-sortable.gemspec
94
+ - spec/db/mongoid.yml
95
+ - spec/mongoid_sortable_spec.rb
96
+ - spec/spec_helper.rb
97
+ - spec/support/factories.rb
98
+ - spec/support/models.rb
99
+ - spec/support/mongoid.rb
100
+ - spec/support/shared_examples_for_a_sortable_item.rb
101
+ homepage: https://github.com/epogue/mongoid-sortable
102
+ licenses: []
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.23
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: A Mongoid 3.0 module for sorting
125
+ test_files:
126
+ - spec/db/mongoid.yml
127
+ - spec/mongoid_sortable_spec.rb
128
+ - spec/spec_helper.rb
129
+ - spec/support/factories.rb
130
+ - spec/support/models.rb
131
+ - spec/support/mongoid.rb
132
+ - spec/support/shared_examples_for_a_sortable_item.rb