Skip to content

Reduce need to work arounds #337

Description

@code-bunny

While perhaps better as three separate issue, they are all interconnected so I will group them into one group of bugs.

Custom endpoints don't respond to route_format changes. We have fixed this with

  JsonApiClient::Query::Requestor.class_eval do
    def custom(method_name, options, params)
      path = resource_path(params)
      params.delete(klass.primary_key)
      path = File.join(path, method_name.to_s)
      request_method = options.fetch(:request_method, :get).to_sym
      query_params, body_params = [:get, :delete].include?(request_method) ? [params, nil] : [nil, params]
      path = path.dasherize if klass.route_format == :dasherized_route
      request(request_method, path, params: query_params, body: body_params)
    end
  end

Has one associations has a bug from the latest code change, fixed by applying .underscore

  JsonApiClient::Associations::HasOne::Association.class_eval do
    def load_records(data)
      record_class = JsonApiClient::Utils.compute_type(klass, data["type"].underscore.classify)
      record_class.load id: data["id"]
    end
  end

And finally relationship_data_for was falling apart when there was no key? method on the relationship_definition which is triggered by assigning and array to a has_many relation which is fixed by making more if's as I am not too sure of all the inner workings

  JsonApiClient::Resource.class_eval do
    def relationship_data_for(name, relationship_definition)
      # look in included data
      if relationship_definition.try(:key?, "data")
        if relationships.attribute_changed?(name)
          return relation_objects_for(name, relationship_definition)
        else
          return included_data_for(name, relationship_definition)
        end
      else
        if relationships.attribute_changed?(name)
          return relationship_definition.to_a
        else
          return included_data_for(name, relationship_definition)
        end
      end

      url = relationship_definition["links"]["related"]
      if relationship_definition["links"] && url
        return association_for(name).data(url)
      end

      nil
    end
  end

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions