sortability 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: 8a43a19779df7ff3847545f12c4581dade2c4852
4
- data.tar.gz: 2fabbb3ef162fa77e2bf7c090f79dc18b9b8bbdc
3
+ metadata.gz: 29973c452c7c5e7cefbedbe2dd255581109dc2e2
4
+ data.tar.gz: 648ede88227d43edc148332223a3ebf8c629a763
5
5
  SHA512:
6
- metadata.gz: 4fa64ad8cac6a90c6886f43c7cd8b69058aaf30e2591fdd6bebb22cbecf4ce00d67f4bbf73755b7f997838dbcc579a79dc4bab12421527fa906bb8ea8034385a
7
- data.tar.gz: 1b3e627ef50acdb7fb72441fed42861b9ce1d0619274005c0218d6a957fa53bb01cb2af04cc0018e14855bd66554d930765be7dcf2bce4fc7daf3ca58ee86fd4
6
+ metadata.gz: 7a03cc07e6a6c2289c06a8cccc7c58341d698a034fd47fea0d5a1fbb3c2d475e91ad60d332e77821ad2498c3468ad06ea6087d6df8b54447d0c6278180bddd6a
7
+ data.tar.gz: 7b17c665e6debd0646e8ff2a54623441b3f34273c3030efb3a808181f93bed0a91c0d7972974a07935cbf87ad15b2f4f21201f94925d193cd3d08171682db2fe
data/README.md CHANGED
@@ -129,7 +129,7 @@ Here are some things that you can do:
129
129
  `container` also be automatically updated to create a gap for that `record`.
130
130
 
131
131
  - Close all gaps in the `sort_position` for the peers of a `record`
132
- by calling the `compact_sort_position_peers` method.
132
+ by calling the `compact_sort_position_peers!` method.
133
133
 
134
134
  - Get the next or previous record by using the `next_by_sort_position` or
135
135
  `previous_by_sort_position` methods.
@@ -138,7 +138,7 @@ The `sort_position` is not guaranteed to contain consecutive numbers.
138
138
  When listing the `records`, you should compute their positions
139
139
  in application code as you iterate through the list.
140
140
  If you need the position for a single `record`, call
141
- `compact_sort_position_peers` first to close any gaps
141
+ `compact_sort_position_peers!` first to close any gaps
142
142
  in its peers, then read `sort_position` directly.
143
143
 
144
144
  ## Contributing
@@ -19,7 +19,7 @@ module Sortability
19
19
  before_validation_mname = "#{onname}_before_validation"
20
20
  next_by_mname = "next_by_#{onname}"
21
21
  prev_by_mname = "previous_by_#{onname}"
22
- compact_peers_mname = "compact_#{onname}_peers"
22
+ compact_peers_mname = "compact_#{onname}_peers!"
23
23
 
24
24
  class_exec do
25
25
  before_validation before_validation_mname
@@ -85,20 +85,27 @@ module Sortability
85
85
  # Renumbers the peers so that their numbers are sequential,
86
86
  # starting at 1
87
87
  define_method compact_peers_mname do
88
- mysql = \
89
- defined?(ActiveRecord::ConnectionAdapters::MysqlAdapter) && \
90
- ActiveRecord::Base.connection.instance_of?(
91
- ActiveRecord::ConnectionAdapters::MysqlAdapter)
92
- cend = mysql ? 'END CASE' : 'END'
93
-
88
+ needs_update = false
94
89
  peers = send(peers_mname)
95
90
  cases = peers.to_a.collect.with_index do |p, i|
91
+ old_val = p.send(on)
92
+ new_val = i + 1
93
+ needs_update = true if old_val != new_val
94
+
96
95
  # Make sure "on" field in self is up to date
97
- send(setter_mname, i + 1) if p == self
96
+ send(setter_mname, new_val) if p == self
98
97
 
99
- "WHEN #{p.send(on)} THEN #{- (i + 1)}"
98
+ "WHEN #{old_val} THEN #{- new_val}"
100
99
  end.join(' ')
101
100
 
101
+ return peers unless needs_update
102
+
103
+ mysql = \
104
+ defined?(ActiveRecord::ConnectionAdapters::MysqlAdapter) && \
105
+ ActiveRecord::Base.connection.instance_of?(
106
+ ActiveRecord::ConnectionAdapters::MysqlAdapter)
107
+ cend = mysql ? 'END CASE' : 'END'
108
+
102
109
  self.class.transaction do
103
110
  peers.reorder(nil)
104
111
  .update_all("#{onname} = CASE #{onname} #{cases} #{cend}")
@@ -1,3 +1,3 @@
1
1
  module Sortability
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sortability
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dante Soares
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-12-05 00:00:00.000000000 Z
12
+ date: 2014-12-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails