sortability 0.1.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +2 -2
- data/lib/sortability/active_record/base.rb +19 -16
- data/lib/sortability/version.rb +1 -1
- data/spec/dummy/app/assets/config/manifest.js +3 -0
- data/spec/dummy/app/models/application_record.rb +3 -0
- data/spec/dummy/app/models/container.rb +5 -0
- data/spec/dummy/app/models/item.rb +3 -0
- data/spec/dummy/config/application.rb +3 -0
- data/spec/dummy/db/migrate/20190523235417_create_containers.rb +9 -0
- data/spec/dummy/db/migrate/20190523235430_create_items.rb +12 -0
- data/spec/dummy/db/schema.rb +29 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/test.log +2485 -0
- data/spec/lib/sortability/active_record/base_spec.rb +202 -1
- data/spec/lib/sortability/active_record/connection_adapters/table_definition_spec.rb +21 -1
- data/spec/lib/sortability/active_record/migration_spec.rb +44 -1
- metadata +35 -32
- data/spec/dummy/log/development.log +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e811a6edc06dbfee9c4432f0b81eec7a36aec4cf07e40e5604f397f84a62007f
|
4
|
+
data.tar.gz: 172646fbe4de26f3def8354d671378a9fc5d6faea2b95757d0973c06c6e9de43
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6844492fe3c495616404aa9cf5edcda32fd8082d6c3a515f706e2b64710a9e8cc459c5845647d26ab8b9f65599af332bc56c085f0cae670072fddd8293cdfadb
|
7
|
+
data.tar.gz: 118dd54f18ad0de05bab919d8f65dddeb0de04861fdb1b3be819ca57fce7b4b00a71531e407f3c37299ca19a293154d497c752293c7ba8093144be1a4f98f67e
|
data/README.md
CHANGED
@@ -49,7 +49,7 @@ $ rails g migration add_sort_position_to_records
|
|
49
49
|
In this migration, you will want something similar to this:
|
50
50
|
|
51
51
|
```rb
|
52
|
-
class AddSortPositionToRecords < ActiveRecord::Migration
|
52
|
+
class AddSortPositionToRecords < ActiveRecord::Migration[4.2]
|
53
53
|
def change
|
54
54
|
add_sortable_column :records # , on: :sort_position
|
55
55
|
add_sortable_index :records, scope: :container_id # , on: :sort_position
|
@@ -65,7 +65,7 @@ but you should still create the index using `add_sortable_index`
|
|
65
65
|
to ensure that the index covers the appropriate column(s):
|
66
66
|
|
67
67
|
```rb
|
68
|
-
class CreateRecords < ActiveRecord::Migration
|
68
|
+
class CreateRecords < ActiveRecord::Migration[4.2]
|
69
69
|
def change
|
70
70
|
create_table :records do |t|
|
71
71
|
t.sortable # on: :sort_position
|
@@ -22,7 +22,7 @@ module Sortability
|
|
22
22
|
compact_peers_mname = "compact_#{onname}_peers!"
|
23
23
|
|
24
24
|
class_exec do
|
25
|
-
before_validation before_validation_mname
|
25
|
+
before_validation before_validation_mname.to_sym
|
26
26
|
|
27
27
|
# Returns all the sort peers for this record, including self
|
28
28
|
define_method peers_mname do |force_scope_load = false|
|
@@ -56,9 +56,12 @@ module Sortability
|
|
56
56
|
send(setter_mname, max_val + 1)
|
57
57
|
elsif peers.to_a.any? { |p| p != self && p.send(on) == val }
|
58
58
|
# Make a gap for the record
|
59
|
-
|
59
|
+
at = self.class.arel_table
|
60
|
+
peers.where(at[on].gteq(val))
|
61
|
+
.reorder(nil)
|
60
62
|
.update_all("#{onname} = - (#{onname} + 1)")
|
61
|
-
peers.where
|
63
|
+
peers.where(at[on].lt(0))
|
64
|
+
.reorder(nil)
|
62
65
|
.update_all("#{onname} = - #{onname}")
|
63
66
|
|
64
67
|
# Cause peers to load from the DB the next time they are used
|
@@ -71,8 +74,8 @@ module Sortability
|
|
71
74
|
val = send(on)
|
72
75
|
peers = send(peers_mname)
|
73
76
|
peers.loaded? ? \
|
74
|
-
peers.to_a.detect{|p| p.send(on) > val} : \
|
75
|
-
peers.where
|
77
|
+
peers.to_a.detect { |p| p.send(on) > val } : \
|
78
|
+
peers.where(peers.arel_table[on].gt(val)).first
|
76
79
|
end
|
77
80
|
|
78
81
|
# Gets the previous record among the peers
|
@@ -80,8 +83,8 @@ module Sortability
|
|
80
83
|
val = send(on)
|
81
84
|
peers = send(peers_mname)
|
82
85
|
peers.loaded? ? \
|
83
|
-
peers.to_a.reverse.detect{|p| p.send(on) < val} : \
|
84
|
-
peers.where
|
86
|
+
peers.to_a.reverse.detect { |p| p.send(on) < val } : \
|
87
|
+
peers.where(peers.arel_table[on].lt(val)).last
|
85
88
|
end
|
86
89
|
|
87
90
|
# Renumbers the peers so that their numbers are sequential,
|
@@ -123,24 +126,24 @@ module Sortability
|
|
123
126
|
end
|
124
127
|
|
125
128
|
# Defines a sortable has_many relation on the container
|
126
|
-
def sortable_has_many(records, scope_or_options = nil,
|
127
|
-
scope, options = extract_association_params(scope_or_options,
|
129
|
+
def sortable_has_many(records, scope_or_options = nil, **remaining_options, &extension)
|
130
|
+
scope, options = extract_association_params(scope_or_options, remaining_options)
|
128
131
|
if scope.nil?
|
129
132
|
on = options[:on] || :sort_position
|
130
133
|
scope = -> { order(on) }
|
131
134
|
end
|
132
135
|
|
133
|
-
class_exec { has_many records, scope, options.except(:on), &extension }
|
136
|
+
class_exec { has_many records, scope, **options.except(:on), &extension }
|
134
137
|
end
|
135
138
|
|
136
139
|
# Defines a sortable belongs_to relation on the child records
|
137
140
|
def sortable_belongs_to(container, scope_or_options = nil,
|
138
|
-
|
139
|
-
scope, options = extract_association_params(scope_or_options,
|
141
|
+
**remaining_options, &extension)
|
142
|
+
scope, options = extract_association_params(scope_or_options, remaining_options)
|
140
143
|
on = options[:on] || :sort_position
|
141
144
|
|
142
145
|
class_exec do
|
143
|
-
belongs_to container, scope, options.except(:on, :scope), &extension
|
146
|
+
belongs_to container, scope, **options.except(:on, :scope), &extension
|
144
147
|
|
145
148
|
reflection = reflect_on_association(container)
|
146
149
|
options[:scope] ||= reflection.polymorphic? ? \
|
@@ -178,11 +181,11 @@ module Sortability
|
|
178
181
|
|
179
182
|
protected
|
180
183
|
|
181
|
-
def extract_association_params(scope_or_options,
|
184
|
+
def extract_association_params(scope_or_options, remaining_options)
|
182
185
|
if scope_or_options.is_a?(Hash)
|
183
|
-
[nil, scope_or_options]
|
186
|
+
[nil, scope_or_options.merge(remaining_options)]
|
184
187
|
else
|
185
|
-
[scope_or_options,
|
188
|
+
[scope_or_options, remaining_options]
|
186
189
|
end
|
187
190
|
end
|
188
191
|
end
|
data/lib/sortability/version.rb
CHANGED
@@ -7,6 +7,9 @@ require "sortability"
|
|
7
7
|
|
8
8
|
module Dummy
|
9
9
|
class Application < Rails::Application
|
10
|
+
# Initialize configuration defaults for originally generated Rails version.
|
11
|
+
config.load_defaults 5.2
|
12
|
+
|
10
13
|
# Settings in config/environments/* take precedence over those specified here.
|
11
14
|
# Application configuration should go into files in config/initializers
|
12
15
|
# -- all .rb files in that directory are automatically loaded.
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class CreateItems < ActiveRecord::Migration[5.2]
|
2
|
+
def change
|
3
|
+
create_table :items do |t|
|
4
|
+
t.references :container, null: false, index: false, foreign_key: true
|
5
|
+
t.sortable
|
6
|
+
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
|
10
|
+
add_sortable_index :items, scope: :container_id
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# This file is auto-generated from the current state of the database. Instead
|
2
|
+
# of editing this file, please use the migrations feature of Active Record to
|
3
|
+
# incrementally modify your database, and then regenerate this schema definition.
|
4
|
+
#
|
5
|
+
# Note that this schema.rb definition is the authoritative source for your
|
6
|
+
# database schema. If you need to create the application database on another
|
7
|
+
# system, you should be using db:schema:load, not running all the migrations
|
8
|
+
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
9
|
+
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
10
|
+
#
|
11
|
+
# It's strongly recommended that you check this file into your version control system.
|
12
|
+
|
13
|
+
ActiveRecord::Schema.define(version: 2019_05_23_235430) do
|
14
|
+
|
15
|
+
create_table "containers", force: :cascade do |t|
|
16
|
+
t.integer "sort_position", null: false
|
17
|
+
t.datetime "created_at", null: false
|
18
|
+
t.datetime "updated_at", null: false
|
19
|
+
end
|
20
|
+
|
21
|
+
create_table "items", force: :cascade do |t|
|
22
|
+
t.integer "container_id", null: false
|
23
|
+
t.integer "sort_position", null: false
|
24
|
+
t.datetime "created_at", null: false
|
25
|
+
t.datetime "updated_at", null: false
|
26
|
+
t.index ["container_id", "sort_position"], name: "index_items_on_container_id_and_sort_position", unique: true
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/spec/dummy/db/test.sqlite3
CHANGED
Binary file
|