From b979e95049d80509510f06008dfac78e0c959b43 Mon Sep 17 00:00:00 2001 From: Daria Mayorova Date: Wed, 18 Sep 2024 16:01:51 +0200 Subject: [PATCH] Less confusing syntax for user_options hash safe navigation --- app/representers/cms/layout_representer.rb | 2 +- app/representers/cms/page_representer.rb | 2 +- app/representers/cms/partial_representer.rb | 2 +- app/representers/cms/portlet_representer.rb | 2 +- app/representers/member_permissions_representer.rb | 12 ++++++------ .../admin/api/cms/templates_controller_test.rb | 1 + 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app/representers/cms/layout_representer.rb b/app/representers/cms/layout_representer.rb index 049c00d7aa..08bb816034 100644 --- a/app/representers/cms/layout_representer.rb +++ b/app/representers/cms/layout_representer.rb @@ -11,7 +11,7 @@ module CMS::LayoutRepresenter property :system_name property :liquid_enabled - with_options(if: ->(options) { !options[:user_options]&.dig(:short) }) do + with_options(if: ->(options) { !options.dig(:user_options, :short) }) do property :draft, render_nil: true property :published, render_nil: true end diff --git a/app/representers/cms/page_representer.rb b/app/representers/cms/page_representer.rb index 5b8653d293..e701e958de 100644 --- a/app/representers/cms/page_representer.rb +++ b/app/representers/cms/page_representer.rb @@ -20,7 +20,7 @@ module CMS::PageRepresenter p.property :hidden, getter: ->(*) { hidden? } end - with_options(if: ->(options) { !options[:user_options]&.dig(:short) }) do + with_options(if: ->(options) { !options.dig(:user_options, :short) }) do property :draft, render_nil: true property :published, render_nil: true end diff --git a/app/representers/cms/partial_representer.rb b/app/representers/cms/partial_representer.rb index 5c53073edd..548b981781 100644 --- a/app/representers/cms/partial_representer.rb +++ b/app/representers/cms/partial_representer.rb @@ -9,7 +9,7 @@ module CMS::PartialRepresenter property :updated_at property :system_name - with_options(if: ->(options) { !options[:user_options]&.dig(:short) }) do + with_options(if: ->(options) { !options.dig(:user_options, :short) }) do property :draft, render_nil: true property :published, render_nil: true end diff --git a/app/representers/cms/portlet_representer.rb b/app/representers/cms/portlet_representer.rb index f29143da95..151e1436d7 100644 --- a/app/representers/cms/portlet_representer.rb +++ b/app/representers/cms/portlet_representer.rb @@ -9,7 +9,7 @@ module CMS::PortletRepresenter property :portlet_type property :name - with_options(if: ->(options) { !options[:user_options]&.dig(:short) }) do + with_options(if: ->(options) { !options.dig(:user_options, :short) }) do property :draft, render_nil: true property :published, render_nil: true end diff --git a/app/representers/member_permissions_representer.rb b/app/representers/member_permissions_representer.rb index c8506e21cd..b4b6428349 100644 --- a/app/representers/member_permissions_representer.rb +++ b/app/representers/member_permissions_representer.rb @@ -38,15 +38,15 @@ class MemberPermissionsRepresenter < ThreeScale::Representer wraps_resource :permissions - property :user_id, getter: ->(options) { options[:user_options]&.[](:user)&.id } - property :role, getter: ->(options) { options[:user_options]&.[](:user)&.role } + property :user_id, getter: ->(options) { options.dig(:user_options, :user)&.id } + property :role, getter: ->(options) { options.dig(:user_options, :user)&.role } class JSON < MemberPermissionsRepresenter include Roar::JSON # NOTE: The list of allowed sections is sorted to facilitate acceptance testing, this is not part of the API specification - property :allowed_sections, getter: ->(options) { options[:user_options]&.[](:user)&.allowed_sections&.sort } - property :allowed_service_ids, getter: ->(options) { options[:user_options]&.[](:user)&.allowed_service_ids }, render_nil: true + property :allowed_sections, getter: ->(options) { options.dig(:user_options, :user)&.allowed_sections&.sort } + property :allowed_service_ids, getter: ->(options) { options.dig(:user_options, :user)&.allowed_service_ids }, render_nil: true link :user do |opts| user = opts[:user] @@ -59,8 +59,8 @@ class XML < MemberPermissionsRepresenter wraps_resource :permissions # NOTE: The list of allowed sections is sorted to facilitate acceptance testing, this is not part of the API specification - collection :allowed_sections, as: :allowed_section, wrap: :allowed_sections, getter: ->(options) { options[:user_options]&.[](:user)&.allowed_sections&.sort } - collection :allowed_service_ids, as: :allowed_service_id, wrap: :allowed_service_ids, getter: ->(options) { options[:user_options]&.[](:user)&.allowed_service_ids } + collection :allowed_sections, as: :allowed_section, wrap: :allowed_sections, getter: ->(options) { options.dig(:user_options, :user)&.allowed_sections&.sort } + collection :allowed_service_ids, as: :allowed_service_id, wrap: :allowed_service_ids, getter: ->(options) { options.dig(:user_options, :user)&.allowed_service_ids } end end diff --git a/test/functional/admin/api/cms/templates_controller_test.rb b/test/functional/admin/api/cms/templates_controller_test.rb index 581a0ffe8e..9be0fd4693 100644 --- a/test/functional/admin/api/cms/templates_controller_test.rb +++ b/test/functional/admin/api/cms/templates_controller_test.rb @@ -228,6 +228,7 @@ def test_update_page_layout_name title: 'About', path: '/about', format: :json, access_token: @token } assert_equal @layout, page.reload.layout + assert JSON.parse(response.body)['draft'] end def test_update_page_layout_id_unknown