mongoid_orderable 5.2.0 → 6.0.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.
Files changed (56) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +73 -58
  3. data/LICENSE.txt +20 -20
  4. data/README.md +256 -150
  5. data/Rakefile +24 -21
  6. data/lib/config/locales/en.yml +12 -9
  7. data/lib/mongoid/orderable.rb +29 -20
  8. data/lib/mongoid/orderable/configs/field_config.rb +79 -0
  9. data/lib/mongoid/orderable/configs/global_config.rb +26 -0
  10. data/lib/mongoid/orderable/engine.rb +204 -0
  11. data/lib/mongoid/orderable/errors/invalid_target_position.rb +19 -15
  12. data/lib/mongoid/orderable/errors/transaction_failed.rb +20 -0
  13. data/lib/mongoid/orderable/generators/base.rb +21 -0
  14. data/lib/mongoid/orderable/generators/helpers.rb +29 -0
  15. data/lib/mongoid/orderable/generators/listable.rb +41 -0
  16. data/lib/mongoid/orderable/generators/lock_collection.rb +37 -0
  17. data/lib/mongoid/orderable/generators/movable.rb +62 -0
  18. data/lib/mongoid/orderable/generators/position.rb +26 -0
  19. data/lib/mongoid/orderable/generators/scope.rb +26 -0
  20. data/lib/mongoid/orderable/installer.rb +63 -0
  21. data/lib/mongoid/orderable/mixins/callbacks.rb +29 -0
  22. data/lib/mongoid/orderable/mixins/helpers.rb +39 -0
  23. data/lib/mongoid/orderable/mixins/listable.rb +49 -0
  24. data/lib/mongoid/orderable/mixins/movable.rb +60 -0
  25. data/lib/mongoid/orderable/version.rb +7 -0
  26. data/lib/mongoid_orderable.rb +29 -56
  27. data/spec/mongoid/orderable_spec.rb +1486 -1380
  28. data/spec/spec_helper.rb +21 -21
  29. metadata +44 -41
  30. data/.gitignore +0 -4
  31. data/.rspec +0 -2
  32. data/.rubocop.yml +0 -6
  33. data/.rubocop_todo.yml +0 -88
  34. data/.rvmrc +0 -1
  35. data/.travis.yml +0 -48
  36. data/CONTRIBUTING.md +0 -118
  37. data/Dangerfile +0 -1
  38. data/Gemfile +0 -26
  39. data/RELEASING.md +0 -68
  40. data/lib/mongoid/orderable/callbacks.rb +0 -79
  41. data/lib/mongoid/orderable/configuration.rb +0 -58
  42. data/lib/mongoid/orderable/errors.rb +0 -2
  43. data/lib/mongoid/orderable/errors/mongoid_orderable_error.rb +0 -6
  44. data/lib/mongoid/orderable/generator.rb +0 -33
  45. data/lib/mongoid/orderable/generator/helpers.rb +0 -27
  46. data/lib/mongoid/orderable/generator/listable.rb +0 -39
  47. data/lib/mongoid/orderable/generator/movable.rb +0 -60
  48. data/lib/mongoid/orderable/generator/position.rb +0 -24
  49. data/lib/mongoid/orderable/generator/scope.rb +0 -17
  50. data/lib/mongoid/orderable/helpers.rb +0 -49
  51. data/lib/mongoid/orderable/listable.rb +0 -47
  52. data/lib/mongoid/orderable/movable.rb +0 -56
  53. data/lib/mongoid/orderable/orderable_class.rb +0 -47
  54. data/lib/mongoid_orderable/mongoid/contextual/memory.rb +0 -15
  55. data/lib/mongoid_orderable/version.rb +0 -3
  56. data/mongoid_orderable.gemspec +0 -26
@@ -1,49 +0,0 @@
1
- module Mongoid
2
- module Orderable
3
- module Helpers
4
- def orderable_keys
5
- Array orderable_inherited_class.orderable_configurations.try(:keys)
6
- end
7
-
8
- def default_orderable_column
9
- self.class.orderable_configurations.detect { |_c, conf| conf[:default] }.try(:first) || orderable_keys.first
10
- end
11
-
12
- private
13
-
14
- def orderable_scoped(column = nil)
15
- column ||= default_orderable_column
16
-
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)
21
- end
22
- end
23
-
24
- def orderable_scope_changed?(column)
25
- if Mongoid.respond_to?(:unit_of_work)
26
- Mongoid.unit_of_work do
27
- orderable_scope_changed_query(column)
28
- end
29
- else
30
- orderable_scope_changed_query(column)
31
- end
32
- end
33
-
34
- def orderable_scope_changed_query(column)
35
- !orderable_scoped(column).where(_id: _id).exists?
36
- end
37
-
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
45
- end
46
- end
47
- end
48
- end
49
- end
@@ -1,47 +0,0 @@
1
- module Mongoid
2
- module Orderable
3
- module Listable
4
- ##
5
- # Returns items above the current document.
6
- # Items with a position lower than this document's position.
7
- def previous_items(column = nil)
8
- column ||= default_orderable_column
9
- orderable_scoped(column).where(orderable_column(column).lt => send(column))
10
- end
11
- alias prev_items previous_items
12
-
13
- ##
14
- # Returns items below the current document.
15
- # Items with a position greater than this document's position.
16
- def next_items(column = nil)
17
- column ||= default_orderable_column
18
- orderable_scoped(column).where(orderable_column(column).gt => send(column))
19
- end
20
-
21
- # returns the previous item in the list
22
- def previous_item(column = nil)
23
- column ||= default_orderable_column
24
- orderable_scoped(column).where(orderable_column(column) => send(column) - 1).first
25
- end
26
- alias prev_item previous_item
27
-
28
- # returns the next item in the list
29
- def next_item(column = nil)
30
- column ||= default_orderable_column
31
- orderable_scoped(column).where(orderable_column(column) => send(column) + 1).first
32
- end
33
-
34
- def first?(column = nil)
35
- in_list?(column) && orderable_position(column) == orderable_base(column)
36
- end
37
-
38
- def last?(column = nil)
39
- in_list?(column) && orderable_position(column) == bottom_orderable_position(column)
40
- end
41
-
42
- def in_list?(column = nil)
43
- !orderable_position(column).nil?
44
- end
45
- end
46
- end
47
- end
@@ -1,56 +0,0 @@
1
- module Mongoid
2
- module Orderable
3
- module Movable
4
- def move_to!(target_position, options = {})
5
- move_column_to target_position, options
6
- save
7
- end
8
- alias insert_at! move_to!
9
-
10
- def move_to(target_position, options = {})
11
- move_column_to target_position, options
12
- end
13
- alias insert_at move_to
14
-
15
- def move_to=(target_position, options = {})
16
- move_column_to target_position, options
17
- end
18
- alias insert_at= move_to=
19
-
20
- [:top, :bottom].each do |symbol|
21
- class_eval <<-eos
22
- def move_to_#{symbol}(options = {})
23
- move_to :#{symbol}, options
24
- end
25
-
26
- def move_to_#{symbol}!(options = {})
27
- move_to! :#{symbol}, options
28
- end
29
- eos
30
- end
31
-
32
- [:higher, :lower].each do |symbol|
33
- class_eval <<-eos
34
- def move_#{symbol}(options = {})
35
- move_to :#{symbol}, options
36
- end
37
-
38
- def move_#{symbol}!(options = {})
39
- move_to! :#{symbol}, options
40
- end
41
- eos
42
- end
43
-
44
- protected
45
-
46
- def move_all
47
- @move_all || {}
48
- end
49
-
50
- def move_column_to(position, options)
51
- column = options[:column] || default_orderable_column
52
- @move_all = move_all.merge(column => position)
53
- end
54
- end
55
- end
56
- end
@@ -1,47 +0,0 @@
1
- module Mongoid
2
- module Orderable
3
- class OrderableClass
4
- include Mongoid::Orderable::Generator
5
-
6
- attr_reader :klass, :configuration
7
-
8
- def initialize(klass, configuration)
9
- @klass = klass
10
- @configuration = configuration
11
- end
12
-
13
- def setup
14
- add_db_field
15
- add_db_index if configuration[:index]
16
- save_configuration
17
- generate_all_helpers
18
- add_callbacks
19
- end
20
-
21
- def self.setup(klass, configuration = {})
22
- new(klass, configuration).setup
23
- end
24
-
25
- protected
26
-
27
- def add_db_field
28
- klass.field configuration[:column], configuration[:field_opts]
29
- end
30
-
31
- def add_db_index
32
- spec = [[configuration[:column], 1]]
33
- spec.unshift([configuration[:scope], 1]) if configuration[:scope].is_a?(Symbol)
34
- klass.index(Hash[spec])
35
- end
36
-
37
- def save_configuration
38
- klass.orderable_configurations ||= {}
39
- klass.orderable_configurations = klass.orderable_configurations.merge(column_name => configuration)
40
- end
41
-
42
- def add_callbacks
43
- klass.add_orderable_callbacks
44
- end
45
- end
46
- end
47
- end
@@ -1,15 +0,0 @@
1
- module MongoidOrderable #:nodoc:
2
- module Mongoid #:nodoc:
3
- module Contextual #:nodoc:
4
- module Memory #:nodoc:
5
- def inc(* args)
6
- each do |document|
7
- document.inc *args
8
- end
9
- end
10
- end
11
- end
12
- end
13
- end
14
-
15
- Mongoid::Contextual::Memory.send :include, MongoidOrderable::Mongoid::Contextual::Memory
@@ -1,3 +0,0 @@
1
- module MongoidOrderable
2
- VERSION = '5.2.0'.freeze
3
- end
@@ -1,26 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- $LOAD_PATH.push File.expand_path('../lib', __FILE__)
3
- require 'mongoid_orderable/version'
4
-
5
- Gem::Specification.new do |s|
6
- s.name = 'mongoid_orderable'
7
- s.version = MongoidOrderable::VERSION
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
-
14
- s.rubyforge_project = 'mongoid_orderable'
15
-
16
- s.files = `git ls-files`.split("\n")
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 = ['lib']
20
-
21
- # specify any dependencies here; for example:
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
- end