mongoid_order 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mongoid_order.gemspec
4
+ gemspec
5
+
6
+ gem 'bson_ext', '>= 1.0.4'
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Ark Xu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,68 @@
1
+ = mongoid_order
2
+
3
+ An easy way to make mongoid documentation order-able.
4
+
5
+ This class is extracted from benedikt's great gem https://github.com/benedikt/mongoid-tree with some modifications. Thanks to benedikt!
6
+
7
+
8
+ == Requirements
9
+
10
+ * mongoid (>= 2.0.0.beta.20)
11
+
12
+
13
+ == Install
14
+
15
+ To install mongoid_order, simply add it to your Gemfile:
16
+
17
+ gem 'mongoid_order'
18
+
19
+ In order to get the latest development version of mongoid-tree:
20
+
21
+ gem 'mongoid_order', :git => 'https://github.com/arkxu/mongoid_order.git'
22
+
23
+ And then:
24
+ bundle install
25
+
26
+
27
+ == Usage
28
+
29
+ Add the <tt>include Mongoid::Orderable</tt> in the model:
30
+
31
+ class Node
32
+ include Mongoid::Document
33
+ include Mongoid::Orderable
34
+ end
35
+
36
+
37
+ === Ordering
38
+
39
+ This will add a <tt>position</tt> field to your document and provide additional utility methods:
40
+
41
+ node.move_up
42
+ node.move_down
43
+ node.move_to_top
44
+ node.move_to_bottom
45
+ node.move_above(other)
46
+ node.move_below(other)
47
+
48
+ node.at_top?
49
+ node.at_bottom?
50
+
51
+ == Known issues
52
+
53
+ See https://github.com/arkxu/mongoid_order/issues
54
+
55
+
56
+ == Repository
57
+
58
+ See https://github.com/arkxu/mongoid_order and feel free to fork it!
59
+
60
+
61
+ == Contributors
62
+
63
+ See a list of all contributors at https://github.com/arkxu/mongoid_order/contributors. Thanks a lot everyone!
64
+
65
+
66
+ == Copyright
67
+
68
+ Copyright (c) 2011 Ark Xu. See LICENSE for details.
@@ -0,0 +1,24 @@
1
+ require 'rake/testtask'
2
+ require 'rake/gempackagetask'
3
+
4
+ require 'bundler'
5
+ Bundler::GemHelper.install_tasks
6
+
7
+
8
+ task :default => 'test'
9
+
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << '.' << 'lib' << 'test'
12
+ t.pattern = 'test/lib/**/*_test.rb'
13
+ t.verbose = false
14
+ end
15
+
16
+ gemspec = eval(File.read('mongoid_order.gemspec'))
17
+ Rake::GemPackageTask.new(gemspec) do |pkg|
18
+ pkg.gem_spec = gemspec
19
+ end
20
+
21
+ desc "build the gem and release it to rubygems.org"
22
+ task :release => :gem do
23
+ sh "gem push pkg/mongoid_order-#{gemspec.version}.gem"
24
+ end
@@ -0,0 +1,153 @@
1
+ module Mongoid
2
+ ##
3
+ # = Mongoid::Orderable
4
+ #
5
+ # == Utility methods
6
+ #
7
+ # There are several methods to move nodes around in the list:
8
+ #
9
+ # node.move_up
10
+ # node.move_down
11
+ # node.move_to_top
12
+ # node.move_to_bottom
13
+ # node.move_above(other)
14
+ # node.move_below(other)
15
+ #
16
+ # Additionally there are some methods to check aspects of the document
17
+ # in the list of children:
18
+ #
19
+ # node.at_top?
20
+ # node.at_bottom?
21
+ module Orderable
22
+ extend ActiveSupport::Concern
23
+
24
+ included do
25
+ field :position, :type => Integer
26
+
27
+ before_save :assign_default_position
28
+ after_destroy :move_lower_siblings_up
29
+ end
30
+
31
+ ##
32
+ # Returns siblings below the current document.
33
+ # Siblings with a position greater than this documents's position.
34
+ def lower_siblings
35
+ self.class.all.where(:position.gt => self.position)
36
+ end
37
+
38
+ ##
39
+ # Returns siblings above the current document.
40
+ # Siblings with a position lower than this documents's position.
41
+ def higher_siblings
42
+ self.class.all.where(:position.lt => self.position)
43
+ end
44
+
45
+ ##
46
+ # Returns the lowest sibling (could be self)
47
+ def last_sibling_in_list
48
+ self.class.all.asc(:position).last
49
+ end
50
+
51
+ ##
52
+ # Returns the highest sibling (could be self)
53
+ def first_sibling_in_list
54
+ self.class.all.asc(:position).first
55
+ end
56
+
57
+ ##
58
+ # Is this the highest sibling?
59
+ def at_top?
60
+ higher_siblings.empty?
61
+ end
62
+
63
+ ##
64
+ # Is this the lowest sibling?
65
+ def at_bottom?
66
+ lower_siblings.empty?
67
+ end
68
+
69
+ ##
70
+ # Move this node above all its siblings
71
+ def move_to_top
72
+ return true if at_top?
73
+ move_above(first_sibling_in_list)
74
+ end
75
+
76
+ ##
77
+ # Move this node below all its siblings
78
+ def move_to_bottom
79
+ return true if at_bottom?
80
+ move_below(last_sibling_in_list)
81
+ end
82
+
83
+ ##
84
+ # Move this node one position up
85
+ def move_up
86
+ return if at_top?
87
+ self.class.all.where(:position => self.position - 1).first.inc(:position, 1)
88
+ inc(:position, -1)
89
+ end
90
+
91
+ ##
92
+ # Move this node one position down
93
+ def move_down
94
+ return if at_bottom?
95
+ self.class.all.where(:position => self.position + 1).first.inc(:position, -1)
96
+ inc(:position, 1)
97
+ end
98
+
99
+ ##
100
+ # Move this node above the specified node
101
+ #
102
+ # This method changes the node's parent if nescessary.
103
+ def move_above(other)
104
+ if position > other.position
105
+ new_position = other.position
106
+ other.lower_siblings.where(:position.lt => self.position).each { |s| s.inc(:position, 1) }
107
+ other.inc(:position, 1)
108
+ self.position = new_position
109
+ save!
110
+ else
111
+ new_position = other.position - 1
112
+ other.higher_siblings.where(:position.gt => self.position).each { |s| s.inc(:position, -1) }
113
+ self.position = new_position
114
+ save!
115
+ end
116
+ end
117
+
118
+ ##
119
+ # Move this node below the specified node
120
+ #
121
+ # This method changes the node's parent if nescessary.
122
+ def move_below(other)
123
+ if position > other.position
124
+ new_position = other.position + 1
125
+ other.lower_siblings.where(:position.lt => self.position).each { |s| s.inc(:position, 1) }
126
+ self.position = new_position
127
+ save!
128
+ else
129
+ new_position = other.position
130
+ other.higher_siblings.where(:position.gt => self.position).each { |s| s.inc(:position, -1) }
131
+ other.inc(:position, -1)
132
+ self.position = new_position
133
+ save!
134
+ end
135
+ end
136
+
137
+ private
138
+
139
+ def move_lower_siblings_up
140
+ lower_siblings.each { |s| s.inc(:position, -1) }
141
+ end
142
+
143
+ def assign_default_position
144
+ return unless self.position.nil?
145
+
146
+ if self.class.all.empty? || self.class.all.collect(&:position).compact.empty?
147
+ self.position = 0
148
+ else
149
+ self.position = self.class.all.max(:position) + 1
150
+ end
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "mongoid_order"
5
+ s.version = "0.0.2"
6
+ s.platform = Gem::Platform::RUBY
7
+ s.authors = ["Ark Xu"]
8
+ s.email = ["ark.work@gmail.com"]
9
+ s.homepage = "https://github.com/arkxu/mongoid_order"
10
+ s.summary = %q{Make Mongoid documents order-able}
11
+ s.description = %q{An easy way to make Mongoid documents order-able by adding position field}
12
+
13
+ s.rubyforge_project = "mongoid_order"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+ s.add_runtime_dependency('mongoid', ['>= 2.0.0.beta.20'])
20
+ end
@@ -0,0 +1,51 @@
1
+ require 'test_helper'
2
+
3
+ class MockModel
4
+ include Mongoid::Document
5
+ include Mongoid::Orderable
6
+
7
+ field :title
8
+ end
9
+
10
+ class OrderableTest < Test::Unit::TestCase
11
+
12
+ def teardown
13
+ MockModel.destroy_all
14
+ Mongoid.database.collection("mock_models").drop
15
+ end
16
+
17
+ def test_create_first_model
18
+ mm = MockModel.create(:title => "first one")
19
+ assert_equal mm.position, 0
20
+ end
21
+
22
+ def test_create_two_models_and_move
23
+ mm = MockModel.create(:title => "first one")
24
+ assert_equal mm.position, 0
25
+ mm2 = MockModel.create(:title => "second one")
26
+ assert_equal mm2.position, 1
27
+ mm2.move_up
28
+ assert_equal mm2.position, 0
29
+ mm2.move_down
30
+ assert_equal mm2.position, 1
31
+ mm3 = MockModel.create(:title => "third one")
32
+ mm3.move_above(mm2)
33
+ assert_equal mm3.position, 1
34
+ mm3.move_below(mm2)
35
+ assert_equal mm3.position, 2
36
+ mm3.move_to_top
37
+ assert_equal mm3.position, 0
38
+ mm3.move_to_bottom
39
+ assert_equal mm3.position, 2
40
+ end
41
+
42
+ def test_destroy_one
43
+ mm = MockModel.create(:title => "first one")
44
+ assert_equal mm.position, 0
45
+ mm2 = MockModel.create(:title => "second one")
46
+ assert_equal mm2.position, 1
47
+ mm.destroy
48
+ mm2.reload
49
+ assert_equal mm2.position, 0
50
+ end
51
+ end # AssignTest
@@ -0,0 +1,12 @@
1
+ require 'rubygems' unless RUBY_VERSION =~ /^(?:1.9.*)$/
2
+ require 'test/unit'
3
+ require 'test/unit/assertions'
4
+ require 'bundler/setup'
5
+
6
+ require 'mongoid'
7
+ require 'mongoid_order'
8
+
9
+ Mongoid.configure do |config|
10
+ config.master = Mongo::Connection.new.db('mongoid_order_test')
11
+ config.allow_dynamic_fields = false
12
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_order
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Ark Xu
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-16 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: mongoid
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 62196427
30
+ segments:
31
+ - 2
32
+ - 0
33
+ - 0
34
+ - beta
35
+ - 20
36
+ version: 2.0.0.beta.20
37
+ type: :runtime
38
+ version_requirements: *id001
39
+ description: An easy way to make Mongoid documents order-able by adding position field
40
+ email:
41
+ - ark.work@gmail.com
42
+ executables: []
43
+
44
+ extensions: []
45
+
46
+ extra_rdoc_files: []
47
+
48
+ files:
49
+ - .gitignore
50
+ - Gemfile
51
+ - LICENSE
52
+ - README.rdoc
53
+ - Rakefile
54
+ - lib/mongoid_order.rb
55
+ - mongoid_order.gemspec
56
+ - test/lib/mongoid/orderable_test.rb
57
+ - test/test_helper.rb
58
+ has_rdoc: true
59
+ homepage: https://github.com/arkxu/mongoid_order
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project: mongoid_order
88
+ rubygems_version: 1.5.2
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Make Mongoid documents order-able
92
+ test_files:
93
+ - test/lib/mongoid/orderable_test.rb
94
+ - test/test_helper.rb