# File lib/jsonapi/link_builder.rb, line 29 def primary_resources_url if @primary_resource_klass._routed primary_resources_path = resources_path(primary_resource_klass) @primary_resources_url_cached ||= "#{ base_url }#{ engine_mount_point }#{ primary_resources_path }" else if JSONAPI.configuration.warn_on_missing_routes && !@primary_resource_klass._warned_missing_route warn "primary_resources_url for #{@primary_resource_klass} could not be generated" @primary_resource_klass._warned_missing_route = true end nil end end
class JSONAPI::LinkBuilder
Attributes
base_url[R]
engine[R]
engine_mount_point[R]
primary_resource_klass[R]
route_formatter[R]
url_helpers[R]
Public Class Methods
new(config = {})
click to toggle source
# File lib/jsonapi/link_builder.rb, line 12 def initialize(config = {}) @base_url = config[:base_url] @primary_resource_klass = config[:primary_resource_klass] @route_formatter = config[:route_formatter] @engine = build_engine @engine_mount_point = @engine ? @engine.routes.find_script_name({}) : "" # url_helpers may be either a controller which has the route helper methods, or the application router's # url helpers module, `Rails.application.routes.url_helpers`. Because the method no longer behaves as a # singleton, and it's expensive to generate the module, the controller is preferred. @url_helpers = config[:url_helpers] end
Public Instance Methods
engine?()
click to toggle source
# File lib/jsonapi/link_builder.rb, line 25 def engine? !!@engine end
primary_resources_url()
click to toggle source
query_link(query_params)
click to toggle source
# File lib/jsonapi/link_builder.rb, line 42 def query_link(query_params) url = primary_resources_url return url if url.nil? "#{ url }?#{ query_params.to_query }" end
relationships_self_link(source, relationship)
click to toggle source
# File lib/jsonapi/link_builder.rb, line 62 def relationships_self_link(source, relationship) if relationship._routed "#{ self_link(source) }/relationships/#{ route_for_relationship(relationship) }" else if JSONAPI.configuration.warn_on_missing_routes && !relationship._warned_missing_route warn "self_link for #{relationship} could not be generated" relationship._warned_missing_route = true end nil end end
self_link(source)
click to toggle source
# File lib/jsonapi/link_builder.rb, line 74 def self_link(source) if source.class._routed resource_url(source) else if JSONAPI.configuration.warn_on_missing_routes && !source.class._warned_missing_route warn "self_link for #{source.class} could not be generated" source.class._warned_missing_route = true end nil end end
Private Instance Methods
build_engine()
click to toggle source
# File lib/jsonapi/link_builder.rb, line 88 def build_engine scopes = module_scopes_from_class(primary_resource_klass) begin unless scopes.empty? "#{ scopes.first.to_s.camelize }::Engine".safe_constantize end # :nocov: rescue LoadError => _e nil # :nocov: end end
format_route(route)
click to toggle source
# File lib/jsonapi/link_builder.rb, line 103 def format_route(route) route_formatter.format(route) end
formatted_module_path_from_class(klass)
click to toggle source
# File lib/jsonapi/link_builder.rb, line 107 def formatted_module_path_from_class(klass) scopes = if @engine module_scopes_from_class(klass)[1..-1] else module_scopes_from_class(klass) end unless scopes.empty? "/#{ scopes.map {|scope| format_route(scope.to_s.underscore)}.compact.join('/') }/" else "/" end end
module_scopes_from_class(klass)
click to toggle source
# File lib/jsonapi/link_builder.rb, line 121 def module_scopes_from_class(klass) klass.name.to_s.split("::")[0...-1] end
resource_path(source)
click to toggle source
# File lib/jsonapi/link_builder.rb, line 130 def resource_path(source) if source.class.singleton? resources_path(source.class) else "#{resources_path(source.class)}/#{source.id}" end end
resource_url(source)
click to toggle source
# File lib/jsonapi/link_builder.rb, line 138 def resource_url(source) "#{ base_url }#{ engine_mount_point }#{ resource_path(source) }" end
resources_path(source_klass)
click to toggle source
# File lib/jsonapi/link_builder.rb, line 125 def resources_path(source_klass) @_resources_path ||= {} @_resources_path[source_klass] ||= formatted_module_path_from_class(source_klass) + format_route(source_klass._type.to_s) end
route_for_relationship(relationship)
click to toggle source
# File lib/jsonapi/link_builder.rb, line 142 def route_for_relationship(relationship) format_route(relationship.name) end