Coder Social home page Coder Social logo

Comments (5)

scottgonzalez avatar scottgonzalez commented on August 19, 2024

You won't want to use self.records as that is now specific to resources that are backed by ActiveRelation (records_for is also gone completely).

I believe you'll want to return the empty array from self.find now. If you actually do have models to return, you can use resources_for(array_of_models, context) to generate the correct response from self.find.

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.

from jsonapi-resources-site.

bf4 avatar bf4 commented on August 19, 2024

sounds like part of what we need to do is have two base resources

  1. class BaseResource < Resource
  2. class PlainOldBaseResource < BasicResource

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?

from jsonapi-resources-site.

bf4 avatar bf4 commented on August 19, 2024

find didn't help (yet?). Doesn't seem to be called

(the guides for 0.10.x need all the removed stuff removed from it.. better to be incomplete and prs accepted than wrong :)

from jsonapi-resources-site.

bf4 avatar bf4 commented on August 19, 2024

Looks like PORO support is generally pretty broken in 0.10.x as well as association overrides. e.g. cerebris/jsonapi-resources#1045 (comment)
and cerebris/jsonapi-resources#1244

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 find_fragments needing to 'find' the _model we're 'creating', but just not feeling any progress at this point.

from jsonapi-resources-site.

scottgonzalez avatar scottgonzalez commented on August 19, 2024

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 find_records to always return an empty set, it's not surprising that you're getting no data in your response.

If the resources are actually findable, you should implement a proper find_records method that will work for all requests. If the resources are not findable (perhaps they do not actually persist), you can store the created model in context in an after_create callback and then return that model in the finder (you can access context via the options argument).

from jsonapi-resources-site.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.