active_model_serializers/test/serializers
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
..
association_macros_test.rb remove dynamic class creation where not needed (#1850) 2016-07-18 14:11:09 -05:00
associations_test.rb Allow serialized ID to be overwritten for belongs-to relationships 2017-05-13 15:22:18 +01:00
attribute_test.rb Make test attributes explicit 2016-12-04 19:33:39 -06:00
attributes_test.rb re: RuboCop - get rid of redundant curly braces around a hash parameter 2016-06-20 22:14:39 +01:00
caching_configuration_test_isolated.rb Fix MT6 assert_nil warnings 2017-01-06 22:06:23 -06:00
configuration_test.rb Favor ActiveSupport::TestCase over Minitest::Test 2015-12-22 10:35:51 -06:00
fieldset_test.rb re: RuboCop - replace rocket style hashes 2016-06-20 22:14:12 +01:00
meta_test.rb Fix RuboCop 0.40 linter errors (#1722) 2016-05-26 12:58:05 -04:00
options_test.rb Make test attributes explicit 2016-12-04 19:33:39 -06:00
read_attribute_for_serialization_test.rb Better AMS Model attributes interface 2016-11-21 09:14:26 -06:00
reflection_test.rb Update reflection tests 2017-04-30 16:39:25 -05:00
root_test.rb re: RuboCop - get rid of redundant curly braces around a hash parameter 2016-06-20 22:14:39 +01:00
serialization_test.rb Better AMS Model attributes interface 2016-11-21 09:14:26 -06:00
serializer_for_test.rb Fix MT6 assert_nil warnings 2017-01-06 22:06:23 -06:00
serializer_for_with_namespace_test.rb Make test attributes explicit 2016-12-04 19:33:39 -06:00