Skip to content

Commit

Permalink
feat(smart-field): set filterable and sortable options at true by def…
Browse files Browse the repository at this point in the history
…ault on polymorphic smart field (#697)
  • Loading branch information
nicolasalexandre9 authored Dec 10, 2024
1 parent b7546ab commit 7f9de48
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/forest_liana/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,16 @@ def field(name, opts, &block)

field = opts.merge({
field: name,
is_filterable: !!opts[:is_filterable],
is_sortable: !!opts[:is_sortable],
is_filterable: if opts.has_key?(:is_filterable)
!!opts[:is_filterable]
else
!!opts[:polymorphic_key]
end,
is_sortable: if opts.has_key?(:is_sortable)
!!opts[:is_sortable]
else
!!opts[:polymorphic_key]
end
})

add_field(field)
Expand Down
17 changes: 17 additions & 0 deletions spec/dummy/lib/forest_liana/collections/address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Forest::Address
include ForestLiana::Collection

collection :Address

field :addressable_type, type: 'String', polymorphic_key: true, is_filterable: false do
object.addressable_type
end

field :addressable_id, type: 'String', polymorphic_key: true do
object.addressable_type
end

field :address_type, type: 'String' do
'delivery'
end
end
51 changes: 51 additions & 0 deletions spec/lib/forest_liana/collection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module ForestLiana
describe Collection do
before do
allow(ForestLiana).to receive(:env_secret).and_return(nil)
end

let(:collection) { ForestLiana.apimap.select { |collection| collection.name == 'Address' }.first }

describe 'field' do
it 'add simple smart field' do
field = collection.fields.select { |field| field[:field] == :address_type }.first

expect(field).not_to be_nil
expect(field).to eq(
{
type: "String",
is_read_only: true,
is_required: false,
default_value: nil,
integration: nil,
reference: nil,
inverse_of: nil,
relationships: nil,
widget: nil,
validations: [],
is_virtual: true,
field: :address_type,
is_filterable: false,
is_sortable: false
}
)
end

it 'add polymorphic smart field with default values' do
field = collection.fields.select { |field| field[:field] == :addressable_id }.first

expect(field).not_to be_nil
expect(field[:is_filterable]).to eq(true)
expect(field[:is_sortable]).to eq(true)
end

it 'add polymorphic smart field with is_filterable option set to false' do
field = collection.fields.select { |field| field[:field] == :addressable_type }.first

expect(field).not_to be_nil
expect(field[:is_filterable]).to eq(false)
expect(field[:is_sortable]).to eq(true)
end
end
end
end

0 comments on commit 7f9de48

Please sign in to comment.