mongoid_orderable 5.0.0 → 5.1.0
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.
- checksums.yaml +4 -4
- data/.rubocop.yml +6 -0
- data/.rubocop_todo.yml +88 -0
- data/.travis.yml +26 -7
- data/CHANGELOG.md +45 -14
- data/CONTRIBUTING.md +118 -0
- data/Dangerfile +1 -0
- data/Gemfile +8 -4
- data/LICENSE.txt +20 -0
- data/README.md +6 -4
- data/RELEASING.md +68 -0
- data/Rakefile +19 -4
- data/lib/mongoid/orderable/callbacks.rb +26 -22
- data/lib/mongoid/orderable/configuration.rb +11 -13
- data/lib/mongoid/orderable/errors/invalid_target_position.rb +4 -7
- data/lib/mongoid/orderable/errors/mongoid_orderable_error.rb +0 -8
- data/lib/mongoid/orderable/generator/helpers.rb +2 -4
- data/lib/mongoid/orderable/generator/listable.rb +0 -2
- data/lib/mongoid/orderable/generator/movable.rb +8 -10
- data/lib/mongoid/orderable/generator/position.rb +0 -2
- data/lib/mongoid/orderable/generator/scope.rb +3 -3
- data/lib/mongoid/orderable/generator.rb +1 -2
- data/lib/mongoid/orderable/helpers.rb +25 -26
- data/lib/mongoid/orderable/listable.rb +13 -15
- data/lib/mongoid/orderable/movable.rb +6 -8
- data/lib/mongoid/orderable/orderable_class.rb +3 -7
- data/lib/mongoid/orderable.rb +1 -3
- data/lib/mongoid_orderable/version.rb +1 -1
- data/lib/mongoid_orderable.rb +6 -12
- data/mongoid_orderable.gemspec +15 -15
- data/spec/mongoid/orderable_spec.rb +279 -331
- data/spec/spec_helper.rb +5 -16
- metadata +11 -8
- data/lib/mongoid_orderable/mongoid/contexts/enumerable.rb +0 -15
- data/lib/mongoid_orderable/mongoid/contexts/mongo.rb +0 -18
- data/lib/mongoid_orderable/mongoid/criteria.rb +0 -4
@@ -1,17 +1,14 @@
|
|
1
1
|
module Mongoid::Orderable
|
2
2
|
module Errors
|
3
3
|
class InvalidTargetPosition < Mongoid::Orderable::Errors::MongoidOrderableError
|
4
|
-
def initialize
|
4
|
+
def initialize(value)
|
5
5
|
super _compose_message(value)
|
6
6
|
end
|
7
7
|
|
8
8
|
private
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
else
|
13
|
-
compose_message 'invalid_target_position', { :value => value.inspect }
|
14
|
-
end
|
9
|
+
|
10
|
+
def _compose_message(value)
|
11
|
+
compose_message 'invalid_target_position', value: value.inspect
|
15
12
|
end
|
16
13
|
end
|
17
14
|
end
|
@@ -1,14 +1,6 @@
|
|
1
1
|
module Mongoid::Orderable
|
2
2
|
module Errors
|
3
3
|
class MongoidOrderableError < ::Mongoid::Errors::MongoidError
|
4
|
-
|
5
|
-
if ::Mongoid::Compatibility::Version.mongoid2?
|
6
|
-
def translate key, options
|
7
|
-
[:message, :summary, :resolution].map do |section|
|
8
|
-
::I18n.translate "#{BASE_KEY}.#{key}.#{section}", options
|
9
|
-
end.join ' '
|
10
|
-
end
|
11
|
-
end
|
12
4
|
end
|
13
5
|
end
|
14
6
|
end
|
@@ -2,7 +2,6 @@ module Mongoid
|
|
2
2
|
module Orderable
|
3
3
|
module Generator
|
4
4
|
module Helpers
|
5
|
-
|
6
5
|
def generate_orderable_helpers
|
7
6
|
self_class = klass
|
8
7
|
|
@@ -19,11 +18,10 @@ module Mongoid
|
|
19
18
|
eos
|
20
19
|
|
21
20
|
generate_method(:orderable_inherited_class) do
|
22
|
-
self_class.orderable_configurations.any?{ |
|
21
|
+
self_class.orderable_configurations.any? { |_col, conf| conf[:inherited] } ? self_class : self.class
|
23
22
|
end
|
24
23
|
end
|
25
|
-
|
26
24
|
end
|
27
25
|
end
|
28
26
|
end
|
29
|
-
end
|
27
|
+
end
|
@@ -2,7 +2,6 @@ module Mongoid
|
|
2
2
|
module Orderable
|
3
3
|
module Generator
|
4
4
|
module Listable
|
5
|
-
|
6
5
|
def generate_listable_helpers(column_name)
|
7
6
|
generate_list_helpers(column_name)
|
8
7
|
generate_aliased_helpers(column_name)
|
@@ -34,7 +33,6 @@ module Mongoid
|
|
34
33
|
alias_method "prev_#{column_name}_item", "previous_#{column_name}_item"
|
35
34
|
end
|
36
35
|
end
|
37
|
-
|
38
36
|
end
|
39
37
|
end
|
40
38
|
end
|
@@ -2,7 +2,6 @@ module Mongoid
|
|
2
2
|
module Orderable
|
3
3
|
module Generator
|
4
4
|
module Movable
|
5
|
-
|
6
5
|
def generate_movable_helpers(column_name)
|
7
6
|
generate_move_to_helpers(column_name)
|
8
7
|
generate_insert_at_helpers(column_name)
|
@@ -13,16 +12,16 @@ module Mongoid
|
|
13
12
|
|
14
13
|
def generate_move_to_helpers(column_name)
|
15
14
|
generate_method("move_#{column_name}_to") do |target_position|
|
16
|
-
move_column_to target_position, :
|
15
|
+
move_column_to target_position, column: column_name
|
17
16
|
end
|
18
17
|
|
19
18
|
generate_method("move_#{column_name}_to!") do |target_position|
|
20
|
-
move_column_to target_position, :
|
19
|
+
move_column_to target_position, column: column_name
|
21
20
|
save
|
22
21
|
end
|
23
22
|
|
24
23
|
generate_method("move_#{column_name}_to=") do |target_position|
|
25
|
-
move_column_to target_position, :
|
24
|
+
move_column_to target_position, column: column_name
|
26
25
|
end
|
27
26
|
end
|
28
27
|
|
@@ -37,26 +36,25 @@ module Mongoid
|
|
37
36
|
def generate_shorthand_helpers(column_name)
|
38
37
|
[:top, :bottom].each do |symbol|
|
39
38
|
generate_method "move_#{column_name}_to_#{symbol}" do
|
40
|
-
move_to symbol, :
|
39
|
+
move_to symbol, column: column_name
|
41
40
|
end
|
42
41
|
|
43
42
|
generate_method "move_#{column_name}_to_#{symbol}!" do
|
44
|
-
move_to! symbol, :
|
43
|
+
move_to! symbol, column: column_name
|
45
44
|
end
|
46
45
|
end
|
47
46
|
|
48
47
|
[:higher, :lower].each do |symbol|
|
49
48
|
generate_method "move_#{column_name}_#{symbol}" do
|
50
|
-
move_to symbol, :
|
49
|
+
move_to symbol, column: column_name
|
51
50
|
end
|
52
51
|
|
53
52
|
generate_method "move_#{column_name}_#{symbol}!" do
|
54
|
-
move_to! symbol, :
|
53
|
+
move_to! symbol, column: column_name
|
55
54
|
end
|
56
55
|
end
|
57
56
|
end
|
58
|
-
|
59
57
|
end
|
60
58
|
end
|
61
59
|
end
|
62
|
-
end
|
60
|
+
end
|
@@ -2,7 +2,6 @@ module Mongoid
|
|
2
2
|
module Orderable
|
3
3
|
module Generator
|
4
4
|
module Position
|
5
|
-
|
6
5
|
def generate_position_helpers(column_name)
|
7
6
|
klass.class_eval <<-eos
|
8
7
|
def orderable_position(column = nil)
|
@@ -19,7 +18,6 @@ module Mongoid
|
|
19
18
|
send "#{column_name}=", value
|
20
19
|
end
|
21
20
|
end
|
22
|
-
|
23
21
|
end
|
24
22
|
end
|
25
23
|
end
|
@@ -5,13 +5,13 @@ module Mongoid
|
|
5
5
|
def generate_scope_helpers(column_name, order_scope)
|
6
6
|
klass.class_eval do
|
7
7
|
scope "orderable_#{column_name}_scope", case order_scope
|
8
|
-
when Symbol then
|
8
|
+
when Symbol then ->(document) { where(order_scope => document.send(order_scope)) }
|
9
9
|
when Proc then order_scope
|
10
|
-
else
|
10
|
+
else ->(_document) { where({}) }
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
17
|
-
end
|
17
|
+
end
|
@@ -1,50 +1,49 @@
|
|
1
1
|
module Mongoid
|
2
2
|
module Orderable
|
3
3
|
module Helpers
|
4
|
-
|
5
4
|
def orderable_keys
|
6
5
|
Array orderable_inherited_class.orderable_configurations.try(:keys)
|
7
6
|
end
|
8
7
|
|
9
8
|
def default_orderable_column
|
10
|
-
self.class.orderable_configurations.detect{ |
|
9
|
+
self.class.orderable_configurations.detect { |_c, conf| conf[:default] }.try(:first) || orderable_keys.first
|
11
10
|
end
|
12
11
|
|
13
12
|
private
|
14
13
|
|
15
|
-
|
16
|
-
|
14
|
+
def orderable_scoped(column = nil)
|
15
|
+
column ||= default_orderable_column
|
17
16
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
17
|
+
if embedded?
|
18
|
+
_parent.send(MongoidOrderable.metadata(self).name).send("orderable_#{column}_scope", self)
|
19
|
+
else
|
20
|
+
orderable_inherited_class.send("orderable_#{column}_scope", self)
|
23
21
|
end
|
22
|
+
end
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
orderable_scope_changed_query(column)
|
29
|
-
end
|
30
|
-
else
|
24
|
+
def orderable_scope_changed?(column)
|
25
|
+
if Mongoid.respond_to?(:unit_of_work)
|
26
|
+
Mongoid.unit_of_work do
|
31
27
|
orderable_scope_changed_query(column)
|
32
28
|
end
|
29
|
+
else
|
30
|
+
orderable_scope_changed_query(column)
|
33
31
|
end
|
32
|
+
end
|
34
33
|
|
35
|
-
|
36
|
-
|
37
|
-
|
34
|
+
def orderable_scope_changed_query(column)
|
35
|
+
!orderable_scoped(column).where(_id: _id).exists?
|
36
|
+
end
|
38
37
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
end
|
38
|
+
def bottom_orderable_position(column = nil)
|
39
|
+
column ||= default_orderable_column
|
40
|
+
@bottom_orderable_position = begin
|
41
|
+
positions_list = orderable_scoped(column).distinct(orderable_column(column)).compact
|
42
|
+
return orderable_base(column) if positions_list.empty?
|
43
|
+
max = positions_list.map(&:to_i).max.to_i
|
44
|
+
in_list?(column) ? max : max.next
|
47
45
|
end
|
46
|
+
end
|
48
47
|
end
|
49
48
|
end
|
50
49
|
end
|
@@ -1,49 +1,47 @@
|
|
1
1
|
module Mongoid
|
2
2
|
module Orderable
|
3
3
|
module Listable
|
4
|
-
|
5
4
|
##
|
6
5
|
# Returns items above the current document.
|
7
6
|
# Items with a position lower than this document's position.
|
8
|
-
def previous_items(column=nil)
|
9
|
-
column
|
7
|
+
def previous_items(column = nil)
|
8
|
+
column ||= default_orderable_column
|
10
9
|
orderable_scoped(column).where(orderable_column(column).lt => send(column))
|
11
10
|
end
|
12
|
-
|
11
|
+
alias prev_items previous_items
|
13
12
|
|
14
13
|
##
|
15
14
|
# Returns items below the current document.
|
16
15
|
# Items with a position greater than this document's position.
|
17
|
-
def next_items(column=nil)
|
18
|
-
column
|
16
|
+
def next_items(column = nil)
|
17
|
+
column ||= default_orderable_column
|
19
18
|
orderable_scoped(column).where(orderable_column(column).gt => send(column))
|
20
19
|
end
|
21
20
|
|
22
21
|
# returns the previous item in the list
|
23
|
-
def previous_item(column=nil)
|
24
|
-
column
|
22
|
+
def previous_item(column = nil)
|
23
|
+
column ||= default_orderable_column
|
25
24
|
orderable_scoped(column).where(orderable_column(column) => send(column) - 1).first
|
26
25
|
end
|
27
|
-
|
26
|
+
alias prev_item previous_item
|
28
27
|
|
29
28
|
# returns the next item in the list
|
30
|
-
def next_item(column=nil)
|
31
|
-
column
|
29
|
+
def next_item(column = nil)
|
30
|
+
column ||= default_orderable_column
|
32
31
|
orderable_scoped(column).where(orderable_column(column) => send(column) + 1).first
|
33
32
|
end
|
34
33
|
|
35
|
-
def first?(column=nil)
|
34
|
+
def first?(column = nil)
|
36
35
|
in_list?(column) && orderable_position(column) == orderable_base(column)
|
37
36
|
end
|
38
37
|
|
39
|
-
def last?(column=nil)
|
38
|
+
def last?(column = nil)
|
40
39
|
in_list?(column) && orderable_position(column) == bottom_orderable_position(column)
|
41
40
|
end
|
42
41
|
|
43
|
-
def in_list?(column=nil)
|
42
|
+
def in_list?(column = nil)
|
44
43
|
!orderable_position(column).nil?
|
45
44
|
end
|
46
|
-
|
47
45
|
end
|
48
46
|
end
|
49
47
|
end
|
@@ -1,22 +1,21 @@
|
|
1
1
|
module Mongoid
|
2
2
|
module Orderable
|
3
3
|
module Movable
|
4
|
-
|
5
|
-
def move_to!(target_position, options={})
|
4
|
+
def move_to!(target_position, options = {})
|
6
5
|
move_column_to target_position, options
|
7
6
|
save
|
8
7
|
end
|
9
|
-
|
8
|
+
alias insert_at! move_to!
|
10
9
|
|
11
|
-
def move_to(target_position, options={})
|
10
|
+
def move_to(target_position, options = {})
|
12
11
|
move_column_to target_position, options
|
13
12
|
end
|
14
|
-
|
13
|
+
alias insert_at move_to
|
15
14
|
|
16
|
-
def move_to=(target_position, options={})
|
15
|
+
def move_to=(target_position, options = {})
|
17
16
|
move_column_to target_position, options
|
18
17
|
end
|
19
|
-
|
18
|
+
alias insert_at= move_to=
|
20
19
|
|
21
20
|
[:top, :bottom].each do |symbol|
|
22
21
|
class_eval <<-eos
|
@@ -52,7 +51,6 @@ module Mongoid
|
|
52
51
|
column = options[:column] || default_orderable_column
|
53
52
|
@move_all = move_all.merge(column => position)
|
54
53
|
end
|
55
|
-
|
56
54
|
end
|
57
55
|
end
|
58
56
|
end
|
@@ -5,7 +5,7 @@ module Mongoid
|
|
5
5
|
|
6
6
|
attr_reader :klass, :configuration
|
7
7
|
|
8
|
-
def initialize
|
8
|
+
def initialize(klass, configuration)
|
9
9
|
@klass = klass
|
10
10
|
@configuration = configuration
|
11
11
|
end
|
@@ -18,7 +18,7 @@ module Mongoid
|
|
18
18
|
add_callbacks
|
19
19
|
end
|
20
20
|
|
21
|
-
def self.setup(klass, configuration={})
|
21
|
+
def self.setup(klass, configuration = {})
|
22
22
|
new(klass, configuration).setup
|
23
23
|
end
|
24
24
|
|
@@ -31,11 +31,7 @@ module Mongoid
|
|
31
31
|
def add_db_index
|
32
32
|
spec = [[configuration[:column], 1]]
|
33
33
|
spec.unshift([configuration[:scope], 1]) if configuration[:scope].is_a?(Symbol)
|
34
|
-
|
35
|
-
klass.index(spec)
|
36
|
-
else
|
37
|
-
klass.index(Hash[spec])
|
38
|
-
end
|
34
|
+
klass.index(Hash[spec])
|
39
35
|
end
|
40
36
|
|
41
37
|
def save_configuration
|
data/lib/mongoid/orderable.rb
CHANGED
@@ -11,12 +11,10 @@ module Mongoid::Orderable
|
|
11
11
|
end
|
12
12
|
|
13
13
|
module ClassMethods
|
14
|
-
|
15
|
-
def orderable options = {}
|
14
|
+
def orderable(options = {})
|
16
15
|
configuration = Mongoid::Orderable::Configuration.build(self, options)
|
17
16
|
|
18
17
|
Mongoid::Orderable::OrderableClass.setup(self, configuration)
|
19
18
|
end
|
20
|
-
|
21
19
|
end
|
22
20
|
end
|
data/lib/mongoid_orderable.rb
CHANGED
@@ -7,20 +7,20 @@ require 'mongoid'
|
|
7
7
|
require 'mongoid/compatibility'
|
8
8
|
|
9
9
|
module MongoidOrderable
|
10
|
-
if ::Mongoid::Compatibility::Version.
|
11
|
-
def self.inc
|
10
|
+
if ::Mongoid::Compatibility::Version.mongoid3?
|
11
|
+
def self.inc(instance, attribute, value)
|
12
12
|
instance.inc attribute, value
|
13
13
|
end
|
14
14
|
|
15
|
-
def self.metadata
|
15
|
+
def self.metadata(instance)
|
16
16
|
instance.metadata
|
17
17
|
end
|
18
18
|
else
|
19
|
-
def self.inc
|
19
|
+
def self.inc(instance, attribute, value)
|
20
20
|
instance.inc(attribute => value)
|
21
21
|
end
|
22
22
|
|
23
|
-
def self.metadata
|
23
|
+
def self.metadata(instance)
|
24
24
|
instance.relation_metadata
|
25
25
|
end
|
26
26
|
end
|
@@ -28,13 +28,7 @@ end
|
|
28
28
|
|
29
29
|
require 'mongoid_orderable/version'
|
30
30
|
|
31
|
-
|
32
|
-
require 'mongoid_orderable/mongoid/contexts/mongo'
|
33
|
-
require 'mongoid_orderable/mongoid/contexts/enumerable'
|
34
|
-
require 'mongoid_orderable/mongoid/criteria'
|
35
|
-
else
|
36
|
-
require 'mongoid_orderable/mongoid/contextual/memory'
|
37
|
-
end
|
31
|
+
require 'mongoid_orderable/mongoid/contextual/memory'
|
38
32
|
|
39
33
|
require 'mongoid/orderable'
|
40
34
|
require 'mongoid/orderable/errors'
|
data/mongoid_orderable.gemspec
CHANGED
@@ -1,26 +1,26 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
require
|
2
|
+
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'mongoid_orderable/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
|
-
s.name =
|
6
|
+
s.name = 'mongoid_orderable'
|
7
7
|
s.version = MongoidOrderable::VERSION
|
8
|
-
s.authors = [
|
9
|
-
s.email = [
|
10
|
-
s.homepage =
|
11
|
-
s.summary =
|
12
|
-
s.description =
|
8
|
+
s.authors = ['pyromaniac']
|
9
|
+
s.email = ['kinwizard@gmail.com']
|
10
|
+
s.homepage = ''
|
11
|
+
s.summary = 'Acts as list mongoid implementation'
|
12
|
+
s.description = 'Gem allows mongoid model behave as orderable list'
|
13
13
|
|
14
|
-
s.rubyforge_project =
|
14
|
+
s.rubyforge_project = 'mongoid_orderable'
|
15
15
|
|
16
16
|
s.files = `git ls-files`.split("\n")
|
17
17
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
-
s.require_paths = [
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
19
|
+
s.require_paths = ['lib']
|
20
20
|
|
21
21
|
# specify any dependencies here; for example:
|
22
|
-
s.add_development_dependency
|
23
|
-
s.add_development_dependency
|
24
|
-
s.add_runtime_dependency
|
25
|
-
s.add_runtime_dependency
|
22
|
+
s.add_development_dependency 'rake'
|
23
|
+
s.add_development_dependency 'rspec'
|
24
|
+
s.add_runtime_dependency 'mongoid', '>= 3.0.0'
|
25
|
+
s.add_runtime_dependency 'mongoid-compatibility'
|
26
26
|
end
|