mongoid_orderable 5.2.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
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
data/Dangerfile DELETED
@@ -1 +0,0 @@
1
- danger.import_dangerfile(gem: 'mongoid-danger')
data/Gemfile DELETED
@@ -1,26 +0,0 @@
1
- source 'http://rubygems.org'
2
-
3
- # Specify your gem's dependencies in mongoid_orderable.gemspec
4
- gemspec
5
-
6
- case version = ENV['MONGOID_VERSION'] || '7'
7
- when 'HEAD'
8
- gem 'mongoid', github: 'mongodb/mongoid'
9
- when /^7/
10
- gem 'mongoid', '~> 7.0'
11
- when /^6/
12
- gem 'mongoid', '~> 6.0'
13
- when /^5/
14
- gem 'mongoid', '~> 5.0'
15
- when /^4/
16
- gem 'mongoid', '~> 4.0'
17
- when /^3/
18
- gem 'mongoid', '~> 3.1'
19
- else
20
- gem 'mongoid', version
21
- end
22
-
23
- group :test do
24
- gem 'rubocop', '0.45.0'
25
- gem 'mongoid-danger', '~> 0.1.0', require: false
26
- end
@@ -1,68 +0,0 @@
1
- Releasing Mongoid::Orderable
2
- ============================
3
-
4
- There're no particular rules about when to release mongoid_orderable. Release bug fixes frequently, features not so frequently and breaking API changes rarely.
5
-
6
- ### Release
7
-
8
- Run tests, check that all tests succeed locally.
9
-
10
- ```
11
- bundle install
12
- rake
13
- ```
14
-
15
- Check that the last build succeeded in [Travis CI](https://travis-ci.org/mongoid/mongoid_orderable) for all supported platforms.
16
-
17
- Increment the version, modify [lib/mongoid_orderable/version.rb](lib/mongoid_orderable/version.rb).
18
-
19
- * Increment the third number if the release has bug fixes and/or very minor features, only (eg. change `0.5.1` to `0.5.2`).
20
- * Increment the second number if the release contains major features or breaking API changes (eg. change `0.5.1` to `0.4.0`).
21
-
22
- Change "Next Release" in [CHANGELOG.md](CHANGELOG.md) to the new version.
23
-
24
- ```
25
- ### 0.4.0 (2014-01-27)
26
- ```
27
-
28
- Remove the line with "Your contribution here.", since there will be no more contributions to this release.
29
-
30
- Commit your changes.
31
-
32
- ```
33
- git add CHANGELOG.md lib/mongoid_orderable/version.rb
34
- git commit -m "Preparing for release, 0.4.0."
35
- git push origin master
36
- ```
37
-
38
- Release.
39
-
40
- ```
41
- $ rake release
42
-
43
- mongoid_orderable 0.4.0 built to pkg/mongoid_orderable-0.4.0.gem.
44
- Tagged v0.4.0.
45
- Pushed git commits and tags.
46
- Pushed mongoid_orderable 0.4.0 to rubygems.org.
47
- ```
48
-
49
- ### Prepare for the Next Version
50
-
51
- Add the next release to [CHANGELOG.md](CHANGELOG.md).
52
-
53
- ```
54
- Next Release
55
- ============
56
-
57
- * Your contribution here.
58
- ```
59
-
60
- Increment the minor version, modify [lib/mongoid_orderable/version.rb](lib/mongoid_orderable/version.rb).
61
-
62
- Commit your changes.
63
-
64
- ```
65
- git add CHANGELOG.md lib/mongoid_orderable/version.rb
66
- git commit -m "Preparing for next release, 0.4.1."
67
- git push origin master
68
- ```
@@ -1,79 +0,0 @@
1
- module Mongoid
2
- module Orderable
3
- module Callbacks
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
- protected
8
-
9
- def add_to_list
10
- orderable_keys.each do |column|
11
- apply_position column, move_all[column]
12
- end
13
- end
14
-
15
- def remove_from_list
16
- orderable_keys.each do |column|
17
- remove_position_from_list column
18
- end
19
- end
20
-
21
- def remove_position_from_list(column)
22
- col = orderable_column(column)
23
- pos = orderable_position(column)
24
- MongoidOrderable.inc orderable_scoped(column).where(col.gt => pos), col, -1
25
- end
26
-
27
- def apply_position(column, target_position)
28
- if persisted? && !embedded? && orderable_scope_changed?(column)
29
- self.class.unscoped.find(_id).remove_position_from_list(column)
30
- send("orderable_#{column}_position=", nil)
31
- end
32
-
33
- return if !target_position && in_list?(column)
34
-
35
- target_position = target_position_to_position column, target_position
36
- scope = orderable_scoped(column)
37
- col = orderable_column(column)
38
- pos = orderable_position(column)
39
-
40
- if !in_list?(column)
41
- MongoidOrderable.inc scope.where(col.gte => target_position), col, 1
42
- elsif target_position < pos
43
- MongoidOrderable.inc(scope.where(col.gte => target_position, col.lt => pos), col, 1)
44
- elsif target_position > pos
45
- MongoidOrderable.inc(scope.where(col.gt => pos, col.lte => target_position), col, -1)
46
- end
47
-
48
- send("orderable_#{column}_position=", target_position)
49
- end
50
-
51
- def target_position_to_position(column, target_position)
52
- target_position = :bottom unless target_position
53
-
54
- unless target_position.is_a? Numeric
55
- target_position = case target_position.to_s
56
- when 'top' then orderable_base(column)
57
- when 'bottom' then bottom_orderable_position(column)
58
- when 'higher' then orderable_position(column).pred
59
- when 'lower' then orderable_position(column).next
60
- when /\A\d+\Z/ then target_position.to_i
61
- else raise Mongoid::Orderable::Errors::InvalidTargetPosition, target_position
62
- end
63
- end
64
-
65
- target_position = orderable_base(column) if target_position < orderable_base(column)
66
- target_position = bottom_orderable_position(column) if target_position > bottom_orderable_position(column)
67
- target_position
68
- end
69
- end
70
-
71
- module ClassMethods
72
- def add_orderable_callbacks
73
- before_save :add_to_list
74
- after_destroy :remove_from_list
75
- end
76
- end
77
- end
78
- end
79
- end
@@ -1,58 +0,0 @@
1
- module Mongoid
2
- module Orderable
3
- class Configuration
4
- CONFIG_OPTIONS = %w(column scope foreign_key inherited base index default).map(&:to_sym)
5
- FIELD_OPTIONS = %w(as).map(&:to_sym)
6
- VALID_OPTIONS = CONFIG_OPTIONS + FIELD_OPTIONS
7
-
8
- attr_reader :orderable_class, :options
9
-
10
- def initialize(parent, options = {})
11
- @orderable_class = parent
12
- set_options(options)
13
- set_field_options
14
- set_orderable_scope
15
- end
16
-
17
- def default_configuration
18
- { column: :position,
19
- index: true,
20
- scope: nil,
21
- base: 1,
22
- field_opts: { type: Integer } }
23
- end
24
-
25
- def self.build(parent, options = {})
26
- new(parent, options).options
27
- end
28
-
29
- protected
30
-
31
- def set_options(options)
32
- @options = default_configuration
33
- return unless options.is_a? Hash
34
- @options.merge! options.symbolize_keys.slice(*VALID_OPTIONS)
35
- end
36
-
37
- def set_field_options
38
- FIELD_OPTIONS.each do |key|
39
- next unless options.key? key
40
- @options[:field_opts][key] = options.delete(key)
41
- end
42
- end
43
-
44
- def set_orderable_scope
45
- if options[:scope].is_a?(Symbol) && options[:scope].to_s !~ /_id$/
46
- scope_relation = @orderable_class.relations[options[:scope].to_s]
47
- @options[:scope] = if scope_relation
48
- scope_relation.key.to_sym
49
- else
50
- "#{options[:scope]}_id".to_sym
51
- end
52
- elsif options[:scope].is_a?(String)
53
- @options[:scope] = options[:scope].to_sym
54
- end
55
- end
56
- end
57
- end
58
- end
@@ -1,2 +0,0 @@
1
- require 'mongoid/orderable/errors/mongoid_orderable_error'
2
- require 'mongoid/orderable/errors/invalid_target_position'
@@ -1,6 +0,0 @@
1
- module Mongoid::Orderable
2
- module Errors
3
- class MongoidOrderableError < ::Mongoid::Errors::MongoidError
4
- end
5
- end
6
- end
@@ -1,33 +0,0 @@
1
- module Mongoid
2
- module Orderable
3
- module Generator
4
- include Mongoid::Orderable::Generator::Scope
5
- include Mongoid::Orderable::Generator::Position
6
- include Mongoid::Orderable::Generator::Movable
7
- include Mongoid::Orderable::Generator::Listable
8
- include Mongoid::Orderable::Generator::Helpers
9
-
10
- def column_name
11
- configuration[:field_opts][:as] || configuration[:column]
12
- end
13
-
14
- def order_scope
15
- configuration[:scope]
16
- end
17
-
18
- def generate_all_helpers
19
- generate_scope_helpers(column_name, order_scope)
20
- generate_position_helpers(column_name)
21
- generate_movable_helpers(column_name)
22
- generate_listable_helpers(column_name)
23
- generate_orderable_helpers
24
- end
25
-
26
- protected
27
-
28
- def generate_method(name, &block)
29
- klass.send(:define_method, name, &block)
30
- end
31
- end
32
- end
33
- end
@@ -1,27 +0,0 @@
1
- module Mongoid
2
- module Orderable
3
- module Generator
4
- module Helpers
5
- def generate_orderable_helpers
6
- self_class = klass
7
-
8
- klass.class_eval <<-eos
9
- def orderable_base(column = nil)
10
- column ||= default_orderable_column
11
- #{self_class}.orderable_configurations[column][:base]
12
- end
13
-
14
- def orderable_column(column = nil)
15
- column ||= default_orderable_column
16
- #{self_class}.orderable_configurations[column][:column]
17
- end
18
- eos
19
-
20
- generate_method(:orderable_inherited_class) do
21
- self_class.orderable_configurations.any? { |_col, conf| conf[:inherited] } ? self_class : self.class
22
- end
23
- end
24
- end
25
- end
26
- end
27
- end
@@ -1,39 +0,0 @@
1
- module Mongoid
2
- module Orderable
3
- module Generator
4
- module Listable
5
- def generate_listable_helpers(column_name)
6
- generate_list_helpers(column_name)
7
- generate_aliased_helpers(column_name)
8
- end
9
-
10
- protected
11
-
12
- def generate_list_helpers(column_name)
13
- generate_method("next_#{column_name}_item") do
14
- next_item(column_name)
15
- end
16
-
17
- generate_method("next_#{column_name}_items") do
18
- next_items(column_name)
19
- end
20
-
21
- generate_method("previous_#{column_name}_item") do
22
- previous_item(column_name)
23
- end
24
-
25
- generate_method("previous_#{column_name}_items") do
26
- previous_items(column_name)
27
- end
28
- end
29
-
30
- def generate_aliased_helpers(column_name)
31
- klass.class_eval do
32
- alias_method "prev_#{column_name}_items", "previous_#{column_name}_items"
33
- alias_method "prev_#{column_name}_item", "previous_#{column_name}_item"
34
- end
35
- end
36
- end
37
- end
38
- end
39
- end
@@ -1,60 +0,0 @@
1
- module Mongoid
2
- module Orderable
3
- module Generator
4
- module Movable
5
- def generate_movable_helpers(column_name)
6
- generate_move_to_helpers(column_name)
7
- generate_insert_at_helpers(column_name)
8
- generate_shorthand_helpers(column_name)
9
- end
10
-
11
- protected
12
-
13
- def generate_move_to_helpers(column_name)
14
- generate_method("move_#{column_name}_to") do |target_position|
15
- move_column_to target_position, column: column_name
16
- end
17
-
18
- generate_method("move_#{column_name}_to!") do |target_position|
19
- move_column_to target_position, column: column_name
20
- save
21
- end
22
-
23
- generate_method("move_#{column_name}_to=") do |target_position|
24
- move_column_to target_position, column: column_name
25
- end
26
- end
27
-
28
- def generate_insert_at_helpers(column_name)
29
- klass.class_eval do
30
- alias_method "insert_#{column_name}_at!", "move_#{column_name}_to!"
31
- alias_method "insert_#{column_name}_at", "move_#{column_name}_to"
32
- alias_method "insert_#{column_name}_at=", "move_#{column_name}_to="
33
- end
34
- end
35
-
36
- def generate_shorthand_helpers(column_name)
37
- [:top, :bottom].each do |symbol|
38
- generate_method "move_#{column_name}_to_#{symbol}" do
39
- move_to symbol, column: column_name
40
- end
41
-
42
- generate_method "move_#{column_name}_to_#{symbol}!" do
43
- move_to! symbol, column: column_name
44
- end
45
- end
46
-
47
- [:higher, :lower].each do |symbol|
48
- generate_method "move_#{column_name}_#{symbol}" do
49
- move_to symbol, column: column_name
50
- end
51
-
52
- generate_method "move_#{column_name}_#{symbol}!" do
53
- move_to! symbol, column: column_name
54
- end
55
- end
56
- end
57
- end
58
- end
59
- end
60
- end
@@ -1,24 +0,0 @@
1
- module Mongoid
2
- module Orderable
3
- module Generator
4
- module Position
5
- def generate_position_helpers(column_name)
6
- klass.class_eval <<-eos
7
- def orderable_position(column = nil)
8
- column ||= default_orderable_column
9
- send "orderable_\#{column}_position"
10
- end
11
- eos
12
-
13
- generate_method("orderable_#{column_name}_position") do
14
- send column_name
15
- end
16
-
17
- generate_method("orderable_#{column_name}_position=") do |value|
18
- send "#{column_name}=", value
19
- end
20
- end
21
- end
22
- end
23
- end
24
- end
@@ -1,17 +0,0 @@
1
- module Mongoid
2
- module Orderable
3
- module Generator
4
- module Scope
5
- def generate_scope_helpers(column_name, order_scope)
6
- klass.class_eval do
7
- scope "orderable_#{column_name}_scope", case order_scope
8
- when Symbol then ->(document) { where(order_scope => document.send(order_scope)) }
9
- when Proc then order_scope
10
- else ->(_document) { where({}) }
11
- end
12
- end
13
- end
14
- end
15
- end
16
- end
17
- end