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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 579a5bb1070dd756f977a6d671a463f18b5481f4
4
- data.tar.gz: 3dd04eb291ac4bd254ba440e120dee92583fc95d
3
+ metadata.gz: 8a43a19779df7ff3847545f12c4581dade2c4852
4
+ data.tar.gz: 2fabbb3ef162fa77e2bf7c090f79dc18b9b8bbdc
5
5
  SHA512:
6
- metadata.gz: 08203d0b5ba84d75481b2112d514389c8bec1dec4112b2031065769fd53216bd432c835971b960dc34b961fbcc97daef29fc4eb9dd562966dfd1f406504fe66a
7
- data.tar.gz: dde8dab18591264e6f229b0f3c5c3a03a92c2e4289ebf4bce1a293a927ac62bdadca7270f4c69b2e3ab2e47f0305c415f1d70bb7944500897ce042ea836705e5
6
+ metadata.gz: 4fa64ad8cac6a90c6886f43c7cd8b69058aaf30e2591fdd6bebb22cbecf4ce00d67f4bbf73755b7f997838dbcc579a79dc4bab12421527fa906bb8ea8034385a
7
+ data.tar.gz: 1b3e627ef50acdb7fb72441fed42861b9ce1d0619274005c0218d6a957fa53bb01cb2af04cc0018e14855bd66554d930765be7dcf2bce4fc7daf3ca58ee86fd4
data/README.md CHANGED
@@ -1,2 +1,193 @@
1
- sortability
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 = "prev_by_#{onname}"
21
+ prev_by_mname = "previous_by_#{onname}"
22
22
  compact_peers_mname = "compact_#{onname}_peers"
23
23
 
24
24
  class_exec do
@@ -1,3 +1,3 @@
1
1
  module Sortability
2
- VERSION = "0.0.0"
2
+ VERSION = "0.0.1"
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.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-04 00:00:00.000000000 Z
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 reorder
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: []