sortability 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +193 -2
- data/lib/sortability/active_record/base.rb +1 -1
- data/lib/sortability/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a43a19779df7ff3847545f12c4581dade2c4852
|
4
|
+
data.tar.gz: 2fabbb3ef162fa77e2bf7c090f79dc18b9b8bbdc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4fa64ad8cac6a90c6886f43c7cd8b69058aaf30e2591fdd6bebb22cbecf4ce00d67f4bbf73755b7f997838dbcc579a79dc4bab12421527fa906bb8ea8034385a
|
7
|
+
data.tar.gz: 1b3e627ef50acdb7fb72441fed42861b9ce1d0619274005c0218d6a957fa53bb01cb2af04cc0018e14855bd66554d930765be7dcf2bce4fc7daf3ca58ee86fd4
|
data/README.md
CHANGED
@@ -1,2 +1,193 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# Sortability
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/sortability.svg)](http://badge.fury.io/rb/sortability)
|
4
|
+
[![Build Status](https://travis-ci.org/openstax/sortability.svg?branch=master)](https://travis-ci.org/openstax/sortability)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/openstax/sortability.png)](https://codeclimate.com/github/openstax/sortability)
|
6
|
+
|
7
|
+
Sortability is a gem that makes it easy to manage records
|
8
|
+
that can be sorted and reordered by users of your Rails app.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```rb
|
15
|
+
gem 'sortability'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
```sh
|
21
|
+
$ bundle install
|
22
|
+
```
|
23
|
+
|
24
|
+
In the following instructions:
|
25
|
+
- `Record` refers to the model your users are allowed to reorder
|
26
|
+
- `Container` refers to the model that holds ordered `Record`s (e.g. a list)
|
27
|
+
|
28
|
+
### Migrations
|
29
|
+
|
30
|
+
Sortability uses a non-null integer column in your `records` table
|
31
|
+
to store the sort order.
|
32
|
+
It is also a good idea to have a unique index that covers the sort column
|
33
|
+
and any `container_id` or `container_type` columns.
|
34
|
+
By default, the sort column is named `sort_position`, but that name can be
|
35
|
+
changed by passing the `on` option to the methods provided by this gem.
|
36
|
+
This will also change the names of the methods created on the `Record` model.
|
37
|
+
The `scope` option in the following methods specifies the container foreign
|
38
|
+
key column(s). You can ommit it if the `records` should be sorted globally.
|
39
|
+
|
40
|
+
#### Existing Tables
|
41
|
+
|
42
|
+
If you don't already have this column, you will need to add it
|
43
|
+
to the `records` table using a migration:
|
44
|
+
|
45
|
+
```sh
|
46
|
+
$ rails g migration add_sort_position_to_records
|
47
|
+
```
|
48
|
+
|
49
|
+
In this migration, you will want something similar to this:
|
50
|
+
|
51
|
+
```rb
|
52
|
+
class AddSortPositionToRecords < ActiveRecord::Migration
|
53
|
+
def change
|
54
|
+
add_sortable_column :records # , on: :sort_position
|
55
|
+
add_sortable_index :records, scope: :container_id # , on: :sort_position
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
#### New Tables
|
61
|
+
|
62
|
+
If you haven't created the `records` table yet, you can use the `sortable`
|
63
|
+
method to create the appropriate column in the new table,
|
64
|
+
but you should still create the index using `add_sortable_index`
|
65
|
+
to ensure that the index covers the appropriate column(s):
|
66
|
+
|
67
|
+
```rb
|
68
|
+
class CreateRecords < ActiveRecord::Migration
|
69
|
+
def change
|
70
|
+
create_table :records do |t|
|
71
|
+
t.sortable # on: :sort_position
|
72
|
+
end
|
73
|
+
|
74
|
+
add_sortable_index :records, scope: :container_id # , on: :sort_position
|
75
|
+
end
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
### Models
|
80
|
+
|
81
|
+
#### Record
|
82
|
+
|
83
|
+
Replace the `belongs_to :container` relation in your `Record` model with:
|
84
|
+
|
85
|
+
```rb
|
86
|
+
sortable_belongs_to :container, inverse_of: :records,
|
87
|
+
scope: :container_id # , on: :sort_position
|
88
|
+
```
|
89
|
+
|
90
|
+
It is highly recommended that you specify the `inverse_of` and `scope` options.
|
91
|
+
|
92
|
+
If `records` are sorted globally, without a `container`,
|
93
|
+
use the `sortable_class` method instead:
|
94
|
+
|
95
|
+
```rb
|
96
|
+
sortable_class # on: :sort_position, scope: :sort_group_number
|
97
|
+
```
|
98
|
+
|
99
|
+
#### Container
|
100
|
+
|
101
|
+
Simply replace the `has_many :records` relation in your `Container` model with:
|
102
|
+
|
103
|
+
```rb
|
104
|
+
sortable_has_many :records, inverse_of: :container # , on: :sort_position
|
105
|
+
```
|
106
|
+
|
107
|
+
## Usage
|
108
|
+
|
109
|
+
Once you have run the migrations and modified your models according to the installation instructions, you are ready to start sorting the `records`.
|
110
|
+
Here are some things that you can do:
|
111
|
+
|
112
|
+
- Get all the `records` in order directly from the relation in the `container`
|
113
|
+
(or from `Record.all` if the `records` are globally sorted).
|
114
|
+
|
115
|
+
- Get all peers of a `record` (`records` in the same `container`)
|
116
|
+
by using the `sort_position_peers` method.
|
117
|
+
|
118
|
+
- Create a new `container` with several `records` with one call to
|
119
|
+
`container.save` and have all the records receive valid `sort_position`s.
|
120
|
+
|
121
|
+
- Add a new `record` to an existing `container` and have it automatically
|
122
|
+
appended at the end of the list.
|
123
|
+
|
124
|
+
- Set the `sort_position` for a `record` and have other `records`
|
125
|
+
in the same `container` be automatically updated to create a gap
|
126
|
+
when that `record` is saved.
|
127
|
+
|
128
|
+
- Change a `record`'s `container` and have other `records` in the new
|
129
|
+
`container` also be automatically updated to create a gap for that `record`.
|
130
|
+
|
131
|
+
- Close all gaps in the `sort_position` for the peers of a `record`
|
132
|
+
by calling the `compact_sort_position_peers` method.
|
133
|
+
|
134
|
+
- Get the next or previous record by using the `next_by_sort_position` or
|
135
|
+
`previous_by_sort_position` methods.
|
136
|
+
|
137
|
+
The `sort_position` is not guaranteed to contain consecutive numbers.
|
138
|
+
When listing the `records`, you should compute their positions
|
139
|
+
in application code as you iterate through the list.
|
140
|
+
If you need the position for a single `record`, call
|
141
|
+
`compact_sort_position_peers` first to close any gaps
|
142
|
+
in its peers, then read `sort_position` directly.
|
143
|
+
|
144
|
+
## Contributing
|
145
|
+
|
146
|
+
1. Fork it
|
147
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
148
|
+
3. Write specs for your feature
|
149
|
+
4. Implement your new feature
|
150
|
+
5. Test your feature (`rake`)
|
151
|
+
6. Commit your changes (`git commit -am 'Added some feature'`)
|
152
|
+
7. Push to the branch (`git push origin my-new-feature`)
|
153
|
+
8. Create new Pull Request
|
154
|
+
|
155
|
+
## Development Environment Setup
|
156
|
+
|
157
|
+
1. Use bundler to install all dependencies:
|
158
|
+
|
159
|
+
```sh
|
160
|
+
$ bundle install
|
161
|
+
```
|
162
|
+
|
163
|
+
2. Load the schema:
|
164
|
+
|
165
|
+
```sh
|
166
|
+
$ rake db:schema:load
|
167
|
+
```
|
168
|
+
|
169
|
+
Or if the above fails:
|
170
|
+
|
171
|
+
```sh
|
172
|
+
$ bundle exec rake db:schema:load
|
173
|
+
```
|
174
|
+
|
175
|
+
## Testing
|
176
|
+
|
177
|
+
To run all existing tests for Sortability,
|
178
|
+
simply execute the following from the main folder:
|
179
|
+
|
180
|
+
```sh
|
181
|
+
$ rake
|
182
|
+
```
|
183
|
+
|
184
|
+
Or if the above fails:
|
185
|
+
|
186
|
+
```sh
|
187
|
+
$ bundle exec rake
|
188
|
+
```
|
189
|
+
|
190
|
+
## License
|
191
|
+
|
192
|
+
This gem is distributed under the terms of the MIT license.
|
193
|
+
See the MIT-LICENSE file for details.
|
@@ -18,7 +18,7 @@ module Sortability
|
|
18
18
|
peers_mname = "#{onname}_peers"
|
19
19
|
before_validation_mname = "#{onname}_before_validation"
|
20
20
|
next_by_mname = "next_by_#{onname}"
|
21
|
-
prev_by_mname = "
|
21
|
+
prev_by_mname = "previous_by_#{onname}"
|
22
22
|
compact_peers_mname = "compact_#{onname}_peers"
|
23
23
|
|
24
24
|
class_exec do
|
data/lib/sortability/version.rb
CHANGED
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.
|
4
|
+
version: 0.0.1
|
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-
|
12
|
+
date: 2014-12-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -53,8 +53,8 @@ dependencies:
|
|
53
53
|
- - ">="
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '0'
|
56
|
-
description: Provides ActiveRecord methods that make it easy to allow users to
|
57
|
-
records
|
56
|
+
description: Provides ActiveRecord methods that make it easy to allow users to sort
|
57
|
+
and reorder records in a list
|
58
58
|
email:
|
59
59
|
- Dante.M.Soares@rice.edu
|
60
60
|
executables: []
|