active_model_serializers/lib/active_model_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
..
adapter Allow serialized ID to be overwritten for belongs-to relationships 2017-05-13 15:22:18 +01:00
test Fixes bug in Test::Schema when using filter_parameters 2017-02-06 14:58:36 -05:00
adapter.rb Improve Coverage 2016-06-07 01:50:03 -05:00
callbacks.rb Move SerializableResource to ActiveModelSerializers namespace 2016-03-30 11:33:04 +02:00
deprecate.rb re: RuboCop: Bulk minor style corrections 2016-06-20 22:12:16 +01:00
deserialization.rb Improve Coverage 2016-06-07 01:50:03 -05:00
json_pointer.rb add an extra format token to the primary data string so that JRuby doesn't break 2016-03-06 20:05:25 -06:00
logging.rb Handle render.ams with nil serializer or adapter 2016-03-28 21:33:23 -05:00
lookup_chain.rb Make serializer lookup configurable (#1757) 2016-11-16 18:38:40 +01:00
model.rb Required 2017-04-30 17:56:13 -05:00
railtie.rb re: RuboCop - replace rocket style hashes 2016-06-20 22:14:12 +01:00
register_jsonapi_renderer.rb re: RuboCop - Use nested module/class definition instead of compact style. 2016-06-20 22:15:20 +01:00
serializable_resource.rb This adds namespace lookup to serializer_for (#1968) 2016-11-09 07:57:39 -05:00
serialization_context.rb Fix #1759, Grape integration, adds serialization_context 2016-06-14 09:45:30 -05:00
test.rb Create assert_response_schema test helper 2016-01-15 00:45:56 -06:00