module SimpleJsonapi::Extensions::Routing

Constants

ACTION_MAP
SUPPORTED_TO_MANY_ACTIONS

Public Instance Methods

jsonapi_to_many_relationship(member_name, association, only: nil, except: nil) click to toggle source
# File lib/simple_jsonapi/rails/extensions/routing.rb, line 17
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
jsonapi_to_one_relationship(member_name, association) click to toggle source
# File lib/simple_jsonapi/rails/extensions/routing.rb, line 13
def jsonapi_to_one_relationship(member_name, association)
  jsonapi_relationship([:replace], member_name, association)
end

Private Instance Methods

ensure_actions_supported(actions) click to toggle source
# File lib/simple_jsonapi/rails/extensions/routing.rb, line 49
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
jsonapi_relationship(actions, member_name, association) click to toggle source
# File lib/simple_jsonapi/rails/extensions/routing.rb, line 23
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
to_many_actions_to_define(only, except) click to toggle source
# File lib/simple_jsonapi/rails/extensions/routing.rb, line 35
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