Class: JSONAPI::LinkBuilder
- Inherits:
-
Object
- Object
- JSONAPI::LinkBuilder
- Defined in:
- lib/jsonapi/link_builder.rb
Constant Summary collapse
- @@url_helper_methods =
{}
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
-
#engine_mount_point ⇒ Object
readonly
Returns the value of attribute engine_mount_point.
-
#primary_resource_klass ⇒ Object
readonly
Returns the value of attribute primary_resource_klass.
-
#route_formatter ⇒ Object
readonly
Returns the value of attribute route_formatter.
-
#url_helpers ⇒ Object
readonly
Returns the value of attribute url_helpers.
Instance Method Summary collapse
- #engine? ⇒ Boolean
-
#initialize(config = {}) ⇒ LinkBuilder
constructor
A new instance of LinkBuilder.
- #primary_resources_url ⇒ Object
- #query_link(query_params) ⇒ Object
- #relationships_related_link(source, relationship, query_params = {}) ⇒ Object
- #relationships_self_link(source, relationship) ⇒ Object
- #self_link(source) ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ LinkBuilder
Returns a new instance of LinkBuilder.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/jsonapi/link_builder.rb', line 14 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_url ⇒ Object (readonly)
Returns the value of attribute base_url.
5 6 7 |
# File 'lib/jsonapi/link_builder.rb', line 5 def base_url @base_url end |
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
5 6 7 |
# File 'lib/jsonapi/link_builder.rb', line 5 def engine @engine end |
#engine_mount_point ⇒ Object (readonly)
Returns the value of attribute engine_mount_point.
5 6 7 |
# File 'lib/jsonapi/link_builder.rb', line 5 def engine_mount_point @engine_mount_point end |
#primary_resource_klass ⇒ Object (readonly)
Returns the value of attribute primary_resource_klass.
5 6 7 |
# File 'lib/jsonapi/link_builder.rb', line 5 def primary_resource_klass @primary_resource_klass end |
#route_formatter ⇒ Object (readonly)
Returns the value of attribute route_formatter.
5 6 7 |
# File 'lib/jsonapi/link_builder.rb', line 5 def route_formatter @route_formatter end |
#url_helpers ⇒ Object (readonly)
Returns the value of attribute url_helpers.
5 6 7 |
# File 'lib/jsonapi/link_builder.rb', line 5 def url_helpers @url_helpers end |
Instance Method Details
#engine? ⇒ Boolean
27 28 29 |
# File 'lib/jsonapi/link_builder.rb', line 27 def engine? !!@engine end |
#primary_resources_url ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/jsonapi/link_builder.rb', line 31 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 |
#query_link(query_params) ⇒ Object
44 45 46 47 48 |
# File 'lib/jsonapi/link_builder.rb', line 44 def query_link(query_params) url = primary_resources_url return url if url.nil? "#{ url }?#{ query_params.to_query }" end |
#relationships_related_link(source, relationship, query_params = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/jsonapi/link_builder.rb', line 50 def (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 |
#relationships_self_link(source, relationship) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/jsonapi/link_builder.rb', line 64 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) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/jsonapi/link_builder.rb', line 76 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 |