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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d95760abdb45a152cee923d24c16f775f4b5814
4
- data.tar.gz: 3588d1042821d5b172c5706d8960530eb9e9720d
3
+ metadata.gz: 5b0efa3c2c9fab95170e8bf7537bc3bb8bc4b140
4
+ data.tar.gz: f301b09466d7025f72c0f58679d6dfbcdb5b78f0
5
5
  SHA512:
6
- metadata.gz: 9caa292897399d8b5608783d29a955a8aebdb2e73906c2ec8b9cd5d031ef71944b704437eac6a3b1e3c0bb56279b7e24fbc18b9a67bc76169e4cf168091ca0bc
7
- data.tar.gz: 5b45e88eac5991161b9bd97060285a7c635f7337ad6c2372e29f30778781414d0a3dc3f45265a67a9370d2cec9711d1a0e005ca7339dbfb2b8941d04257aff0e
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
- def index
44
- @users = UserSorting.sort(User, params[:sort]).page(params[:page])
45
- end
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/sorting/sorting
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
- def self.sort(order)
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
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordSorting
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,5 +1,6 @@
1
1
  require "active_record_sorting/version"
2
2
  require "active_record_sorting/base"
3
+ require "active_record_sorting/concern"
3
4
 
4
5
  module ActiveRecordSorting
5
6
  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
@@ -1,2 +1,3 @@
1
1
  class Group < ActiveRecord::Base
2
+ include ActiveRecordSorting::Concern
2
3
  end
@@ -1,3 +1,5 @@
1
1
  class User < ActiveRecord::Base
2
+ include ActiveRecordSorting::Concern
3
+
2
4
  belongs_to :group
3
5
  end
@@ -0,0 +1,2 @@
1
+ class UserSorting < ActiveRecordSorting::Base
2
+ end
data/spec/spec_helper.rb CHANGED
@@ -6,3 +6,4 @@ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
6
6
  require 'fixtures/schema'
7
7
  require 'fixtures/group'
8
8
  require 'fixtures/user'
9
+ require 'fixtures/user_sorting'
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.3
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