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.
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_url ⇒ Object (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 |
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
3 4 5 |
# File 'lib/jsonapi/link_builder.rb', line 3 def engine @engine end |
#engine_mount_point ⇒ Object (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_klass ⇒ Object (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_formatter ⇒ Object (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_helpers ⇒ Object (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
25 26 27 |
# File 'lib/jsonapi/link_builder.rb', line 25 def engine? !!@engine end |
#primary_resources_url ⇒ Object
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 |
#query_link(query_params) ⇒ Object
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 |
#relationships_related_link(source, relationship, query_params = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/jsonapi/link_builder.rb', line 48 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
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 |
#self_link(source) ⇒ Object
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 |