Skip to content

Commit 7f9de48

Browse files
feat(smart-field): set filterable and sortable options at true by default on polymorphic smart field (#697)
1 parent b7546ab commit 7f9de48

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

lib/forest_liana/collection.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,16 @@ def field(name, opts, &block)
8080

8181
field = opts.merge({
8282
field: name,
83-
is_filterable: !!opts[:is_filterable],
84-
is_sortable: !!opts[:is_sortable],
83+
is_filterable: if opts.has_key?(:is_filterable)
84+
!!opts[:is_filterable]
85+
else
86+
!!opts[:polymorphic_key]
87+
end,
88+
is_sortable: if opts.has_key?(:is_sortable)
89+
!!opts[:is_sortable]
90+
else
91+
!!opts[:polymorphic_key]
92+
end
8593
})
8694

8795
add_field(field)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Forest::Address
2+
include ForestLiana::Collection
3+
4+
collection :Address
5+
6+
field :addressable_type, type: 'String', polymorphic_key: true, is_filterable: false do
7+
object.addressable_type
8+
end
9+
10+
field :addressable_id, type: 'String', polymorphic_key: true do
11+
object.addressable_type
12+
end
13+
14+
field :address_type, type: 'String' do
15+
'delivery'
16+
end
17+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module ForestLiana
2+
describe Collection do
3+
before do
4+
allow(ForestLiana).to receive(:env_secret).and_return(nil)
5+
end
6+
7+
let(:collection) { ForestLiana.apimap.select { |collection| collection.name == 'Address' }.first }
8+
9+
describe 'field' do
10+
it 'add simple smart field' do
11+
field = collection.fields.select { |field| field[:field] == :address_type }.first
12+
13+
expect(field).not_to be_nil
14+
expect(field).to eq(
15+
{
16+
type: "String",
17+
is_read_only: true,
18+
is_required: false,
19+
default_value: nil,
20+
integration: nil,
21+
reference: nil,
22+
inverse_of: nil,
23+
relationships: nil,
24+
widget: nil,
25+
validations: [],
26+
is_virtual: true,
27+
field: :address_type,
28+
is_filterable: false,
29+
is_sortable: false
30+
}
31+
)
32+
end
33+
34+
it 'add polymorphic smart field with default values' do
35+
field = collection.fields.select { |field| field[:field] == :addressable_id }.first
36+
37+
expect(field).not_to be_nil
38+
expect(field[:is_filterable]).to eq(true)
39+
expect(field[:is_sortable]).to eq(true)
40+
end
41+
42+
it 'add polymorphic smart field with is_filterable option set to false' do
43+
field = collection.fields.select { |field| field[:field] == :addressable_type }.first
44+
45+
expect(field).not_to be_nil
46+
expect(field[:is_filterable]).to eq(false)
47+
expect(field[:is_sortable]).to eq(true)
48+
end
49+
end
50+
end
51+
end

0 commit comments

Comments
 (0)