codenoble / cache-crispies

Speedy Rails JSON serialization with built-in caching
MIT License
156 stars 14 forks source link

Provide a through option to delegate attributes to nested objects #4

Closed adamcrown closed 4 years ago

adamcrown commented 5 years ago

Currently if you want to pull an attribute from a tested object, you need to do something like this.

class UserSerializer < CacheCrispies::Base
  serialize :manager_name

  def manager_name
    model.manager.name
  end
end

It would be nice to be able to do this more succinctly like the below or something similar.

class UserSerializer < CacheCrispies::Base
  serialize :manager_name, from: :name, through: :manager
  # or possibly
    serialize :manager_name, from: { manager: :name }
end

But testing needs to be done to make sure there is little to no performance impact.