lib/jsonapi/routing_ext.rb in jsonapi-resources-0.7.1.beta1 vs lib/jsonapi/routing_ext.rb in jsonapi-resources-0.7.1.beta2

- old
+ new

@@ -31,17 +31,31 @@ else options[:except] = [:new, :edit] end resource @resource_type, options do - @scope[:jsonapi_resource] = @resource_type + # :nocov: + if @scope.respond_to? :[]= + # Rails 4 + @scope[:jsonapi_resource] = @resource_type - if block_given? - yield + if block_given? + yield + else + jsonapi_relationships + end else - jsonapi_relationships + # Rails 5 + jsonapi_resource_scope(SingletonResource.new(@resource_type, api_only?, @scope[:shallow], options), @resource_type) do + if block_given? + yield + else + jsonapi_relationships + end + end end + # :nocov: end end def jsonapi_relationships(options = {}) res = JSONAPI::Resource.resource_for(resource_type_with_module_prefix(@resource_type)) @@ -67,11 +81,12 @@ options[:param] = :id options[:path] = format_route(@resource_type) if res.resource_key_type == :uuid - options[:constraints] = {id: /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/} + options[:constraints] ||= {} + options[:constraints][:id] ||= /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/ end if options[:except] options[:except] = Array(options[:except]) options[:except] << :new unless options[:except].include?(:new) || options[:except].include?('new') @@ -85,17 +100,30 @@ options[:except] << :update options[:except] << :destroy end resources @resource_type, options do - @scope[:jsonapi_resource] = @resource_type - - if block_given? - yield + # :nocov: + if @scope.respond_to? :[]= + # Rails 4 + @scope[:jsonapi_resource] = @resource_type + if block_given? + yield + else + jsonapi_relationships + end else - jsonapi_relationships + # Rails 5 + jsonapi_resource_scope(Resource.new(@resource_type, api_only?, @scope[:shallow], options), @resource_type) do + if block_given? + yield + else + jsonapi_relationships + end + end end + # :nocov: end end def links_methods(options) default_methods = [:show, :create, :destroy, :update] @@ -204,9 +232,19 @@ match "#{formatted_relationship_name}", controller: options[:controller], relationship: relationship.name, source: resource_type_with_module_prefix(source._type), action: 'get_related_resources', via: [:get] end + protected + # :nocov: + def jsonapi_resource_scope(resource, resource_type) #:nodoc: + @scope = @scope.new(scope_level_resource: resource, jsonapi_resource: resource_type) + + controller(resource.resource_scope) { yield } + ensure + @scope = @scope.parent + end + # :nocov: private def resource_type_with_module_prefix(resource = nil) resource_name = resource || @scope[:jsonapi_resource] [@scope[:module], resource_name].compact.collect(&:to_s).join('/')