active_model_serializers/test
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 Return null resource object identifier for blank id 2017-04-30 23:28:08 -05:00
active_model_serializers Move Rails.application.config into configure block for test rails app. 2017-02-06 15:52:53 -05:00
adapter Merge 0-10-stable into master (to fix breaking change). (#2023) 2017-01-10 02:28:50 -06:00
benchmark Swap out KeyTransform for CaseTransform (#1993) 2016-12-07 10:49:38 -05:00
fixtures Merge 0-10-stable into master (to fix breaking change). (#2023) 2017-01-10 02:28:50 -06:00
generators Fix method redefined warning 2017-01-06 17:14:56 -06:00
serializers Allow serialized ID to be overwritten for belongs-to relationships 2017-05-13 15:22:18 +01:00
support Merge pull request #2046 from leonelgalan/bug_filter_parameters 2017-02-16 11:55:23 -06:00
active_record_test.rb Favor ActiveSupport::TestCase over Minitest::Test 2015-12-22 10:35:51 -06:00
adapter_test.rb re: RuboCop - get rid of redundant curly braces around a hash parameter 2016-06-20 22:14:39 +01:00
array_serializer_test.rb Drop support for Rails 4.0 and Ruby 2.0.0 2016-04-04 11:41:35 -06:00
cache_test.rb Merge 0-10-stable into master (to fix breaking change). (#2023) 2017-01-10 02:28:50 -06:00
collection_serializer_test.rb Make test attributes explicit 2016-12-04 19:33:39 -06:00
grape_test.rb Silence Grape warnings 2017-01-06 17:18:08 -06:00
lint_test.rb re: RuboCop: Bulk minor style corrections 2016-06-20 22:12:16 +01:00
logger_test.rb re: RuboCop - Use nested module/class definition instead of compact style. 2016-06-20 22:15:20 +01:00
poro_test.rb Favor ActiveSupport::TestCase over Minitest::Test 2015-12-22 10:35:51 -06:00
serializable_resource_test.rb re: RuboCop - get rid of redundant curly braces around a hash parameter 2016-06-20 22:14:39 +01:00
test_helper.rb Silence Grape warnings 2017-01-06 17:18:08 -06:00