model_sorter 0.0.2 → 0.0.3
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 +4 -4
- data/README.md +26 -11
- data/lib/model_sorter/version.rb +1 -1
- data/lib/model_sorter.rb +45 -4
- data/spec/model_sorter_spec.rb +7 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10a92e51ba86308774599943ab345b332fa02cc8
|
4
|
+
data.tar.gz: e41dd8beaf3450599d40464f95bd6d18c40ed2b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 566d316f4d810ca4417db6100c2850c55ddc1e9dcf653e3ce5050c107bb62032060b249693eb98168ad59c6dd8721bfdc04eaed84267aa41dd344eb7f158f623
|
7
|
+
data.tar.gz: 19e0c2b0cea499e7cf3804b1691ec8f69576cc62e10f580538698f41201125398904b974d2ad6308523596f817a757522d0220e2cc68e3dc915fc1be95cc117f
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# ModelSorter
|
2
2
|
|
3
|
-
用
|
3
|
+
用 Redis 来支持 ActiveRecord 或 DataMapper 的等 Ruby ORM 对象排序,免去在文件数据库中使用排序字段。
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -8,12 +8,16 @@ Add this line to your application's Gemfile:
|
|
8
8
|
|
9
9
|
gem 'model_sorter'
|
10
10
|
|
11
|
-
add this to 'config/initializes/model_sorter.rb'
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
Name your column, add this to 'config/initializes/model_sorter.rb'
|
12
|
+
```
|
13
|
+
module ModelSorter
|
14
|
+
SORT_COLUMN = "what_column_you_want"
|
15
|
+
end
|
16
|
+
```
|
15
17
|
## Usage
|
16
18
|
|
19
|
+
As:需要 View 中配置 Post 结果集的对象排序(用 jquery-ui.sortable 等组件)。
|
20
|
+
|
17
21
|
__In your Model__
|
18
22
|
|
19
23
|
```
|
@@ -25,13 +29,19 @@ end
|
|
25
29
|
|
26
30
|
__In your Coffee__
|
27
31
|
|
28
|
-
组装出Hash
|
32
|
+
组装出 Hash
|
33
|
+
|
34
|
+
hsh: { id: index, id: index, ... }
|
35
|
+
|
36
|
+
或 Array, 按你希望的顺序由小到大
|
37
|
+
|
38
|
+
arr: [ id, id, ... ]
|
29
39
|
|
30
40
|
```
|
31
41
|
$.ajax(
|
32
42
|
type: '...',
|
33
43
|
url: '...',
|
34
|
-
data: {
|
44
|
+
data: { serial_list: [5, 3, 4, 2, ...] },
|
35
45
|
dataType: '...',
|
36
46
|
success: () ->
|
37
47
|
# ...
|
@@ -40,7 +50,7 @@ $.ajax(
|
|
40
50
|
|
41
51
|
__In your Controller__
|
42
52
|
|
43
|
-
用sort_serial_number方法,传入
|
53
|
+
用sort_serial_number方法,传入Hash, Array, ActiveRecord::Relation ...
|
44
54
|
|
45
55
|
```
|
46
56
|
def index
|
@@ -48,8 +58,11 @@ def index
|
|
48
58
|
end
|
49
59
|
|
50
60
|
def update_serial_number
|
51
|
-
|
52
|
-
|
61
|
+
serial_list = params[:serial_list]
|
62
|
+
|
63
|
+
# if Post.sort_serial_number(Post.all)
|
64
|
+
|
65
|
+
if Post.sort_serial_number(serial_list)
|
53
66
|
return render text: "success"
|
54
67
|
else
|
55
68
|
return render text: "fail"
|
@@ -59,10 +72,12 @@ end
|
|
59
72
|
|
60
73
|
## Note
|
61
74
|
|
62
|
-
出现这个错误?请再看 Installation
|
75
|
+
1. 出现这个错误?请再看 Installation
|
63
76
|
|
64
77
|
uninitialized constant ModelSorter::SORT_COLUMN (NameError)
|
65
78
|
|
79
|
+
2. 使用 DataMapper 需有 id 字段
|
80
|
+
|
66
81
|
|
67
82
|
|
68
83
|
|
data/lib/model_sorter/version.rb
CHANGED
data/lib/model_sorter.rb
CHANGED
@@ -8,11 +8,52 @@ module ModelSorter
|
|
8
8
|
#定义counter
|
9
9
|
counter ModelSorter::SORT_COLUMN.to_sym
|
10
10
|
|
11
|
-
#
|
12
|
-
|
11
|
+
# Usage
|
12
|
+
#
|
13
|
+
# hsh 格式: {"id" => "index" ...}
|
14
|
+
# hsh = {2:1, 3:2, 1:3}
|
15
|
+
# Klass.sort_serial_number hsh
|
16
|
+
# =>
|
17
|
+
# id | index
|
18
|
+
# 1 | 3
|
19
|
+
# 2 | 1
|
20
|
+
# 3 | 2
|
21
|
+
#
|
22
|
+
# ---------------------------------
|
23
|
+
#
|
24
|
+
# arr 格式: ["id", "id" ...]
|
25
|
+
# arr = [1, 2, 3]
|
26
|
+
# Klass.sort_serial_number arr
|
27
|
+
# =>
|
28
|
+
# id | index
|
29
|
+
# 1 | 1
|
30
|
+
# 2 | 2
|
31
|
+
# 3 | 3
|
32
|
+
#
|
33
|
+
# ---------------------------------
|
34
|
+
#
|
35
|
+
# 使用ActiveRecord::Relation的实例
|
36
|
+
# relation_instance = Klass.all
|
37
|
+
# Klass.sort_serial_number relation_instance
|
38
|
+
# =>
|
39
|
+
# id | index
|
40
|
+
# 1 | 1
|
41
|
+
# 2 | 2
|
42
|
+
# 3 | 3
|
43
|
+
#
|
44
|
+
# 用Redis批量写入所有klass对象counter
|
45
|
+
def self.sort_serial_number serial_list
|
13
46
|
results = self.redis.multi do
|
14
|
-
|
15
|
-
|
47
|
+
case
|
48
|
+
when serial_list.is_a?(Hash)
|
49
|
+
serial_list.each do |id, sn|
|
50
|
+
self.redis.set "#{self.name.downcase}:#{id}:#{ModelSorter::SORT_COLUMN}", sn.to_i
|
51
|
+
end
|
52
|
+
else
|
53
|
+
#Handle Array and ActiveRecord::Relation
|
54
|
+
serial_list.each_with_index do |id, index|
|
55
|
+
self.redis.set "#{self.name.downcase}:#{id}:#{ModelSorter::SORT_COLUMN}", index + 1
|
56
|
+
end
|
16
57
|
end
|
17
58
|
end
|
18
59
|
results.uniq == ["OK"]
|
data/spec/model_sorter_spec.rb
CHANGED
@@ -33,7 +33,6 @@ describe ModelSorter do
|
|
33
33
|
@hsh[i] = 6 - i
|
34
34
|
i += 1
|
35
35
|
end
|
36
|
-
|
37
36
|
end
|
38
37
|
|
39
38
|
it "set User's all serial_number to redis" do
|
@@ -45,4 +44,11 @@ describe ModelSorter do
|
|
45
44
|
expect(arr).to eq([5, 4, 3, 2, 1])
|
46
45
|
end
|
47
46
|
|
47
|
+
it "handle serial_number is Array" do
|
48
|
+
User.sort_serial_number @hsh.keys
|
49
|
+
|
50
|
+
arr = @users.sort_by{ |u| u.ex_column.value }.map{|u| u.id}
|
51
|
+
expect(arr).to eq([1, 2, 3, 4, 5])
|
52
|
+
end
|
53
|
+
|
48
54
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: model_sorter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott1743
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis-objects
|