Skip to content
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

access to assigned relationship #335

Merged

Conversation

senid231
Copy link
Member

fixes #334

@senid231 senid231 self-assigned this Mar 19, 2019
@senid231 senid231 marked this pull request as ready for review March 25, 2019 13:21
@senid231 senid231 requested a review from gaorlov March 25, 2019 13:21
Copy link
Collaborator

@gaorlov gaorlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch! looks good

@senid231 senid231 merged commit e4b763f into JsonApiClient:master Mar 26, 2019
@@ -5,7 +5,12 @@ class Association < BaseAssociation
def from_result_set(result_set)
result_set.first
end

def load_records(data)
record_class = Utils.compute_type(klass, data["type"].classify)
Copy link

@code-bunny code-bunny Mar 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After testing I have found there is a bug here, when the payload is in dash case running classify on the type we get unexpected results. "skill-level". classify will return Skill-level. So what we need to do here is data["type"].underscore.classify

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If fixed this locally with

  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

@code-bunny
Copy link

To access has_many results we have a further issue, without knowing the in's and outs I have overridden the method. I am guessing calling to_a isn't quite right but it seems to work for now.

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Relations raise a error when added to a new record
3 participants