cloudfoundry/cloud_controller_ng

View on GitHub
app/decorators/include_role_organization_decorator.rb

Summary

Maintainability
A
1 hr
Test Coverage
module VCAP::CloudController
  class IncludeRoleOrganizationDecorator
    class << self
      def match?(include_params)
        # roles may be associated with an org without being associated with a space
        # this is why this is not `space.organization`
        include_params&.include?('organization')
      end

      def decorate(hash, roles)
        hash[:included] ||= {}
        organization_ids = roles.reject(&:for_space?).map(&:organization_id).uniq
        unless organization_ids.empty?
          organizations = Organization.where(id: organization_ids).order(:created_at).
                          eager(Presenters::V3::OrganizationPresenter.associated_resources).all
        end

        hash[:included][:organizations] = organizations&.map { |organization| Presenters::V3::OrganizationPresenter.new(organization).to_hash } || []
        hash
      end
    end
  end
end