Skip to content

Commit d4b801f

Browse files
committed
[*] Filters - Fix resources display if filtered with associations conditions with the related columns are hidden in the list
1 parent 349ce5b commit d4b801f

File tree

5 files changed

+104
-20
lines changed

5 files changed

+104
-20
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Change Log
22

33
## [Unreleased]
4+
### Fixed
5+
- Filters - Fix resources display if filtered with associations conditions with the related columns are hidden in the list. 🛡
46

57
## RELEASE 2.15.4 - 2019-02-26
68
### Fixed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ group :test do
2222
end
2323

2424
gem 'rails', '4.2.7.1'
25-
gem 'jsonapi-serializers', '0.14.0'
25+
gem 'jsonapi-serializers', '1.0.1'
2626
gem 'rack-cors'
2727
gem 'arel-helpers'
2828
gem 'groupdate'

Gemfile.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ GEM
6060
bcrypt (3.1.10)
6161
builder (3.2.3)
6262
byebug (8.2.2)
63-
concurrent-ruby (1.0.5)
63+
concurrent-ruby (1.1.4)
6464
crass (1.0.3)
6565
diff-lcs (1.3)
6666
erubis (2.7.0)
@@ -71,11 +71,11 @@ GEM
7171
httparty (0.13.7)
7272
json (~> 1.8)
7373
multi_xml (>= 0.5.2)
74-
i18n (0.9.1)
74+
i18n (0.9.5)
7575
concurrent-ruby (~> 1.0)
7676
ipaddress (0.8.3)
7777
json (1.8.6)
78-
jsonapi-serializers (0.14.0)
78+
jsonapi-serializers (1.0.1)
7979
activesupport
8080
jwt (1.5.4)
8181
loofah (2.1.1)
@@ -85,7 +85,7 @@ GEM
8585
mini_mime (>= 0.1.1)
8686
mini_mime (1.0.0)
8787
mini_portile2 (2.3.0)
88-
minitest (5.11.1)
88+
minitest (5.11.3)
8989
multi_xml (0.6.0)
9090
nokogiri (1.8.1)
9191
mini_portile2 (~> 2.3.0)
@@ -146,7 +146,7 @@ GEM
146146
sqlite3 (1.3.11)
147147
thor (0.20.0)
148148
thread_safe (0.3.6)
149-
tzinfo (1.2.4)
149+
tzinfo (1.2.5)
150150
thread_safe (~> 0.1)
151151
useragent (0.16.5)
152152

@@ -162,7 +162,7 @@ DEPENDENCIES
162162
groupdate
163163
httparty (= 0.13.7)
164164
ipaddress (= 0.8.3)
165-
jsonapi-serializers (= 0.14.0)
165+
jsonapi-serializers (= 1.0.1)
166166
jwt
167167
rack-cors
168168
rails (= 4.2.7.1)

app/services/forest_liana/resources_getter.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def initialize(resource, params)
1010
@count_needs_includes = false
1111
@collection_name = ForestLiana.name_for(@resource)
1212
@collection = get_collection(@collection_name)
13+
@fields_to_serialize = get_fields_to_serialize
1314
@field_names_requested = field_names_requested
1415
get_segment()
1516
compute_includes()
@@ -62,8 +63,16 @@ def compute_includes
6263
end
6364
end
6465

66+
def includes_for_serialization
67+
super & @fields_to_serialize.map(&:to_s)
68+
end
69+
6570
private
6671

72+
def get_fields_to_serialize
73+
@params[:fields][@collection_name].split(',').map { |name| name.to_sym }
74+
end
75+
6776
def get_segment
6877
if @params[:segment]
6978
@segment = @collection.segments.find do |segment|
@@ -94,8 +103,7 @@ def field_names_requested
94103
associations_for_query << @params[:sort].split('.').first.to_sym
95104
end
96105

97-
field_names = @params[:fields][@collection_name].split(',').map { |name| name.to_sym }
98-
field_names | associations_for_query
106+
@fields_to_serialize | associations_for_query
99107
end
100108

101109
def search_query

spec/requests/resources_spec.rb

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,97 @@
11
require 'rails_helper'
22

3-
describe 'Requesting Tree index', :type => :request do
4-
before do
3+
describe 'Requesting Tree resources', :type => :request do
4+
before(:each) do
5+
user = User.create(name: 'Michel')
6+
tree = Tree.create(name: 'Lemon Tree', owner: user, cutter: user)
7+
end
8+
9+
after(:each) do
10+
User.destroy_all
11+
Tree.destroy_all
12+
end
13+
14+
before(:each) do
515
allow(ForestLiana::IpWhitelist).to receive(:retrieve) { true }
616
allow(ForestLiana::IpWhitelist).to receive(:is_ip_whitelist_retrieved) { true }
717
allow(ForestLiana::IpWhitelist).to receive(:is_ip_valid) { true }
818

919
allow_any_instance_of(ForestLiana::PermissionsChecker).to receive(:is_authorized?) { true }
1020
end
1121

12-
it 'should route /:collection to resource#index' do
13-
headers = {
14-
'Accept' => 'application/json',
15-
'Content-Type' => 'application/json',
16-
'Authorization' => 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJleHAiOiIxODQ5ODc4ODYzIiwiZGF0YSI6eyJpZCI6IjM4IiwidHlwZSI6InVzZXJzIiwiZGF0YSI6eyJlbWFpbCI6Im1pY2hhZWwua2Vsc29AdGhhdDcwLnNob3ciLCJmaXJzdF9uYW1lIjoiTWljaGFlbCIsImxhc3RfbmFtZSI6IktlbHNvIiwidGVhbXMiOiJPcGVyYXRpb25zIn0sInJlbGF0aW9uc2hpcHMiOnsicmVuZGVyaW5ncyI6eyJkYXRhIjpbeyJ0eXBlIjoicmVuZGVyaW5ncyIsImlkIjoxNn1dfX19fQ.U4Mxi0tq0Ce7y5FRXP47McNPRPhUx37LznQ5E3mJIp4'
17-
}
22+
headers = {
23+
'Accept' => 'application/json',
24+
'Content-Type' => 'application/json',
25+
'Authorization' => 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJleHAiOiIxODQ5ODc4ODYzIiwiZGF0YSI6eyJpZCI6IjM4IiwidHlwZSI6InVzZXJzIiwiZGF0YSI6eyJlbWFpbCI6Im1pY2hhZWwua2Vsc29AdGhhdDcwLnNob3ciLCJmaXJzdF9uYW1lIjoiTWljaGFlbCIsImxhc3RfbmFtZSI6IktlbHNvIiwidGVhbXMiOiJPcGVyYXRpb25zIn0sInJlbGF0aW9uc2hpcHMiOnsicmVuZGVyaW5ncyI6eyJkYXRhIjpbeyJ0eXBlIjoicmVuZGVyaW5ncyIsImlkIjoxNn1dfX19fQ.U4Mxi0tq0Ce7y5FRXP47McNPRPhUx37LznQ5E3mJIp4'
26+
}
27+
28+
describe 'index' do
29+
describe 'without any filter' do
30+
params = {
31+
fields: { 'Tree' => 'id,name' },
32+
page: { 'number' => '1', 'size' => '10' },
33+
searchExtended: '0',
34+
sort: '-id',
35+
timezone: 'Europe/Paris'
36+
}
37+
38+
it 'should respond 200' do
39+
get '/forest/Tree', params, headers
40+
expect(response.status).to eq(200)
41+
end
42+
43+
it 'should respond the tree data' do
44+
get '/forest/Tree', params, headers
45+
expect(JSON.parse(response.body)).to eq({
46+
"data" => [{
47+
"type" => "Tree",
48+
"id" => "1",
49+
"attributes" => {
50+
"id" => 1,
51+
"name" => "Lemon Tree"
52+
},
53+
"links" => {
54+
"self" => "/forest/tree/1"
55+
}
56+
}],
57+
"included" => []
58+
})
59+
end
60+
end
61+
62+
describe 'with a filter on an association that is not a displayed column' do
63+
params = {
64+
fields: { 'Tree' => 'id,name' },
65+
filterType: 'and',
66+
filter: { 'owner:id' => '$present' },
67+
page: { 'number' => '1', 'size' => '10' },
68+
searchExtended: '0',
69+
sort: '-id',
70+
timezone: 'Europe/Paris'
71+
}
72+
73+
it 'should respond 200' do
74+
get '/forest/Tree', params, headers
75+
expect(response.status).to eq(200)
76+
end
1877

19-
get '/forest/Tree', {}, headers
20-
expect(response.status).to eq(200)
21-
expect(response.body).to eq("{\"data\":[],\"included\":[]}")
78+
it 'should respond the tree data' do
79+
get '/forest/Tree', params, headers
80+
expect(JSON.parse(response.body)).to eq({
81+
"data" => [{
82+
"type" => "Tree",
83+
"id" => "1",
84+
"attributes" => {
85+
"id" => 1,
86+
"name" => "Lemon Tree"
87+
},
88+
"links" => {
89+
"self" => "/forest/tree/1"
90+
}
91+
}],
92+
"included" => []
93+
})
94+
end
95+
end
2296
end
2397
end

0 commit comments

Comments
 (0)