Sha256: cbf3944a7d2e4ca4d9a84880d936d58218a550ac43e01c3952519efbdd3b2b2d

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

module SimpleJsonapi
  module Extensions
    module Routing
      ACTION_MAP = {
        add: :create,
        remove: :destroy,
        replace: :update,
        fetch: :show,
      }.freeze

      SUPPORTED_TO_MANY_ACTIONS = ACTION_MAP.keys.freeze

      def jsonapi_to_one_relationship(member_name, association)
        jsonapi_relationship([:replace], member_name, association)
      end

      def jsonapi_to_many_relationship(member_name, association, only: nil, except: nil)
        jsonapi_relationship(to_many_actions_to_define(only, except), member_name, association)
      end

      private

      def jsonapi_relationship(actions, member_name, association)
        member do
          scope as: member_name, module: member_name.to_s.pluralize do
            namespace "relationships" do
              actions.each do |action|
                resource association, only: [ACTION_MAP[action]], action: action
              end
            end
          end
        end
      end

      def to_many_actions_to_define(only, except)
        actions = if only
                    Array(only)
                  elsif except
                    SUPPORTED_TO_MANY_ACTIONS - Array(except)
                  else
                    SUPPORTED_TO_MANY_ACTIONS
                  end

        ensure_actions_supported(actions)

        actions
      end

      def ensure_actions_supported(actions)
        if actions.any? { |action| SUPPORTED_TO_MANY_ACTIONS.exclude?(action) }
          raise ArgumentError, "#jsonapi_to_many_relationship supports :add, :remove, :replace, and :fetch actions"
        end
      end
    end

    ActionDispatch::Routing::Mapper.include(SimpleJsonapi::Extensions::Routing)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
simple_jsonapi_rails-1.3.0 lib/simple_jsonapi/rails/extensions/routing.rb
simple_jsonapi_rails-1.2.0 lib/simple_jsonapi/rails/extensions/routing.rb
simple_jsonapi_rails-1.1.1 lib/simple_jsonapi/rails/extensions/routing.rb
simple_jsonapi_rails-1.1.0 lib/simple_jsonapi/rails/extensions/routing.rb