|
606 | 606 | controller.send(:serialize_models, records, options)
|
607 | 607 | end
|
608 | 608 | end
|
| 609 | + |
| 610 | + describe 'fields_per_model method' do |
| 611 | + let(:model) { Island } |
| 612 | + let(:collection_name) { 'Island' } |
| 613 | + |
| 614 | + before do |
| 615 | + forest_collection = double('forest_collection') |
| 616 | + allow(ForestLiana).to receive(:apimap).and_return([forest_collection]) |
| 617 | + allow(forest_collection).to receive(:name).and_return(collection_name) |
| 618 | + end |
| 619 | + |
| 620 | + context 'with smart relations pointing to external models' do |
| 621 | + it 'should use reference model name as key for external smart relations' do |
| 622 | + smart_relations = [ |
| 623 | + { |
| 624 | + field: :organization, |
| 625 | + reference: 'Api__OrganizationsView.id', |
| 626 | + is_virtual: true, |
| 627 | + is_searchable: false, |
| 628 | + type: 'String' |
| 629 | + } |
| 630 | + ] |
| 631 | + |
| 632 | + forest_collection = double('forest_collection') |
| 633 | + allow(forest_collection).to receive(:name).and_return(collection_name) |
| 634 | + allow(forest_collection).to receive(:fields_smart_belongs_to).and_return(smart_relations) |
| 635 | + allow(ForestLiana).to receive(:apimap).and_return([forest_collection]) |
| 636 | + |
| 637 | + params_fields = ActionController::Parameters.new({ |
| 638 | + 'organization' => 'name' |
| 639 | + }) |
| 640 | + |
| 641 | + mock_params = ActionController::Parameters.new({ |
| 642 | + collection: 'Island', |
| 643 | + fields: { 'Island' => 'id,name', 'organization' => 'name'}, |
| 644 | + page: { 'number' => '1', 'size' => '10' }, |
| 645 | + searchExtended: '0', |
| 646 | + sort: '-id', |
| 647 | + timezone: 'Europe/Paris' |
| 648 | + }) |
| 649 | + allow(controller).to receive(:params).and_return(mock_params) |
| 650 | + |
| 651 | + result = controller.send(:fields_per_model, params_fields, model) |
| 652 | + |
| 653 | + expect(result).to have_key('Api__OrganizationsView') |
| 654 | + expect(result['Api__OrganizationsView']).to eq('name') |
| 655 | + expect(result).not_to have_key('organization') |
| 656 | + end |
| 657 | + end |
| 658 | + |
| 659 | + context 'with smart relations pointing to same collection (self-reference)' do |
| 660 | + it 'should use relation name as key for self-referencing smart relations' do |
| 661 | + smart_relations = [ |
| 662 | + { |
| 663 | + field: :parent_island, |
| 664 | + reference: 'Island.id', |
| 665 | + is_virtual: true, |
| 666 | + is_searchable: false, |
| 667 | + type: 'String' |
| 668 | + } |
| 669 | + ] |
| 670 | + |
| 671 | + forest_collection = double('forest_collection') |
| 672 | + allow(forest_collection).to receive(:name).and_return(collection_name) |
| 673 | + allow(forest_collection).to receive(:fields_smart_belongs_to).and_return(smart_relations) |
| 674 | + allow(ForestLiana).to receive(:apimap).and_return([forest_collection]) |
| 675 | + |
| 676 | + params_fields = ActionController::Parameters.new({ |
| 677 | + 'parent_island' => 'name' |
| 678 | + }) |
| 679 | + |
| 680 | + mock_params = ActionController::Parameters.new({ |
| 681 | + collection: 'Island', |
| 682 | + fields: { 'Island' => 'id,name', 'parent_island' => 'name'}, |
| 683 | + page: { 'number' => '1', 'size' => '10' }, |
| 684 | + searchExtended: '0', |
| 685 | + sort: '-id', |
| 686 | + timezone: 'Europe/Paris' |
| 687 | + }) |
| 688 | + allow(controller).to receive(:params).and_return(mock_params) |
| 689 | + |
| 690 | + result = controller.send(:fields_per_model, params_fields, model) |
| 691 | + |
| 692 | + expect(result).to have_key('parent_island') |
| 693 | + expect(result['parent_island']).to eq('name') |
| 694 | + expect(result).not_to have_key('Island') |
| 695 | + end |
| 696 | + end |
| 697 | + end |
609 | 698 | end
|
0 commit comments