Skip to content

Commit

Permalink
Less confusing syntax for user_options hash safe navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
mayorova committed Sep 18, 2024
1 parent 4924c8d commit b979e95
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/representers/cms/layout_representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/representers/cms/page_representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/representers/cms/partial_representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/representers/cms/portlet_representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions app/representers/member_permissions_representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
1 change: 1 addition & 0 deletions test/functional/admin/api/cms/templates_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b979e95

Please sign in to comment.