lib/jsonapi/routing_ext.rb in jsonapi-resources-0.1.1 vs lib/jsonapi/routing_ext.rb in jsonapi-resources-0.2.0

- old
+ new

@@ -63,12 +63,14 @@ yield else res._associations.each do |association_name, association| if association.is_a?(JSONAPI::Association::HasMany) jsonapi_links(association_name) + jsonapi_related_resources(association_name) else jsonapi_link(association_name) + jsonapi_related_resource(association_name) end end end end end @@ -93,26 +95,21 @@ methods = links_methods(options) if methods.include?(:show) match "links/#{formatted_association_name}", controller: res._type.to_s, - action: 'show_association', association: link_type.to_s, via: [:get] + action: 'show_association', association: link_type.to_s, via: [:get] end - if methods.include?(:create) - match "links/#{formatted_association_name}", controller: res._type.to_s, - action: 'create_association', association: link_type.to_s, via: [:post] - end - if methods.include?(:update) match "links/#{formatted_association_name}", controller: res._type.to_s, - action: 'update_association', association: link_type.to_s, via: [:put] + action: 'update_association', association: link_type.to_s, via: [:put] end if methods.include?(:destroy) match "links/#{formatted_association_name}", controller: res._type.to_s, - action: 'destroy_association', association: link_type.to_s, via: [:delete] + action: 'destroy_association', association: link_type.to_s, via: [:delete] end end def jsonapi_links(*links) link_type = links.first @@ -123,28 +120,57 @@ methods = links_methods(options) if methods.include?(:show) match "links/#{formatted_association_name}", controller: res._type.to_s, - action: 'show_association', association: link_type.to_s, via: [:get] + action: 'show_association', association: link_type.to_s, via: [:get] end if methods.include?(:create) match "links/#{formatted_association_name}", controller: res._type.to_s, - action: 'create_association', association: link_type.to_s, via: [:post] + action: 'create_association', association: link_type.to_s, via: [:post] end if methods.include?(:update) && res._association(link_type).acts_as_set match "links/#{formatted_association_name}", controller: res._type.to_s, - action: 'update_association', association: link_type.to_s, via: [:put] + action: 'update_association', association: link_type.to_s, via: [:put] end if methods.include?(:destroy) match "links/#{formatted_association_name}/:keys", controller: res._type.to_s, - action: 'destroy_association', association: link_type.to_s, via: [:delete] + action: 'destroy_association', association: link_type.to_s, via: [:delete] end end + def jsonapi_related_resource(*association) + source = JSONAPI::Resource.resource_for(resource_type_with_module_prefix) + + association_name = association.first + association = source._associations[association_name] + + formatted_association_name = format_route(association.name) + related_resource = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(association.name.pluralize)) + + match "#{formatted_association_name}", controller: related_resource._type.to_s, + association: association.name, source: resource_type_with_module_prefix(source._type), + action: 'get_related_resource', via: [:get] + end + + def jsonapi_related_resources(*association) + source = JSONAPI::Resource.resource_for(resource_type_with_module_prefix) + + association_name = association.first + association = source._associations[association_name] + + formatted_association_name = format_route(association.name) + related_resource = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(association.name)) + + match "#{formatted_association_name}", controller: related_resource._type.to_s, + association: association.name, source: resource_type_with_module_prefix(source._type), + action: 'get_related_resources', via: [:get] + end + + private def resource_type_with_module_prefix(resource = nil) resource_name = resource || @scope[:jsonapi_resource] [@scope[:module], resource_name].compact.collect(&:to_s).join("/") end end