active_record_sorting 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -11
- data/lib/active_record_sorting/concern.rb +14 -0
- data/lib/active_record_sorting/version.rb +1 -1
- data/lib/active_record_sorting.rb +1 -0
- data/spec/active_record_sorting/concern_spec.rb +33 -0
- data/spec/fixtures/group.rb +1 -0
- data/spec/fixtures/user.rb +2 -0
- data/spec/fixtures/user_sorting.rb +2 -0
- data/spec/spec_helper.rb +1 -0
- metadata +6 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b0efa3c2c9fab95170e8bf7537bc3bb8bc4b140
|
4
|
+
data.tar.gz: f301b09466d7025f72c0f58679d6dfbcdb5b78f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b1afc2ed2fc414adc4bf723f80128966510583d097820972a6e16aa14a77e6cf8dc376107e42d1106d889b6571099696a49698a6405b07b7b0798605ae57e88
|
7
|
+
data.tar.gz: f2d054dec6857fdaa0b595c164f709575d409c4b6f7c52af872a474ae7ed1b0e9d8659d70de863bf16704317643830894d55dabefbb3020abd3a821c667bdaaf
|
data/README.md
CHANGED
@@ -27,8 +27,7 @@ But if you care about you code being clean and follow single responsibility prin
|
|
27
27
|
|
28
28
|
1. Create sorting class:
|
29
29
|
```ruby
|
30
|
-
# app/sortings
|
31
|
-
|
30
|
+
# app/sortings/user_sorting
|
32
31
|
class UserSorting < ActiveRecordSorting::Base
|
33
32
|
named_sorting_orders :full_name
|
34
33
|
|
@@ -40,17 +39,17 @@ But if you care about you code being clean and follow single responsibility prin
|
|
40
39
|
2. In your controller:
|
41
40
|
|
42
41
|
```ruby
|
43
|
-
|
44
|
-
|
45
|
-
|
42
|
+
UserSorting.sort(user_scope, 'id_asc').another_scope
|
43
|
+
UserSorting.sort(user_scope, 'created_at_desc')
|
44
|
+
UserSorting.sort(user_scope, 'relation.column_asc')
|
45
|
+
UserSorting.sort(user_scope, 'full_name_asc')
|
46
|
+
UserSorting.sort(user_scope, 'any_column_desc')
|
46
47
|
```
|
47
|
-
Example values of `sort` param: `'id_asc', 'created_at_desc', 'relation.column_asc', 'named_order_asc', 'column_desc'`, etc.
|
48
48
|
|
49
49
|
You can also define basic sorting class if you don't want to create it for each model:
|
50
50
|
|
51
51
|
```ruby
|
52
|
-
# app/
|
53
|
-
|
52
|
+
# app/sortings/sorting
|
54
53
|
class Sorting < ActiveRecordSorting::Base
|
55
54
|
end
|
56
55
|
```
|
@@ -66,9 +65,7 @@ If you still want to access sort from your model just do the following:
|
|
66
65
|
|
67
66
|
```ruby
|
68
67
|
class User < ActiveRecord::Base
|
69
|
-
|
70
|
-
UserSorting.sort(self, order)
|
71
|
-
end
|
68
|
+
include ActiveRecordSorting::Concern
|
72
69
|
end
|
73
70
|
|
74
71
|
# Usage
|
@@ -77,6 +74,8 @@ User.sort(params[:sort])
|
|
77
74
|
|
78
75
|
```
|
79
76
|
|
77
|
+
It will instantiate `UserSorting` if it is present, base sorting class otherwise.
|
78
|
+
|
80
79
|
## Contributing
|
81
80
|
|
82
81
|
1. Fork it
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module ActiveRecordSorting
|
4
|
+
module Concern
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
def self.sort(order)
|
9
|
+
klass = "#{self.name}Sorting".constantize rescue ActiveRecordSorting::Base
|
10
|
+
klass.new(self).sort(order)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveRecordSorting::Concern do
|
4
|
+
let(:scope) { double }
|
5
|
+
|
6
|
+
context 'custom sorting class exists' do
|
7
|
+
subject { User }
|
8
|
+
let(:sorting_class) { UserSorting }
|
9
|
+
|
10
|
+
it 'should delegate sorting to sorting class' do
|
11
|
+
expected_to_sort_with 'name_asc', sorting_class
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'custom sorting class doesnt exists' do
|
16
|
+
subject { Group }
|
17
|
+
let(:sorting_class) { ActiveRecordSorting::Base }
|
18
|
+
|
19
|
+
it 'should delegate sorting to base sorting class' do
|
20
|
+
expected_to_sort_with 'name_asc', sorting_class
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def expected_to_sort_with(order, sorting_class)
|
27
|
+
expect(sorting_class).to receive(:new).with(subject).and_call_original
|
28
|
+
expect_any_instance_of(sorting_class).
|
29
|
+
to receive(:sort).with(order).and_return scope
|
30
|
+
|
31
|
+
expect(subject.sort(order)).to eq scope
|
32
|
+
end
|
33
|
+
end
|
data/spec/fixtures/group.rb
CHANGED
data/spec/fixtures/user.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_sorting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yratanov
|
@@ -110,11 +110,14 @@ files:
|
|
110
110
|
- active_record_sorting.gemspec
|
111
111
|
- lib/active_record_sorting.rb
|
112
112
|
- lib/active_record_sorting/base.rb
|
113
|
+
- lib/active_record_sorting/concern.rb
|
113
114
|
- lib/active_record_sorting/version.rb
|
114
115
|
- spec/active_record_sorting/base_spec.rb
|
116
|
+
- spec/active_record_sorting/concern_spec.rb
|
115
117
|
- spec/fixtures/group.rb
|
116
118
|
- spec/fixtures/schema.rb
|
117
119
|
- spec/fixtures/user.rb
|
120
|
+
- spec/fixtures/user_sorting.rb
|
118
121
|
- spec/spec_helper.rb
|
119
122
|
homepage: ''
|
120
123
|
licenses:
|
@@ -142,7 +145,9 @@ specification_version: 4
|
|
142
145
|
summary: Extract you sorting logic to separate object
|
143
146
|
test_files:
|
144
147
|
- spec/active_record_sorting/base_spec.rb
|
148
|
+
- spec/active_record_sorting/concern_spec.rb
|
145
149
|
- spec/fixtures/group.rb
|
146
150
|
- spec/fixtures/schema.rb
|
147
151
|
- spec/fixtures/user.rb
|
152
|
+
- spec/fixtures/user_sorting.rb
|
148
153
|
- spec/spec_helper.rb
|