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

Cascade key transformation only cascades for one level #260

Closed
trevorturk opened this issue Oct 24, 2022 · 3 comments · Fixed by #263
Closed

Cascade key transformation only cascades for one level #260

trevorturk opened this issue Oct 24, 2022 · 3 comments · Fixed by #263
Labels
bug Something isn't working

Comments

@trevorturk
Copy link
Collaborator

Describe the bug

Apologies for not testing this myself sooner, but I think we have a bug left over from #220, where transform_keys only cascades for one level.

To Reproduce

Given the following code sample, note that the articles array has the createdAt key, but the comments array has the created_at key, which has not been transformed:

class User
  attr_accessor :created_at, :articles

  def initialize
    @created_at = Time.now
  end
end

class Article
  attr_accessor :created_at, :comments

  def initialize
    @created_at = Time.now
  end
end

class Comment
  attr_accessor :created_at

  def initialize
    @created_at = Time.now
  end
end

class UserResource
  include Alba::Resource

  transform_keys :lower_camel

  attributes :created_at

  association :articles do
    attributes :created_at

    association :comments do
      attributes :created_at
    end
  end
end

user = User.new
comment = Comment.new
article = Article.new
article.comments = [comment]
user.articles = [article]

UserResource.new(user).to_h
# => 
{"createdAt"=>2022-10-24 15:23:10.190596 -0500,
 "articles"=>[{"createdAt"=>2022-10-24 15:23:10.203395 -0500, "comments"=>[{"created_at"=>2022-10-24 15:23:10.198995 -0500}]}]}
@trevorturk trevorturk added the bug Something isn't working label Oct 24, 2022
@okuramasafumi
Copy link
Owner

@trevorturk Thank you for reporting, I'll take a look.

@okuramasafumi
Copy link
Owner

@trevorturk #263 was a solution, but it was hard to debug. I described the problem and solution in the PR description so please check it if you're interested.
In short, there's an implicit requirement that transform_keys should be called before attributes. This might not be a problem for daily development, but in this case it's a trick issue.

@trevorturk
Copy link
Collaborator Author

Ack! Sorry the fix was so difficult, but I did test your PR/branch in my app and everything works as expected! Thank you so much! I have a lot of nested attributes, so this removes a lot of repeated transform_keys keys calls and my resources look much skinnier now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants