mongoid_orderable 6.0.3 → 6.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -2
  3. data/lib/mongoid/orderable/configs/field_config.rb +1 -1
  4. data/lib/mongoid/orderable/generators/base.rb +1 -1
  5. data/lib/mongoid/orderable/generators/listable.rb +1 -1
  6. data/lib/mongoid/orderable/generators/lock_collection.rb +1 -1
  7. data/lib/mongoid/orderable/generators/movable.rb +1 -1
  8. data/lib/mongoid/orderable/generators/position.rb +3 -3
  9. data/lib/mongoid/orderable/generators/scope.rb +1 -1
  10. data/lib/mongoid/orderable/handlers/base.rb +32 -6
  11. data/lib/mongoid/orderable/handlers/document.rb +9 -1
  12. data/lib/mongoid/orderable/handlers/document_transactional.rb +3 -6
  13. data/lib/mongoid/orderable/handlers/transaction.rb +6 -2
  14. data/lib/mongoid/orderable/installer.rb +2 -1
  15. data/lib/mongoid/orderable/mixins/callbacks.rb +3 -1
  16. data/lib/mongoid/orderable/mixins/cascadeable.rb +15 -0
  17. data/lib/mongoid/orderable/mixins/movable.rb +7 -3
  18. data/lib/mongoid/orderable/version.rb +1 -1
  19. data/lib/mongoid_orderable.rb +2 -2
  20. metadata +10 -94
  21. data/spec/integration/concurrency_spec.rb +0 -232
  22. data/spec/integration/conditional_spec.rb +0 -36
  23. data/spec/integration/customized_spec.rb +0 -31
  24. data/spec/integration/embedded_spec.rb +0 -41
  25. data/spec/integration/foreign_key_spec.rb +0 -33
  26. data/spec/integration/inherited_spec.rb +0 -54
  27. data/spec/integration/multiple_fields_spec.rb +0 -554
  28. data/spec/integration/multiple_scoped_spec.rb +0 -63
  29. data/spec/integration/no_indexed_spec.rb +0 -23
  30. data/spec/integration/scoped_spec.rb +0 -151
  31. data/spec/integration/simple_spec.rb +0 -184
  32. data/spec/integration/string_scoped_spec.rb +0 -28
  33. data/spec/integration/zero_based_spec.rb +0 -161
  34. data/spec/spec_helper.rb +0 -42
  35. data/spec/support/models.rb +0 -134
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8882038ec4e57486bd693bc1732f935f2d01f949aeedc7f71b3c54bca5ac0b44
4
- data.tar.gz: c0c82c4802280dfe6005e89122e9f7905fabcdf261bc9b3864016d10a70c112c
3
+ metadata.gz: 130b0033ba55283b4cb619832e51b4b9c42191db02c12e0b9db87e98fb3c6cb8
4
+ data.tar.gz: 24994768318208173add72989cf364c888137a739390d39f9759d35fbb8a2ce6
5
5
  SHA512:
6
- metadata.gz: 64332b6ae9d7786864d238accdd3ec81c8defa059ff1d7e1763e487a271c2dffb2f25b3a1a2332947a3b262ecc6cb8da33aae0acdab0cd04bd3ac6e0a2a2ca44
7
- data.tar.gz: 918ea29f2b71caf6cfba5cdc7e145bba69d0d90d2561e1983676fa13a3517f87eaabd7f66d628fbae1ec45df56fe82a11a723de632bbd1650697f39939d10534
6
+ metadata.gz: e401433fc0247621f99cebad69db3034f8b525a3d93035b8ec07c4deeb1116a5a1d568504e0ccfaeb23b8fe9f8fdd56ba5ef41add3526ed4e020b8a8b79f53ec
7
+ data.tar.gz: 73c6c58d8a9da7e14a2e6f9f0866fbf3a7bedf194f81b44d9156387f57c4032f3fb2a04c8fe94dd49953d259b59c0f9f49c9f37778f3c773a3e61a9705965bbb
data/CHANGELOG.md CHANGED
@@ -1,11 +1,22 @@
1
- ### 6.0.4 (Next)
1
+ ### 6.0.6 (Next)
2
2
 
3
- * Your contribution here.
3
+ * Your change here.
4
+
5
+ ### 6.0.5 (2023/11/08)
6
+
7
+ * [#75](https://github.com/mongoid/mongoid_orderable/pull/75): Fix: Preserve position when creating new records.
8
+ * [#84](https://github.com/mongoid/mongoid_orderable/pull/84): Feature: Use Mongo::QueryCache if available.
9
+ * [#85](https://github.com/mongoid/mongoid_orderable/pull/85): Refactor: Replace all usages of "protected" with "private".
10
+
11
+ ### 6.0.4 (2023/02/01)
12
+
13
+ * [#75](https://github.com/mongoid/mongoid_orderable/pull/75): Fix: Setting #move_to should mark an orderable object as changed so that callbacks are triggered.
4
14
 
5
15
  ### 6.0.3 (2021/06/27)
6
16
 
7
17
  * [#72](https://github.com/mongoid/mongoid_orderable/pull/72): Feature: Add :if and :unless conditions which disable callbacks.
8
18
  * [#71](https://github.com/mongoid/mongoid_orderable/pull/71): Fix: Add TTL index to locks table.
19
+ * [#73](https://github.com/mongoid/mongoid_orderable/pull/73): Refactor: Replace Travis CI with Github Actions.
9
20
 
10
21
  ### 6.0.2 (2021/01/26)
11
22
 
@@ -41,7 +41,7 @@ module Configs
41
41
  lock_collection: cfg.lock_collection }
42
42
  end
43
43
 
44
- protected
44
+ private
45
45
 
46
46
  def assign_options(options)
47
47
  @options = global_config
@@ -10,7 +10,7 @@ module Generators
10
10
  @klass = klass
11
11
  end
12
12
 
13
- protected
13
+ private
14
14
 
15
15
  def generate_method(name, &block)
16
16
  klass.send(:define_method, name, &block)
@@ -9,7 +9,7 @@ module Generators
9
9
  generate_aliased_helpers(field_name)
10
10
  end
11
11
 
12
- protected
12
+ private
13
13
 
14
14
  def generate_list_helpers(field_name)
15
15
  generate_method("next_#{field_name}_item") do
@@ -25,7 +25,7 @@ module Generators
25
25
  KLASS
26
26
  end
27
27
 
28
- protected
28
+ private
29
29
 
30
30
  def model_exists?(model_name)
31
31
  base = ::Mongoid::Orderable::Models
@@ -10,7 +10,7 @@ module Generators
10
10
  generate_shorthand_helpers(field_name)
11
11
  end
12
12
 
13
- protected
13
+ private
14
14
 
15
15
  def generate_move_to_helpers(field_name)
16
16
  generate_method("move_#{field_name}_to") do |target_position|
@@ -8,16 +8,16 @@ module Generators
8
8
  klass.class_eval <<~KLASS, __FILE__, __LINE__ + 1
9
9
  def orderable_position(field = nil)
10
10
  field ||= default_orderable_field
11
- send "orderable_\#{field}_position"
11
+ send("orderable_\#{field}_position")
12
12
  end
13
13
  KLASS
14
14
 
15
15
  generate_method("orderable_#{field_name}_position") do
16
- send field_name
16
+ send(field_name)
17
17
  end
18
18
 
19
19
  generate_method("orderable_#{field_name}_position=") do |value|
20
- send "#{field_name}=", value
20
+ send("#{field_name}=", value)
21
21
  end
22
22
  end
23
23
  end
@@ -11,7 +11,7 @@ module Generators
11
11
  end
12
12
  end
13
13
 
14
- protected
14
+ private
15
15
 
16
16
  def criteria(order_scope)
17
17
  case order_scope
@@ -8,9 +8,10 @@ module Handlers
8
8
 
9
9
  def initialize(doc)
10
10
  @doc = doc
11
+ reset_new_record
11
12
  end
12
13
 
13
- protected
14
+ private
14
15
 
15
16
  delegate :orderable_keys,
16
17
  :orderable_field,
@@ -22,12 +23,15 @@ module Handlers
22
23
  :orderable_top,
23
24
  :orderable_bottom,
24
25
  :_id,
25
- :new_record?,
26
26
  :persisted?,
27
27
  :embedded?,
28
28
  :collection_name,
29
29
  to: :doc
30
30
 
31
+ def new_record?
32
+ use_transactions ? @new_record : doc.new_record?
33
+ end
34
+
31
35
  def use_transactions
32
36
  false
33
37
  end
@@ -36,8 +40,18 @@ module Handlers
36
40
  orderable_keys.any? {|field| changed?(field) }
37
41
  end
38
42
 
43
+ def set_new_record_positions
44
+ return unless new_record?
45
+
46
+ orderable_keys.each do |field|
47
+ next unless (position = doc.send(field))
48
+
49
+ move_all[field] ||= position
50
+ end
51
+ end
52
+
39
53
  def apply_all_positions
40
- orderable_keys.map {|field| apply_one_position(field, move_all[field]) }
54
+ orderable_keys.each {|field| apply_one_position(field, move_all[field]) }
41
55
  end
42
56
 
43
57
  def apply_one_position(field, target_position)
@@ -56,7 +70,7 @@ module Handlers
56
70
  end
57
71
 
58
72
  # Get the current position as exists in the database
59
- current = if !persisted? || scope_changed
73
+ current = if new_record? || scope_changed
60
74
  nil
61
75
  elsif persisted? && !embedded?
62
76
  scope.where(_id: _id).pluck(f).first
@@ -74,8 +88,9 @@ module Handlers
74
88
  in_list = persisted? && current
75
89
  return if in_list && !target_position
76
90
 
77
- # Use $inc operator to shift the position of the other documents
78
91
  target = resolve_target_position(field, target_position, in_list)
92
+
93
+ # Use $inc operator to shift the position of the other documents
79
94
  if !in_list
80
95
  scope.gte(f => target).inc(f => 1)
81
96
  elsif target < current
@@ -107,10 +122,21 @@ module Handlers
107
122
  doc.send(:move_all)
108
123
  end
109
124
 
125
+ def reset
126
+ reset_new_record
127
+ doc.send(:clear_move_all!)
128
+ end
129
+
130
+ # Required for transactions, which perform some actions
131
+ # in the after_create callback.
132
+ def reset_new_record
133
+ @new_record = doc.new_record?
134
+ end
135
+
110
136
  def resolve_target_position(field, target_position, in_list)
111
137
  target_position ||= 'bottom'
112
138
 
113
- unless target_position.is_a? Numeric
139
+ unless target_position.is_a?(Numeric)
114
140
  target_position = case target_position.to_s
115
141
  when 'top' then (min ||= orderable_top(field))
116
142
  when 'bottom' then (max ||= orderable_bottom(field, in_list))
@@ -5,18 +5,26 @@ module Orderable
5
5
  module Handlers
6
6
  class Document < Base
7
7
  def before_create
8
+ set_new_record_positions
8
9
  apply_all_positions
9
10
  end
10
11
 
11
- def after_create; end
12
+ def after_create
13
+ reset
14
+ end
12
15
 
13
16
  def before_update
14
17
  return unless any_field_changed?
15
18
  apply_all_positions
16
19
  end
17
20
 
21
+ def after_update
22
+ reset
23
+ end
24
+
18
25
  def after_destroy
19
26
  remove_all_positions
27
+ reset
20
28
  end
21
29
  end
22
30
  end
@@ -5,23 +5,20 @@ module Orderable
5
5
  module Handlers
6
6
  class DocumentTransactional < Document
7
7
  def before_create
8
- clear_all_positions
8
+ set_new_record_positions
9
9
  end
10
10
 
11
11
  def after_create
12
12
  apply_all_positions
13
+ super
13
14
  end
14
15
 
15
- protected
16
+ private
16
17
 
17
18
  def apply_all_positions
18
19
  with_transaction { super }
19
20
  end
20
21
 
21
- def clear_all_positions
22
- orderable_keys.each {|field| doc.send("orderable_#{field}_position=", nil) }
23
- end
24
-
25
22
  def use_transactions
26
23
  true
27
24
  end
@@ -15,7 +15,7 @@ module Handlers
15
15
  end
16
16
 
17
17
  def with_transaction(&block)
18
- Mongoid::QueryCache.uncached do
18
+ query_cache.uncached do
19
19
  if Thread.current[THREAD_KEY]
20
20
  yield
21
21
  else
@@ -35,7 +35,7 @@ module Handlers
35
35
  end
36
36
  end
37
37
 
38
- protected
38
+ private
39
39
 
40
40
  def do_transaction(&_block)
41
41
  doc.class.with_session(session_opts) do |session|
@@ -65,6 +65,10 @@ module Handlers
65
65
  def transaction_max_retries
66
66
  doc.orderable_keys.map {|k| doc.class.orderable_configs.dig(k, :transaction_max_retries) }.compact.max
67
67
  end
68
+
69
+ def query_cache
70
+ defined?(Mongo::QueryCache) ? Mongo::QueryCache : Mongoid::QueryCache
71
+ end
68
72
  end
69
73
  end
70
74
  end
@@ -18,7 +18,7 @@ module Orderable
18
18
  generate_all_helpers
19
19
  end
20
20
 
21
- protected
21
+ private
22
22
 
23
23
  def field_name
24
24
  config[:field_options][:as] || config[:field]
@@ -48,6 +48,7 @@ module Orderable
48
48
  klass.send :include, Mixins::Callbacks
49
49
  klass.send :include, Mixins::Movable
50
50
  klass.send :include, Mixins::Listable
51
+ klass.send :prepend, Mixins::Cascadeable if klass.embedded?
51
52
  end
52
53
 
53
54
  def generate_all_helpers
@@ -12,16 +12,18 @@ module Mixins
12
12
  before_create :orderable_before_create
13
13
  after_create :orderable_after_create, prepend: true
14
14
  before_update :orderable_before_update
15
+ after_update :orderable_after_update, prepend: true
15
16
  after_destroy :orderable_after_destroy, prepend: true
16
17
 
17
18
  delegate :before_create,
18
19
  :after_create,
19
20
  :before_update,
21
+ :after_update,
20
22
  :after_destroy,
21
23
  to: :orderable_handler,
22
24
  prefix: :orderable
23
25
 
24
- protected
26
+ private
25
27
 
26
28
  def orderable_handler
27
29
  @orderable_handler ||= self.class.orderable_handler_class.new(self)
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mongoid
4
+ module Orderable
5
+ module Mixins
6
+ # This is required to trigger callbacks on embedded objects.
7
+ # Otherwise, the #move_to parameter won't work when saving the parent.
8
+ module Cascadeable
9
+ def in_callback_state?(kind)
10
+ super || move_all.present?
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -44,15 +44,19 @@ module Mixins
44
44
  KLASS
45
45
  end
46
46
 
47
- protected
47
+ private
48
48
 
49
49
  def move_all
50
- @move_all || {}
50
+ @move_all ||= {}
51
51
  end
52
52
 
53
53
  def move_field_to(position, options)
54
54
  field = options[:field] || default_orderable_field
55
- @move_all = move_all.merge(field => position)
55
+ move_all[field] = position
56
+ end
57
+
58
+ def clear_move_all!
59
+ @move_all = {}
56
60
  end
57
61
  end
58
62
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Mongoid
4
4
  module Orderable
5
- VERSION = '6.0.3'
5
+ VERSION = '6.0.5'
6
6
  end
7
7
  end
@@ -7,9 +7,8 @@ I18n.load_path << File.join(File.dirname(__FILE__), 'config', 'locales', 'en.yml
7
7
 
8
8
  require 'mongoid'
9
9
 
10
- require 'mongoid/orderable/version'
11
-
12
10
  require 'mongoid/orderable'
11
+ require 'mongoid/orderable/version'
13
12
  require 'mongoid/orderable/configs/global_config'
14
13
  require 'mongoid/orderable/configs/field_config'
15
14
  require 'mongoid/orderable/errors/invalid_target_position'
@@ -18,6 +17,7 @@ require 'mongoid/orderable/mixins/helpers'
18
17
  require 'mongoid/orderable/mixins/callbacks'
19
18
  require 'mongoid/orderable/mixins/listable'
20
19
  require 'mongoid/orderable/mixins/movable'
20
+ require 'mongoid/orderable/mixins/cascadeable'
21
21
  require 'mongoid/orderable/generators/base'
22
22
  require 'mongoid/orderable/generators/listable'
23
23
  require 'mongoid/orderable/generators/lock_collection'
metadata CHANGED
@@ -1,71 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_orderable
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.3
4
+ version: 6.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - pyromaniac
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-28 00:00:00.000000000 Z
11
+ date: 2023-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: rake
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rspec
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: 3.0.0
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: 3.0.0
41
- - !ruby/object:Gem::Dependency
42
- name: rspec-retry
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: rubocop
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: 1.8.1
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: 1.8.1
69
13
  - !ruby/object:Gem::Dependency
70
14
  name: mongoid
71
15
  requirement: !ruby/object:Gem::Requirement
@@ -111,31 +55,18 @@ files:
111
55
  - lib/mongoid/orderable/handlers/transaction.rb
112
56
  - lib/mongoid/orderable/installer.rb
113
57
  - lib/mongoid/orderable/mixins/callbacks.rb
58
+ - lib/mongoid/orderable/mixins/cascadeable.rb
114
59
  - lib/mongoid/orderable/mixins/helpers.rb
115
60
  - lib/mongoid/orderable/mixins/listable.rb
116
61
  - lib/mongoid/orderable/mixins/movable.rb
117
62
  - lib/mongoid/orderable/version.rb
118
63
  - lib/mongoid_orderable.rb
119
- - spec/integration/concurrency_spec.rb
120
- - spec/integration/conditional_spec.rb
121
- - spec/integration/customized_spec.rb
122
- - spec/integration/embedded_spec.rb
123
- - spec/integration/foreign_key_spec.rb
124
- - spec/integration/inherited_spec.rb
125
- - spec/integration/multiple_fields_spec.rb
126
- - spec/integration/multiple_scoped_spec.rb
127
- - spec/integration/no_indexed_spec.rb
128
- - spec/integration/scoped_spec.rb
129
- - spec/integration/simple_spec.rb
130
- - spec/integration/string_scoped_spec.rb
131
- - spec/integration/zero_based_spec.rb
132
- - spec/spec_helper.rb
133
- - spec/support/models.rb
134
64
  homepage: https://github.com/mongoid/mongoid_orderable
135
65
  licenses:
136
66
  - MIT
137
- metadata: {}
138
- post_install_message:
67
+ metadata:
68
+ rubygems_mfa_required: 'true'
69
+ post_install_message:
139
70
  rdoc_options: []
140
71
  require_paths:
141
72
  - lib
@@ -150,23 +81,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
81
  - !ruby/object:Gem::Version
151
82
  version: '0'
152
83
  requirements: []
153
- rubygems_version: 3.2.9
154
- signing_key:
84
+ rubygems_version: 3.4.4
85
+ signing_key:
155
86
  specification_version: 4
156
87
  summary: Mongoid orderable list implementation
157
- test_files:
158
- - spec/integration/concurrency_spec.rb
159
- - spec/integration/conditional_spec.rb
160
- - spec/integration/customized_spec.rb
161
- - spec/integration/embedded_spec.rb
162
- - spec/integration/foreign_key_spec.rb
163
- - spec/integration/inherited_spec.rb
164
- - spec/integration/multiple_fields_spec.rb
165
- - spec/integration/multiple_scoped_spec.rb
166
- - spec/integration/no_indexed_spec.rb
167
- - spec/integration/scoped_spec.rb
168
- - spec/integration/simple_spec.rb
169
- - spec/integration/string_scoped_spec.rb
170
- - spec/integration/zero_based_spec.rb
171
- - spec/spec_helper.rb
172
- - spec/support/models.rb
88
+ test_files: []