class ActionDispatch::Routing::Mapper

Public Instance Methods

format_route(route) click to toggle source
# File lib/jsonapi/routing_ext.rb, line 15
def format_route(route)
  JSONAPI.configuration.route_formatter.format(route.to_s)
end
jsonapi_relationships(options = {}) click to toggle source
# File lib/jsonapi/routing_ext.rb, line 72
def jsonapi_relationships(options = {})
  res = JSONAPI::Resource.resource_klass_for(resource_type_with_module_prefix(@resource_type))
  res._relationships.each do |relationship_name, relationship|
    if relationship.is_a?(JSONAPI::Relationship::ToMany)
      jsonapi_links(relationship_name, options)
      jsonapi_related_resources(relationship_name, options)
    else
      jsonapi_link(relationship_name, options)
      jsonapi_related_resource(relationship_name, options)
    end
  end
end
jsonapi_resource(*resources) { || ... } click to toggle source
# File lib/jsonapi/routing_ext.rb, line 19
def jsonapi_resource(*resources, &_block)
  @resource_type = resources.first
  res = JSONAPI::Resource.resource_klass_for(resource_type_with_module_prefix(@resource_type))

  res._routed = true

  unless res.singleton?
    warn "Singleton routes created for non singleton resource #{res}. Links may not be generated correctly."
  end

  options = resources.extract_options!.dup
  options[:controller] ||= @resource_type
  options.merge!(res.routing_resource_options)
  options[:path] = format_route(@resource_type)

  if options[:except]
    options[:except] << :new unless options[:except].include?(:new) || options[:except].include?('new')
    options[:except] << :edit unless options[:except].include?(:edit) || options[:except].include?('edit')
  else
    options[:except] = [:new, :edit]
  end

  if res._immutable
    options[:except] << :create unless options[:except].include?(:create) || options[:except].include?('create')
    options[:except] << :update unless options[:except].include?(:update) || options[:except].include?('update')
    options[:except] << :destroy unless options[:except].include?(:destroy) || options[:except].include?('destroy')
  end

  resource @resource_type, options do
    # :nocov:
    if @scope.respond_to? :[]=
      # Rails 4
      @scope[:jsonapi_resource] = @resource_type

      if block_given?
        yield
      else
        jsonapi_relationships
      end
    else
      # Rails 5
      jsonapi_resource_scope(SingletonResource.new(@resource_type, api_only?, @scope[:shallow], options), @resource_type) do
        if block_given?
          yield
        else
          jsonapi_relationships
        end
      end
    end
    # :nocov:
  end
end
jsonapi_resources(*resources) { || ... } click to toggle source
# File lib/jsonapi/routing_ext.rb, line 85
def jsonapi_resources(*resources, &_block)
  @resource_type = resources.first
  res = JSONAPI::Resource.resource_klass_for(resource_type_with_module_prefix(@resource_type))

  res._routed = true

  if res.singleton?
    warn "Singleton resource #{res} should use `jsonapi_resource` instead."
  end

  options = resources.extract_options!.dup
  options[:controller] ||= @resource_type
  options.merge!(res.routing_resource_options)

  options[:param] = :id

  options[:path] = format_route(@resource_type)

  if res.resource_key_type == :uuid
    options[:constraints] ||= {}
    options[:constraints][:id] ||= /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/
  end

  if options[:except]
    options[:except] = Array(options[:except])
    options[:except] << :new unless options[:except].include?(:new) || options[:except].include?('new')
    options[:except] << :edit unless options[:except].include?(:edit) || options[:except].include?('edit')
  else
    options[:except] = [:new, :edit]
  end

  if res._immutable
    options[:except] << :create unless options[:except].include?(:create) || options[:except].include?('create')
    options[:except] << :update unless options[:except].include?(:update) || options[:except].include?('update')
    options[:except] << :destroy unless options[:except].include?(:destroy) || options[:except].include?('destroy')
  end

  resources @resource_type, options do
    # :nocov:
    if @scope.respond_to? :[]=
      # Rails 4
      @scope[:jsonapi_resource] = @resource_type
      if block_given?
        yield
      else
        jsonapi_relationships
      end
    else
      # Rails 5
      jsonapi_resource_scope(Resource.new(@resource_type, api_only?, @scope[:shallow], options), @resource_type) do
        if block_given?
          yield
        else
          jsonapi_relationships
        end
      end
    end
    # :nocov:
  end
end
nested_param() click to toggle source
# File lib/jsonapi/routing_ext.rb, line 9
def nested_param
  :"#{unformat_route(singular)}_#{param}"
end
resource_type_with_module_prefix(resource = nil) click to toggle source
# File lib/jsonapi/routing_ext.rb, line 277
def resource_type_with_module_prefix(resource = nil)
  resource_name = resource || @scope[:jsonapi_resource]
  [@scope[:module], resource_name].compact.collect(&:to_s).join('/')
end
unformat_route(route) click to toggle source
# File lib/jsonapi/routing_ext.rb, line 5
def unformat_route(route)
  JSONAPI.configuration.route_formatter.unformat(route.to_s)
end