Class: JSONAPI::LinkBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/link_builder.rb

Constant Summary collapse

@@url_helper_methods =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ LinkBuilder

Returns a new instance of LinkBuilder.



12
13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



3
4
5
# File 'lib/jsonapi/link_builder.rb', line 3

def base_url
  @base_url
end

#engineObject (readonly)

Returns the value of attribute engine.



3
4
5
# File 'lib/jsonapi/link_builder.rb', line 3

def engine
  @engine
end

#engine_mount_pointObject (readonly)

Returns the value of attribute engine_mount_point.



3
4
5
# File 'lib/jsonapi/link_builder.rb', line 3

def engine_mount_point
  @engine_mount_point
end

#primary_resource_klassObject (readonly)

Returns the value of attribute primary_resource_klass.



3
4
5
# File 'lib/jsonapi/link_builder.rb', line 3

def primary_resource_klass
  @primary_resource_klass
end

#route_formatterObject (readonly)

Returns the value of attribute route_formatter.



3
4
5
# File 'lib/jsonapi/link_builder.rb', line 3

def route_formatter
  @route_formatter
end

#url_helpersObject (readonly)

Returns the value of attribute url_helpers.



3
4
5
# File 'lib/jsonapi/link_builder.rb', line 3

def url_helpers
  @url_helpers
end

Instance Method Details

#engine?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/jsonapi/link_builder.rb', line 25

def engine?
  !!@engine
end

#primary_resources_urlObject



29
30
31
32
33
34
35
36
37
38
39
40
# 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


42
43
44
45
46
# 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


48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jsonapi/link_builder.rb', line 48

def relationships_related_link(source, relationship, query_params = {})
  if relationship._routed
    url = "#{ self_link(source) }/#{ route_for_relationship(relationship) }"
    url = "#{ url }?#{ query_params.to_query }" if query_params.present?
    url
  else
    if JSONAPI.configuration.warn_on_missing_routes && !relationship._warned_missing_route
      warn "related_link for #{relationship} could not be generated"
      relationship._warned_missing_route = true
    end
    nil
  end
end


62
63
64
65
66
67
68
69
70
71
72
# 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


74
75
76
77
78
79
80
81
82
83
84
# 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