restpack_serializer 0.6.7 → 0.6.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/Gemfile +1 -0
- data/Rakefile +1 -0
- data/lib/restpack_serializer.rb +1 -1
- data/lib/restpack_serializer/serializable.rb +17 -5
- data/lib/restpack_serializer/serializable/side_loading.rb +5 -1
- data/lib/restpack_serializer/version.rb +1 -1
- data/performance/mem.rb +49 -0
- data/restpack_serializer.gemspec +8 -9
- data/spec/factory/factory_spec.rb +21 -14
- data/spec/fixtures/db.rb +0 -11
- data/spec/restpack_serializer_spec.rb +4 -4
- data/spec/result_spec.rb +16 -15
- data/spec/serializable/filterable_spec.rb +1 -1
- data/spec/serializable/options_spec.rb +40 -35
- data/spec/serializable/paging_spec.rb +88 -81
- data/spec/serializable/resource_spec.rb +11 -10
- data/spec/serializable/serializer_spec.rb +79 -41
- data/spec/serializable/side_loading/belongs_to_spec.rb +16 -13
- data/spec/serializable/side_loading/has_and_belongs_many_spec.rb +7 -7
- data/spec/serializable/side_loading/has_many_spec.rb +19 -20
- data/spec/serializable/side_loading/side_loading_spec.rb +21 -19
- data/spec/serializable/single_spec.rb +4 -4
- data/spec/serializable/sortable_spec.rb +1 -1
- data/spec/spec_helper.rb +1 -0
- data/spec/support/factory.rb +20 -26
- metadata +31 -40
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 29675ee4cb74f76f0f88dfa4ddb86484cfd5fb7c15e5e0a01ccfa2142f1086c4
|
4
|
+
data.tar.gz: f09ca1d697f23127162ebafbb57f502cb53817cee03f34fb38cbd27861921a1b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca2acc1214a42e32f9ccf8aab6617b7a5c914b7d0f21a5fef64152e0130ababb26844a03bf9489e178f5d32ed13e6f5224a48d6f3e0c710a62ae8ffb22ee70ac
|
7
|
+
data.tar.gz: df71b93f4a14adbdde6f953185b4d105d1f8756cff5fa3f8db3f2e8d65630c7ab53a743515600e901053093955fbebccbfa24c63b390984389a24a13f4126576
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/Rakefile
CHANGED
data/lib/restpack_serializer.rb
CHANGED
@@ -6,7 +6,7 @@ require_relative 'restpack_serializer/serializable'
|
|
6
6
|
require_relative 'restpack_serializer/factory'
|
7
7
|
require_relative 'restpack_serializer/result'
|
8
8
|
|
9
|
-
Kaminari::Hooks.init
|
9
|
+
Kaminari::Hooks.init if defined?(Kaminari::Hooks)
|
10
10
|
|
11
11
|
module RestPack
|
12
12
|
module Serializer
|
@@ -34,7 +34,7 @@ module RestPack
|
|
34
34
|
def as_json(model, context = {})
|
35
35
|
return if model.nil?
|
36
36
|
if model.kind_of?(Array)
|
37
|
-
return model.map { |item| as_json(item, context) }
|
37
|
+
return model.map { |item| self.class.new.as_json(item, context) }
|
38
38
|
end
|
39
39
|
|
40
40
|
apply_whitelist_and_blacklist(context)
|
@@ -57,11 +57,15 @@ module RestPack
|
|
57
57
|
end
|
58
58
|
|
59
59
|
add_custom_attributes(data)
|
60
|
-
add_links(model, data)
|
60
|
+
add_links(model, data) if self.class.has_associations?
|
61
61
|
|
62
62
|
data
|
63
63
|
end
|
64
64
|
|
65
|
+
def to_json(model, context = {})
|
66
|
+
as_json(model, context).to_json
|
67
|
+
end
|
68
|
+
|
65
69
|
def custom_attributes
|
66
70
|
nil
|
67
71
|
end
|
@@ -140,9 +144,13 @@ module RestPack
|
|
140
144
|
end
|
141
145
|
|
142
146
|
def has_user_defined_method?(method_name)
|
143
|
-
user_defined_methods
|
144
|
-
|
145
|
-
|
147
|
+
if user_defined_methods && user_defined_methods.include?(method_name)
|
148
|
+
true
|
149
|
+
elsif superclass.respond_to?(:has_user_defined_method?)
|
150
|
+
superclass.has_user_defined_method?(method_name)
|
151
|
+
else
|
152
|
+
false
|
153
|
+
end
|
146
154
|
end
|
147
155
|
|
148
156
|
def memoized_has_user_defined_method?(method_name)
|
@@ -165,6 +173,10 @@ module RestPack
|
|
165
173
|
new.as_json(model, context)
|
166
174
|
end
|
167
175
|
|
176
|
+
def to_json(model, context = {})
|
177
|
+
new.as_json(model, context).to_json
|
178
|
+
end
|
179
|
+
|
168
180
|
def serialize(models, context = {})
|
169
181
|
models = [models] unless models.kind_of?(Array)
|
170
182
|
|
@@ -44,8 +44,12 @@ module RestPack::Serializer::SideLoading
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
+
def has_associations?
|
48
|
+
@can_includes
|
49
|
+
end
|
50
|
+
|
47
51
|
def associations
|
48
|
-
return [] unless
|
52
|
+
return [] unless has_associations?
|
49
53
|
can_includes.map do |include|
|
50
54
|
association = association_from_include(include)
|
51
55
|
association if supported_association?(association.macro)
|
data/performance/mem.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'memory_profiler'
|
2
|
+
require_relative '../lib/restpack_serializer'
|
3
|
+
|
4
|
+
class SimpleSerializer
|
5
|
+
include RestPack::Serializer
|
6
|
+
attributes :id, :title
|
7
|
+
end
|
8
|
+
|
9
|
+
simple_model = {
|
10
|
+
id: "123",
|
11
|
+
title: 'This is the title',
|
12
|
+
}
|
13
|
+
|
14
|
+
# warmup
|
15
|
+
SimpleSerializer.as_json(simple_model)
|
16
|
+
|
17
|
+
report = MemoryProfiler.report do
|
18
|
+
SimpleSerializer.as_json(simple_model)
|
19
|
+
end
|
20
|
+
|
21
|
+
puts "="*64
|
22
|
+
puts "Simple Serializer:"
|
23
|
+
puts "="*64
|
24
|
+
|
25
|
+
report.pretty_print(detailed_report: false)
|
26
|
+
|
27
|
+
class ComplexSerializer
|
28
|
+
include RestPack::Serializer
|
29
|
+
|
30
|
+
attributes :a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o, :p, :q, :r, :s, :t
|
31
|
+
end
|
32
|
+
|
33
|
+
complex_model = {
|
34
|
+
a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10,
|
35
|
+
k: 11, l: 12, m: 13, n: 14, o: 15, p: 16, q: 17, r: 18, s: 19, t: 20,
|
36
|
+
}
|
37
|
+
|
38
|
+
# warmup
|
39
|
+
ComplexSerializer.as_json(complex_model)
|
40
|
+
|
41
|
+
report = MemoryProfiler.report do
|
42
|
+
ComplexSerializer.as_json(complex_model)
|
43
|
+
end
|
44
|
+
|
45
|
+
puts "="*64
|
46
|
+
puts "Complex Serializer:"
|
47
|
+
puts "="*64
|
48
|
+
|
49
|
+
report.pretty_print(detailed_report: false)
|
data/restpack_serializer.gemspec
CHANGED
@@ -17,17 +17,16 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
|
20
|
-
gem.add_dependency 'activesupport', ['>= 4.0.3', '<
|
21
|
-
gem.add_dependency 'activerecord', ['>= 4.0.3', '<
|
22
|
-
gem.add_dependency 'kaminari', '
|
20
|
+
gem.add_dependency 'activesupport', ['>= 4.0.3', '< 6.1']
|
21
|
+
gem.add_dependency 'activerecord', ['>= 4.0.3', '< 6.1']
|
22
|
+
gem.add_dependency 'kaminari', ['>= 0.17.0', '< 2.0']
|
23
23
|
|
24
24
|
gem.add_development_dependency 'restpack_gem', '~> 0.0.9'
|
25
|
-
gem.add_development_dependency 'rake', '~> 11.
|
26
|
-
gem.add_development_dependency 'guard-rspec', '~> 4.
|
27
|
-
gem.add_development_dependency 'factory_girl', '~> 4.7
|
28
|
-
gem.add_development_dependency 'sqlite3', '~> 1.3
|
29
|
-
gem.add_development_dependency 'database_cleaner', '~> 1.5
|
25
|
+
gem.add_development_dependency 'rake', '~> 11.3'
|
26
|
+
gem.add_development_dependency 'guard-rspec', '~> 4.7'
|
27
|
+
gem.add_development_dependency 'factory_girl', '~> 4.7'
|
28
|
+
gem.add_development_dependency 'sqlite3', '~> 1.3'
|
29
|
+
gem.add_development_dependency 'database_cleaner', '~> 1.5'
|
30
30
|
gem.add_development_dependency 'rspec'
|
31
31
|
gem.add_development_dependency 'bump'
|
32
|
-
gem.add_development_dependency 'protected_attributes', '~> 1.1.3'
|
33
32
|
end
|
@@ -5,45 +5,52 @@ describe RestPack::Serializer::Factory do
|
|
5
5
|
|
6
6
|
describe "single-word" do
|
7
7
|
it "creates by string" do
|
8
|
-
factory.create("Song").
|
8
|
+
expect(factory.create("Song")).to be_an_instance_of(MyApp::SongSerializer)
|
9
9
|
end
|
10
|
+
|
10
11
|
it "creates by lowercase string" do
|
11
|
-
factory.create("song").
|
12
|
+
expect(factory.create("song")).to be_an_instance_of(MyApp::SongSerializer)
|
12
13
|
end
|
14
|
+
|
13
15
|
it "creates by lowercase plural string" do
|
14
|
-
factory.create("songs").
|
16
|
+
expect(factory.create("songs")).to be_an_instance_of(MyApp::SongSerializer)
|
15
17
|
end
|
18
|
+
|
16
19
|
it "creates by symbol" do
|
17
|
-
factory.create(:song).
|
20
|
+
expect(factory.create(:song)).to be_an_instance_of(MyApp::SongSerializer)
|
18
21
|
end
|
22
|
+
|
19
23
|
it "creates by class" do
|
20
|
-
factory.create(MyApp::Song).
|
24
|
+
expect(factory.create(MyApp::Song)).to be_an_instance_of(MyApp::SongSerializer)
|
21
25
|
end
|
22
26
|
|
23
27
|
it "creates multiple with Array" do
|
24
28
|
serializers = factory.create("Song", "artists", :album)
|
25
|
-
serializers[0].
|
26
|
-
serializers[1].
|
27
|
-
serializers[2].
|
29
|
+
expect(serializers[0]).to be_an_instance_of(MyApp::SongSerializer)
|
30
|
+
expect(serializers[1]).to be_an_instance_of(MyApp::ArtistSerializer)
|
31
|
+
expect(serializers[2]).to be_an_instance_of(MyApp::AlbumSerializer)
|
28
32
|
end
|
29
33
|
end
|
30
34
|
|
31
35
|
describe "multi-word" do
|
32
36
|
it "creates multi-word string" do
|
33
|
-
factory.create("AlbumReview").
|
37
|
+
expect(factory.create("AlbumReview")).to be_an_instance_of(MyApp::AlbumReviewSerializer)
|
34
38
|
end
|
39
|
+
|
35
40
|
it "creates multi-word lowercase string" do
|
36
|
-
factory.create("album_review").
|
41
|
+
expect(factory.create("album_review")).to be_an_instance_of(MyApp::AlbumReviewSerializer)
|
37
42
|
end
|
43
|
+
|
38
44
|
it "creates multi-word lowercase plural string" do
|
39
|
-
factory.create("album_reviews").
|
45
|
+
expect(factory.create("album_reviews")).to be_an_instance_of(MyApp::AlbumReviewSerializer)
|
40
46
|
end
|
47
|
+
|
41
48
|
it "creates multi-word symbol" do
|
42
|
-
factory.create(:album_review).
|
49
|
+
expect(factory.create(:album_review)).to be_an_instance_of(MyApp::AlbumReviewSerializer)
|
43
50
|
end
|
51
|
+
|
44
52
|
it "creates multi-word class" do
|
45
|
-
factory.create(MyApp::AlbumReview).
|
53
|
+
expect(factory.create(MyApp::AlbumReview)).to be_an_instance_of(MyApp::AlbumReviewSerializer)
|
46
54
|
end
|
47
55
|
end
|
48
|
-
|
49
56
|
end
|
data/spec/fixtures/db.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'sqlite3'
|
2
2
|
require 'active_record'
|
3
|
-
require 'protected_attributes'
|
4
3
|
|
5
4
|
ActiveRecord::Base.establish_connection(
|
6
5
|
:adapter => 'sqlite3',
|
@@ -66,8 +65,6 @@ end
|
|
66
65
|
|
67
66
|
module MyApp
|
68
67
|
class Artist < ActiveRecord::Base
|
69
|
-
attr_accessible :name, :website
|
70
|
-
|
71
68
|
has_many :albums
|
72
69
|
has_many :songs
|
73
70
|
has_many :payments
|
@@ -76,7 +73,6 @@ module MyApp
|
|
76
73
|
end
|
77
74
|
|
78
75
|
class Album < ActiveRecord::Base
|
79
|
-
attr_accessible :title, :year, :artist
|
80
76
|
scope :classic, -> { where("year < 1950") }
|
81
77
|
|
82
78
|
belongs_to :artist
|
@@ -85,34 +81,27 @@ module MyApp
|
|
85
81
|
end
|
86
82
|
|
87
83
|
class AlbumReview < ActiveRecord::Base
|
88
|
-
attr_accessible :message
|
89
84
|
belongs_to :album
|
90
85
|
end
|
91
86
|
|
92
87
|
class Song < ActiveRecord::Base
|
93
88
|
default_scope -> { order(id: :asc) }
|
94
89
|
|
95
|
-
attr_accessible :title, :artist, :album
|
96
|
-
|
97
90
|
belongs_to :artist
|
98
91
|
belongs_to :album
|
99
92
|
end
|
100
93
|
|
101
94
|
class Payment < ActiveRecord::Base
|
102
|
-
attr_accessible :amount, :artist
|
103
|
-
|
104
95
|
belongs_to :artist
|
105
96
|
belongs_to :fan
|
106
97
|
end
|
107
98
|
|
108
99
|
class Fan < ActiveRecord::Base
|
109
|
-
attr_accessible :name
|
110
100
|
has_many :payments
|
111
101
|
has_many :artists, :through => :albums
|
112
102
|
end
|
113
103
|
|
114
104
|
class Stalker < ActiveRecord::Base
|
115
|
-
attr_accessible :name
|
116
105
|
has_and_belongs_to_many :artists
|
117
106
|
end
|
118
107
|
end
|
@@ -6,8 +6,8 @@ describe RestPack::Serializer do
|
|
6
6
|
|
7
7
|
context "#setup" do
|
8
8
|
it "has defaults" do
|
9
|
-
subject.config.href_prefix.
|
10
|
-
subject.config.page_size.
|
9
|
+
expect(subject.config.href_prefix).to eq('')
|
10
|
+
expect(subject.config.page_size).to eq(10)
|
11
11
|
end
|
12
12
|
|
13
13
|
it "can be configured" do
|
@@ -16,8 +16,8 @@ describe RestPack::Serializer do
|
|
16
16
|
config.page_size = 50
|
17
17
|
end
|
18
18
|
|
19
|
-
subject.config.href_prefix.
|
20
|
-
subject.config.page_size.
|
19
|
+
expect(subject.config.href_prefix).to eq('/api/v1')
|
20
|
+
expect(subject.config.page_size).to eq(50)
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
data/spec/result_spec.rb
CHANGED
@@ -3,18 +3,19 @@ require 'spec_helper'
|
|
3
3
|
describe RestPack::Serializer::Result do
|
4
4
|
context 'a new instance' do
|
5
5
|
it 'has defaults' do
|
6
|
-
subject.resources.
|
7
|
-
subject.meta.
|
8
|
-
subject.links.
|
6
|
+
expect(subject.resources).to eq({})
|
7
|
+
expect(subject.meta).to eq({})
|
8
|
+
expect(subject.links).to eq({})
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
context 'when serializing' do
|
13
13
|
let(:result) { subject.serialize }
|
14
|
+
|
14
15
|
context 'in jsonapi.org format' do
|
15
16
|
context 'an empty result' do
|
16
17
|
it 'returns an empty result' do
|
17
|
-
result.
|
18
|
+
expect(result).to eq({})
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
@@ -26,9 +27,9 @@ describe RestPack::Serializer::Result do
|
|
26
27
|
end
|
27
28
|
|
28
29
|
it 'returns correct jsonapi.org format' do
|
29
|
-
result[:albums].
|
30
|
-
result[:meta].
|
31
|
-
result[:links].
|
30
|
+
expect(result[:albums]).to eq(subject.resources[:albums])
|
31
|
+
expect(result[:meta]).to eq(subject.meta)
|
32
|
+
expect(result[:links]).to eq(subject.links)
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
@@ -43,16 +44,16 @@ describe RestPack::Serializer::Result do
|
|
43
44
|
end
|
44
45
|
|
45
46
|
it 'returns correct jsonapi.org format, including injected has_many links' do
|
46
|
-
result[:albums].
|
47
|
-
result[:links].
|
48
|
-
result[:linked][:songs].
|
47
|
+
expect(result[:albums]).to eq([{ id: '1', name: 'AMOK', links: { songs: ['91'] } }])
|
48
|
+
expect(result[:links]).to eq(subject.links)
|
49
|
+
expect(result[:linked][:songs]).to eq(subject.resources[:songs])
|
49
50
|
end
|
50
51
|
|
51
52
|
it 'includes resources in correct order' do
|
52
|
-
result.keys[0].
|
53
|
-
result.keys[1].
|
54
|
-
result.keys[2].
|
55
|
-
result.keys[3].
|
53
|
+
expect(result.keys[0]).to eq(:albums)
|
54
|
+
expect(result.keys[1]).to eq(:linked)
|
55
|
+
expect(result.keys[2]).to eq(:links)
|
56
|
+
expect(result.keys[3]).to eq(:meta)
|
56
57
|
end
|
57
58
|
|
58
59
|
context 'with multiple calls to serialize' do
|
@@ -62,7 +63,7 @@ describe RestPack::Serializer::Result do
|
|
62
63
|
end
|
63
64
|
|
64
65
|
it 'does not create duplicate has_many links' do
|
65
|
-
result[:albums].first[:links][:songs].count.
|
66
|
+
expect(result[:albums].first[:links][:songs].count).to eq(1)
|
66
67
|
end
|
67
68
|
end
|
68
69
|
end
|
@@ -6,94 +6,99 @@ describe RestPack::Serializer::Options do
|
|
6
6
|
let(:scope) { nil }
|
7
7
|
|
8
8
|
describe 'default values' do
|
9
|
-
it { subject.model_class.
|
10
|
-
it { subject.include.
|
11
|
-
it { subject.page.
|
12
|
-
it { subject.page_size.
|
13
|
-
it { subject.filters.
|
14
|
-
it { subject.scope.
|
15
|
-
it { subject.default_page_size
|
16
|
-
it { subject.filters_as_url_params.
|
9
|
+
it { expect(subject.model_class).to eq(MyApp::Song) }
|
10
|
+
it { expect(subject.include).to eq([]) }
|
11
|
+
it { expect(subject.page).to eq(1) }
|
12
|
+
it { expect(subject.page_size).to eq(10) }
|
13
|
+
it { expect(subject.filters).to eq({}) }
|
14
|
+
it { expect(subject.scope).to eq(MyApp::Song.all) }
|
15
|
+
it { expect(subject.default_page_size?).to eq(true) }
|
16
|
+
it { expect(subject.filters_as_url_params).to eq('') }
|
17
17
|
end
|
18
18
|
|
19
19
|
describe 'with paging params' do
|
20
20
|
let(:params) { { 'page' => '2', 'page_size' => '8' } }
|
21
|
-
it { subject.page.
|
22
|
-
it { subject.page_size.
|
21
|
+
it { expect(subject.page).to eq(2) }
|
22
|
+
it { expect(subject.page_size).to eq(8) }
|
23
23
|
end
|
24
24
|
|
25
25
|
describe 'with include' do
|
26
26
|
let(:params) { { 'include' => 'model1,model2' } }
|
27
|
-
it { subject.include.
|
27
|
+
it { expect(subject.include).to eq(%w(model1 model2)) }
|
28
28
|
end
|
29
29
|
|
30
30
|
context 'with filters' do
|
31
31
|
describe 'with no filter params' do
|
32
|
-
let(:params) { {
|
33
|
-
it { subject.filters.
|
32
|
+
let(:params) { {} }
|
33
|
+
it { expect(subject.filters).to eq({}) }
|
34
34
|
end
|
35
|
+
|
35
36
|
describe 'with a primary key with a single value' do
|
36
37
|
let(:params) { { 'id' => '142857' } }
|
37
|
-
it { subject.filters.
|
38
|
-
it { subject.filters_as_url_params.
|
38
|
+
it { expect(subject.filters).to eq(id: %w(142857)) }
|
39
|
+
it { expect(subject.filters_as_url_params).to eq('id=142857') }
|
39
40
|
end
|
41
|
+
|
40
42
|
describe 'with a primary key with multiple values' do
|
41
43
|
let(:params) { { 'ids' => '42,142857' } }
|
42
|
-
it { subject.filters.
|
43
|
-
it { subject.filters_as_url_params.
|
44
|
+
it { expect(subject.filters).to eq(id: %w(42 142857)) }
|
45
|
+
it { expect(subject.filters_as_url_params).to eq('id=42,142857') }
|
44
46
|
end
|
47
|
+
|
45
48
|
describe 'with a foreign key with a single value' do
|
46
49
|
let(:params) { { 'album_id' => '789' } }
|
47
|
-
it { subject.filters.
|
48
|
-
it { subject.filters_as_url_params.
|
50
|
+
it { expect(subject.filters).to eq(album_id: %w(789)) }
|
51
|
+
it { expect(subject.filters_as_url_params).to eq('album_id=789') }
|
49
52
|
end
|
53
|
+
|
50
54
|
describe 'with a foreign key with multiple values' do
|
51
55
|
let(:params) { { 'album_id' => '789,678,567' } }
|
52
|
-
it { subject.filters.
|
53
|
-
it { subject.filters_as_url_params.
|
56
|
+
it { expect(subject.filters).to eq(album_id: %w(789 678 567)) }
|
57
|
+
it { expect(subject.filters_as_url_params).to eq('album_id=789,678,567') }
|
54
58
|
end
|
59
|
+
|
55
60
|
describe 'with multiple foreign keys' do
|
56
61
|
let(:params) { { 'album_id' => '111,222', 'artist_id' => '888,999' } }
|
57
|
-
it { subject.filters.
|
58
|
-
it { subject.filters_as_url_params.
|
62
|
+
it { expect(subject.filters).to eq(album_id: %w(111 222), artist_id: %w(888 999)) }
|
63
|
+
it { expect(subject.filters_as_url_params).to eq('album_id=111,222&artist_id=888,999') }
|
59
64
|
end
|
60
65
|
end
|
61
66
|
|
62
67
|
context 'with sorting parameters' do
|
63
68
|
describe 'with no params' do
|
64
|
-
let(:params) { {
|
65
|
-
it { subject.sorting.
|
69
|
+
let(:params) { {} }
|
70
|
+
it { expect(subject.sorting).to eq({}) }
|
66
71
|
end
|
67
72
|
describe 'with a sorting value' do
|
68
73
|
let(:params) { { 'sort' => 'Title' } }
|
69
|
-
it { subject.sorting.
|
70
|
-
it { subject.sorting_as_url_params.
|
74
|
+
it { expect(subject.sorting).to eq(title: :asc) }
|
75
|
+
it { expect(subject.sorting_as_url_params).to eq('sort=title') }
|
71
76
|
end
|
72
77
|
describe 'with a descending sorting value' do
|
73
78
|
let(:params) { { 'sort' => '-title' } }
|
74
|
-
it { subject.sorting.
|
75
|
-
it { subject.sorting_as_url_params.
|
79
|
+
it { expect(subject.sorting).to eq(title: :desc) }
|
80
|
+
it { expect(subject.sorting_as_url_params).to eq('sort=-title') }
|
76
81
|
end
|
77
82
|
describe 'with multiple sorting values' do
|
78
83
|
let(:params) { { 'sort' => '-Title,ID' } }
|
79
|
-
it { subject.sorting.
|
80
|
-
it { subject.sorting_as_url_params.
|
84
|
+
it { expect(subject.sorting).to eq(title: :desc, id: :asc) }
|
85
|
+
it { expect(subject.sorting_as_url_params).to eq('sort=-title,id') }
|
81
86
|
end
|
82
87
|
describe 'with a not allowed sorting value' do
|
83
88
|
let(:params) { { 'sort' => '-title,album_id,id' } }
|
84
|
-
it { subject.sorting.
|
85
|
-
it { subject.sorting_as_url_params.
|
89
|
+
it { expect(subject.sorting).to eq(title: :desc, id: :asc) }
|
90
|
+
it { expect(subject.sorting_as_url_params).to eq('sort=-title,id') }
|
86
91
|
end
|
87
92
|
end
|
88
93
|
|
89
94
|
context 'scopes' do
|
90
95
|
describe 'with default scope' do
|
91
|
-
it { subject.scope.
|
96
|
+
it { expect(subject.scope).to eq(MyApp::Song.all) }
|
92
97
|
end
|
93
98
|
|
94
99
|
describe 'with custom scope' do
|
95
100
|
let(:scope) { MyApp::Song.where("id >= 100") }
|
96
|
-
it { subject.scope.
|
101
|
+
it { expect(subject.scope).to eq(scope) }
|
97
102
|
end
|
98
103
|
end
|
99
104
|
end
|