active_model_serializers/lib
Grey Baker be7ee70376 Allow serialized ID to be overwritten for belongs-to relationships
If the `id` attribute for a class isn't taken directly from the object when
serializing it, it may be desirible for other classes that serialize a
relationship with that class to overwrite the relationship IDs they serialize.

For example, suppose we have:

```(ruby)
class Repo < Model
  attributes :id, :github_id, :name
  associations :configs
end

class Config < Model
  attributes :id
  belongs_to :repo
end

class RepoSerializer < ActiveModel::Serializer
  attributes :id, :name

  has_many :update_configs

  def id
    object.github_id
  end
end

class ConfigSerializer < ActiveModel::Serializer
  attributes :id
  belongs_to :repo
end
```

In the above example, serializing a list of `Repo`s will give the `github_id`
for each one, but serializing a `Config` will give the `id` for its parent repo.

Ideally AMS would inspect the `RepoSerializer` when serializing the `Config`,
and realise it can't just output the foreign key. Unfortunately, getting the
serialization class for the child repo currently requires loading the record
(via evaluating `lazy_assocation`), and loses the performance benefit of the
existing `belongs_to?` path. Instead, I've opted to use
`read_attribute_for_serialization` instead of `object.send` to fetch the
serialized foreign key. This allows the serialized relationship ID to be
overwritten using

```(ruby)
class ConfigSerializer < ActiveModel::Serializer
  ...

  def repo_id
    object.repo.github_id
  end
end
```
2017-05-13 15:22:18 +01:00
..
action_controller This adds namespace lookup to serializer_for (#1968) 2016-11-09 07:57:39 -05:00
active_model Bump to v0.10.6 2017-05-01 10:59:14 -05:00
active_model_serializers Allow serialized ID to be overwritten for belongs-to relationships 2017-05-13 15:22:18 +01:00
generators/rails Fix thor warning to set type: :boolean, was setting { banner: "" } 2017-01-06 17:16:57 -06:00
grape re: RuboCop - Use nested module/class definition instead of compact style. 2016-06-20 22:15:20 +01:00
tasks Add rubocop binstub that rspects file patterns 2017-03-12 15:50:05 -05:00
active_model_serializers.rb Make test attributes explicit 2016-12-04 19:33:39 -06:00