-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
problems with 0.10.1 changes to plain old resources #45
Comments
You won't want to use I believe you'll want to return the empty array from That should at least you get further along. If you continue having problems, please just let us know. If that alone solves the problem, please close this issue once your app is working again. We'll hopefully have documentation for these changes posted soon. |
sounds like part of what we need to do is have two base resources
and mix in any common stuff we want 'cause right now I guess we need to patch the JR::Resource to handle where it assumes the resource is active-record based? |
(the guides for 0.10.x need all the removed stuff removed from it.. better to be incomplete and prs accepted than wrong :) |
Looks like PORO support is generally pretty broken in 0.10.x as well as association overrides. e.g. cerebris/jsonapi-resources#1045 (comment) I'm going to stop working on the upgrade now. I ended up with something like class PoroResource < JSONAPI::BasicResource
root_resource
# see https://github.com/cerebris/jsonapi-resources/blob/cc9679302b7989018aad47cc04b9112f9422ffff/test/fixtures/active_record.rb
class << self
def find_records(filters, options)
fail NotImplementedError, <<~EOF
Should be something like
def find_records(filters, options)
breeds = []
id_filter = filters[:id]
id_filter = [id_filter] unless id_filter.nil? || id_filter.is_a?(Array)
$breed_data.breeds.values.each do |breed|
breeds.push(breed) unless id_filter && !id_filter.include?(breed.id)
end
breeds
end
EOF
end
def find_record_by_key(key, options = {})
fail NotImplementedError, <<~EOF
Should be something like
def find_record_by_key(key, options = {})
$breed_data.breeds[key.to_i]
end
EOF
end
def find_records_by_keys(keys, options = {})
fail NotImplementedError, <<~EOF
Should be something like
def find_records_by_keys(keys, options = {})
breeds = []
keys.each do |key|
breeds.push($breed_data.breeds[key.to_i])
end
breeds
end
EOF
end
# Records
# NOTE(BF) this seems to be the source of all our pain
def find_fragments(filters, options = {})
fragments = {}
find_records(filters, options).each do |record|
rid = JSONAPI::ResourceIdentity.new(resource_klass, record.id)
fragments[rid] = JSONAPI::ResourceFragment.new(rid)
end
fragments
end
def resource_klass
self
end
# Finds Resources using the `filters`. Pagination and sort options are used when provided
#
# @param filters [Hash] the filters hash
# @option options [Hash] :context The context of the request, set in the controller
# @option options [Hash] :sort_criteria The `sort criteria`
# @option options [Hash] :include_directives The `include_directives`
#
# @return [Array<Resource>] the Resource instances matching the filters, sorting and pagination rules.
def find(filters, options = {})
records = find_breeds(filters, options)
resources_for(records, options[:context])
end
# # Counts Resources found using the `filters`
# #
# # @param filters [Hash] the filters hash
# # @option options [Hash] :context The context of the request, set in the controller
# #
# # @return [Integer] the count
# def count(filters, options = {})
# count_records(records)
# end
# Returns the single Resource identified by `key`
#
# @param key the primary key of the resource to find
# @option options [Hash] :context The context of the request, set in the controller
def find_by_key(key, options = {})
record = find_record_by_key(key, options)
resource_for(record, options[:context])
end
def find_to_populate_by_keys(keys, options = {})
find_by_keys(keys, options)
end
# Returns an array of Resources identified by the `keys` array
#
# @param keys [Array<key>] Array of primary keys to find resources for
# @option options [Hash] :context The context of the request, set in the controller
def find_by_keys(keys, options = {})
records = find_records_by_keys(keys, options)
resources_for(records, options[:context])
end
# This resource class (ActiveRelationResource) uses an `ActiveRecord::Relation` as the starting point for
# retrieving models. From this relation filters, sorts and joins are applied as needed.
# Depending on which phase of the request processing different `records` methods will be called, giving the user
# the opportunity to override them differently for performance and security reasons.
# begin `records`methods
# Base for the `records` methods that follow and is not directly used for accessing model data by this class.
# Overriding this method gives a single place to affect the `ActiveRecord::Relation` used for the resource.
#
# @option options [Hash] :context The context of the request, set in the controller
#
# @return [ActiveRecord::Relation]
def records_base(_options = {})
raise 'not used'
# _model_class.all
end
# The `ActiveRecord::Relation` used for finding user requested models. This may be overridden to enforce
# permissions checks on the request.
#
# @option options [Hash] :context The context of the request, set in the controller
#
# @return [ActiveRecord::Relation]
def records(options = {})
raise 'not used'
# records_base(options)
end
# The `ActiveRecord::Relation` used for populating the ResourceSet. Only resources that have been previously
# identified through the `records` method will be accessed. Thus it should not be necessary to reapply permissions
# checks. However if the model needs to include other models adding `includes` is appropriate
#
# @option options [Hash] :context The context of the request, set in the controller
#
# @return [ActiveRecord::Relation]
def records_for_populate(options = {})
raise 'not used'
# records_base(options)
end
# The `ActiveRecord::Relation` used for the finding related resources. Only resources that have been previously
# identified through the `records` method will be accessed and used as the basis to find related resources. Thus
# it should not be necessary to reapply permissions checks.
#
# @option options [Hash] :context The context of the request, set in the controller
#
# @return [ActiveRecord::Relation]
def records_for_source_to_related(options = {})
raise 'not used'
# records_base(options)
end
# end `records` methods
# def apply_join(records:, relationship:, resource_type:, join_type:, options:)
# def relationship_records(relationship:, join_type: :inner, resource_type: nil, options: {})
# def join_relationship(records:, relationship:, resource_type: nil, join_type: :inner, options: {})
end
end
class BasePlainResource < PoroResource
include PlainOldResource
end and module PlainOldResource
module Extensions
# snip
+ def find_records(filters, options)
+ []
+ end
# snip
end
extend ActiveSupport::Concern
end getting a 201 on the POST but an empty data attribute in the response. Seems to have to do with |
In v0.10, the operation processing and the response generation are two distinct processes. This is why you need to find the created model now. Since you've explicitly defined If the resources are actually findable, you should implement a proper |
We're upgrading from 0.9.10 to 0.10.1
in one of our acceptance specs we saw a failure
it turns out, it's added a
filter[:id] = "57630ec8-3332-4be3-8421-6de45545f294"
where that uuid is coming from the resource identifier.the reason there's an
[]
is that this is a plain old resource where we, naturally, cannot return an active record relation, but instead are returning an empty collection.I've tried fiddling with class methods
self.records
,self.records_base
,self.apply_filter
,self.apply_join
and instance methodsrecords_for
,records
,records_base
and stepped through quite a bunch of code, but haven't been able to figure out what exactly changed or how to handle ite.g.
probably this is related to cerebris/jsonapi-resources#1221 ?
The 0.9.x resource in question is defined something like below (we only interact with it by POST)
The text was updated successfully, but these errors were encountered: