positioning 0.1.2 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 42c7d9b5b08bb03060627230a1b44fe9d243b57779541de8def15a0f49f57011
4
- data.tar.gz: 66a36e6cc2f019a34c4e1c0d92700be81062556c01da38de8fed632f12bde607
3
+ metadata.gz: 2863317333ca4e36b7c8aecb178fb38f4489fc4dd76eb771c053769eb82cfbe2
4
+ data.tar.gz: 13ad7808c1beb5ffaff7d97849b48e194915eeb043e541254843bb22ecfc167d
5
5
  SHA512:
6
- metadata.gz: 50b3a860281e94d7657b82265e639f038d0004bbfde9d3cf19ca38aeab77814a92d31f5660e6f802c4c72c8931e42c960313413660dd48b0cbc81ca3c899ca30
7
- data.tar.gz: 71718c58f8ada39e8b089cc2a2de1da96b685099461b5b397fd40c766549e60279e7b5e6c7d3a61081d5e435b18d3d24e260d985bbda5f2916669f3bdd8f1321
6
+ metadata.gz: dfcf0d2652e7c9e7ac65e5a1c885edae966ec0d7bc7119b3691c13b2c34c8ad434c6a7e8590e0ff76e550000cddd8cc3b2b2daf72b0626803d988ed21d6e8d7c
7
+ data.tar.gz: bcdd1517650a8327eaa63ebed37bdcf2cc236beaad383eed23c3e063dfca1031c00714acf99542e8dca0d7320a53fcba6181da24f1e60fe0e4c706e870dd23ae
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.4] - 2024-03-04
4
+
5
+ - Fix bug relating to relative position hash coming from Rails being a Hash With Indifferent Access
6
+
7
+ ## [0.1.3] - 2024-03-04
8
+
9
+ - Internal refactoring of Mechanisms for clarity
10
+ - Additional unit testing of Mechanisms
11
+ - Added additional Ruby and Rails versions to the Github Actions matrix
12
+
3
13
  ## [0.1.2] - 2024-02-29
4
14
 
5
15
  - Fix a bug related to the scope changing with an explicitly set position value that is the same as the original position.
data/Gemfile CHANGED
@@ -8,3 +8,8 @@ gem "rake", "~> 13.0"
8
8
  gem "minitest", "~> 5.0"
9
9
 
10
10
  gem "standard", "~> 1.3"
11
+
12
+ if ENV["RAILS_VERSION"]
13
+ gem "activerecord", ENV["RAILS_VERSION"]
14
+ gem "activesupport", ENV["RAILS_VERSION"]
15
+ end
@@ -20,23 +20,14 @@ module Positioning
20
20
  end
21
21
 
22
22
  def update_position
23
- # If we're changing scope but not explicitly setting the position then we set the position
24
- # to nil so that the item gets placed at the end of the list.
25
- self.position = nil if positioning_scope_changed? && !position_changed?
23
+ clear_position if positioning_scope_changed? && !position_changed?
26
24
 
27
25
  solidify_position
28
26
 
29
- # The update strategy is to temporarily set our position to 0, then shift everything out of the way of
30
- # our new desired position before finalising it.
31
27
  if positioning_scope_changed? || position_changed?
32
- record_scope = base_class.where("#{primary_key_column}": primary_key)
33
-
34
- position_was = record_scope.pick(@column)
35
- record_scope.update_all "#{@column}": 0
28
+ move_out_of_the_way
36
29
 
37
30
  if positioning_scope_changed?
38
- positioning_scope_was = base_class.where record_scope.first.slice(*positioning_columns)
39
-
40
31
  contract(positioning_scope_was, position_was..)
41
32
  expand(positioning_scope, position..)
42
33
  elsif position_was > position
@@ -65,6 +56,10 @@ module Positioning
65
56
  @positioned.send primary_key_column
66
57
  end
67
58
 
59
+ def record_scope
60
+ base_class.where("#{primary_key_column}": primary_key)
61
+ end
62
+
68
63
  def position
69
64
  @positioned.send @column
70
65
  end
@@ -73,10 +68,23 @@ module Positioning
73
68
  @positioned.send :"#{@column}=", position
74
69
  end
75
70
 
71
+ def clear_position
72
+ self.position = nil
73
+ end
74
+
76
75
  def position_changed?
77
76
  @positioned.send :"#{@column}_changed?"
78
77
  end
79
78
 
79
+ def position_was
80
+ @position_was ||= record_scope.pick(@column)
81
+ end
82
+
83
+ def move_out_of_the_way
84
+ position_was # Memoize the original position before changing it
85
+ record_scope.update_all "#{@column}": 0
86
+ end
87
+
80
88
  def expand(scope, range)
81
89
  scope.where("#{@column}": range).update_all "#{@column} = #{@column} * -1"
82
90
  scope.where("#{@column}": ..-1).update_all "#{@column} = #{@column} * -1 + 1"
@@ -90,7 +98,7 @@ module Positioning
90
98
  def solidify_position
91
99
  position_before_type_cast = @positioned.read_attribute_before_type_cast @column
92
100
  position_before_type_cast.to_sym if position_before_type_cast.is_a? String
93
- position_before_type_cast.symbolize_keys! if position_before_type_cast.is_a? Hash
101
+ position_before_type_cast = position_before_type_cast.symbolize_keys if position_before_type_cast.is_a? Hash
94
102
 
95
103
  case position_before_type_cast
96
104
  when Integer
@@ -118,8 +126,6 @@ module Positioning
118
126
  raise Error.new, "relative `#{@column}` record must be in the same scope"
119
127
  end
120
128
 
121
- position_was = base_class.where("#{primary_key_column}": primary_key).pick(@column)
122
-
123
129
  solidified_position = relative_record_scope.pick(@column)
124
130
  solidified_position += 1 if relative_position == :after
125
131
  solidified_position -= 1 if in_positioning_scope? && position_was < solidified_position
@@ -143,11 +149,15 @@ module Positioning
143
149
  end
144
150
 
145
151
  def positioning_scope
146
- base_class.where(@positioned.slice(*positioning_columns)).order(@column)
152
+ base_class.where @positioned.slice(*positioning_columns)
153
+ end
154
+
155
+ def positioning_scope_was
156
+ base_class.where record_scope.first.slice(*positioning_columns)
147
157
  end
148
158
 
149
159
  def in_positioning_scope?
150
- @positioned.persisted? && positioning_scope.exists?(primary_key)
160
+ @positioned.persisted? && positioning_scope.where("#{primary_key_column}": primary_key).exists?
151
161
  end
152
162
 
153
163
  def positioning_scope_changed?
@@ -1,3 +1,3 @@
1
1
  module Positioning
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: positioning
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brendon Muir
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-29 00:00:00.000000000 Z
11
+ date: 2024-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -118,7 +118,6 @@ files:
118
118
  - ".standard.yml"
119
119
  - CHANGELOG.md
120
120
  - Gemfile
121
- - Gemfile.lock
122
121
  - LICENSE.txt
123
122
  - README.md
124
123
  - Rakefile
data/Gemfile.lock DELETED
@@ -1,109 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- positioning (0.1.2)
5
- activerecord (>= 6.1)
6
- activesupport (>= 6.1)
7
-
8
- GEM
9
- remote: https://rubygems.org/
10
- specs:
11
- activemodel (7.1.3.2)
12
- activesupport (= 7.1.3.2)
13
- activerecord (7.1.3.2)
14
- activemodel (= 7.1.3.2)
15
- activesupport (= 7.1.3.2)
16
- timeout (>= 0.4.0)
17
- activesupport (7.1.3.2)
18
- base64
19
- bigdecimal
20
- concurrent-ruby (~> 1.0, >= 1.0.2)
21
- connection_pool (>= 2.2.5)
22
- drb
23
- i18n (>= 1.6, < 2)
24
- minitest (>= 5.1)
25
- mutex_m
26
- tzinfo (~> 2.0)
27
- ast (2.4.2)
28
- base64 (0.2.0)
29
- bigdecimal (3.1.6)
30
- concurrent-ruby (1.2.3)
31
- connection_pool (2.4.1)
32
- drb (2.2.0)
33
- ruby2_keywords
34
- i18n (1.14.1)
35
- concurrent-ruby (~> 1.0)
36
- json (2.7.1)
37
- language_server-protocol (3.17.0.3)
38
- lint_roller (1.1.0)
39
- minitest (5.22.1)
40
- minitest-hooks (1.5.1)
41
- minitest (> 5.3)
42
- mocha (2.1.0)
43
- ruby2_keywords (>= 0.0.5)
44
- mutex_m (0.2.0)
45
- mysql2 (0.5.6)
46
- parallel (1.24.0)
47
- parser (3.3.0.5)
48
- ast (~> 2.4.1)
49
- racc
50
- pg (1.5.5)
51
- racc (1.7.3)
52
- rainbow (3.1.1)
53
- rake (13.1.0)
54
- regexp_parser (2.9.0)
55
- rexml (3.2.6)
56
- rubocop (1.59.0)
57
- json (~> 2.3)
58
- language_server-protocol (>= 3.17.0)
59
- parallel (~> 1.10)
60
- parser (>= 3.2.2.4)
61
- rainbow (>= 2.2.2, < 4.0)
62
- regexp_parser (>= 1.8, < 3.0)
63
- rexml (>= 3.2.5, < 4.0)
64
- rubocop-ast (>= 1.30.0, < 2.0)
65
- ruby-progressbar (~> 1.7)
66
- unicode-display_width (>= 2.4.0, < 3.0)
67
- rubocop-ast (1.30.0)
68
- parser (>= 3.2.1.0)
69
- rubocop-performance (1.20.2)
70
- rubocop (>= 1.48.1, < 2.0)
71
- rubocop-ast (>= 1.30.0, < 2.0)
72
- ruby-progressbar (1.13.0)
73
- ruby2_keywords (0.0.5)
74
- sqlite3 (1.7.2-arm64-darwin)
75
- sqlite3 (1.7.2-x86_64-linux)
76
- standard (1.33.0)
77
- language_server-protocol (~> 3.17.0.2)
78
- lint_roller (~> 1.0)
79
- rubocop (~> 1.59.0)
80
- standard-custom (~> 1.0.0)
81
- standard-performance (~> 1.3)
82
- standard-custom (1.0.2)
83
- lint_roller (~> 1.0)
84
- rubocop (~> 1.50)
85
- standard-performance (1.3.1)
86
- lint_roller (~> 1.1)
87
- rubocop-performance (~> 1.20.2)
88
- timeout (0.4.1)
89
- tzinfo (2.0.6)
90
- concurrent-ruby (~> 1.0)
91
- unicode-display_width (2.5.0)
92
-
93
- PLATFORMS
94
- arm64-darwin-21
95
- x86_64-linux
96
-
97
- DEPENDENCIES
98
- minitest (~> 5.0)
99
- minitest-hooks (~> 1.5.1)
100
- mocha (~> 2.1.0)
101
- mysql2 (~> 0.5.6)
102
- pg (~> 1.5.5)
103
- positioning!
104
- rake (~> 13.0)
105
- sqlite3 (~> 1.7.2)
106
- standard (~> 1.3)
107
-
108
- BUNDLED WITH
109
- 2.3.8