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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8de084b141765cc5613a436a6573b6a370d535c3
4
- data.tar.gz: 20c7a9f1951fa31c5840cb12c8046d2c734536ce
2
+ SHA256:
3
+ metadata.gz: e811a6edc06dbfee9c4432f0b81eec7a36aec4cf07e40e5604f397f84a62007f
4
+ data.tar.gz: 172646fbe4de26f3def8354d671378a9fc5d6faea2b95757d0973c06c6e9de43
5
5
  SHA512:
6
- metadata.gz: 5d1313f9b10346d6f2dd1d5506ab6b9a24cba540ff0fdef6b388ed32631e4aaa7cb2c9b5a0949971a6dd7e19e26020d2023a18a78036d7f0e8f4d066398d3ced
7
- data.tar.gz: a9fb6ac2194be7a270f51d5bdf6a48a781389f3365d4c14b239d3ab028cbaed4952e8ae449766349a8674037bb0be6c548e0ed001e96055b470bd41c1308b1c0
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
- peers.where{__send__(on) >= val}.reorder(nil)
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{__send__(on) < 0}.reorder(nil)
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{__send__(on) > val}.first
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{__send__(on) < val}.last
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, options_with_scope = {}, &extension)
127
- scope, options = extract_association_params(scope_or_options, options_with_scope)
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
- options_with_scope = {}, &extension)
139
- scope, options = extract_association_params(scope_or_options, options_with_scope)
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, options_with_scope)
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, options_with_scope]
188
+ [scope_or_options, remaining_options]
186
189
  end
187
190
  end
188
191
  end
@@ -1,3 +1,3 @@
1
1
  module Sortability
2
- VERSION = "0.1.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -0,0 +1,3 @@
1
+ //= link_tree ../images
2
+ //= link_directory ../javascripts .js
3
+ //= link_directory ../stylesheets .css
@@ -0,0 +1,3 @@
1
+ class ApplicationRecord < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ end
@@ -0,0 +1,5 @@
1
+ class Container < ApplicationRecord
2
+ sortable_class
3
+
4
+ sortable_has_many :items, inverse_of: :container
5
+ end
@@ -0,0 +1,3 @@
1
+ class Item < ApplicationRecord
2
+ sortable_belongs_to :container, inverse_of: :items
3
+ end
@@ -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,9 @@
1
+ class CreateContainers < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :containers do |t|
4
+ t.sortable
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -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
Binary file