{"action":"created","issue":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1478","repository_url":"https://api.github.com/repos/rails-api/active_model_serializers","labels_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1478/labels{/name}","comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1478/comments","events_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1478/events","html_url":"https://github.com/rails-api/active_model_serializers/pull/1478","id":129594873,"number":1478,"title":"Trigger callback to set serializer#_cache when controller loaded","user":{"login":"bf4","id":142914,"avatar_url":"https://avatars.githubusercontent.com/u/142914?v=3","gravatar_id":"","url":"https://api.github.com/users/bf4","html_url":"https://github.com/bf4","followers_url":"https://api.github.com/users/bf4/followers","following_url":"https://api.github.com/users/bf4/following{/other_user}","gists_url":"https://api.github.com/users/bf4/gists{/gist_id}","starred_url":"https://api.github.com/users/bf4/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bf4/subscriptions","organizations_url":"https://api.github.com/users/bf4/orgs","repos_url":"https://api.github.com/users/bf4/repos","events_url":"https://api.github.com/users/bf4/events{/privacy}","received_events_url":"https://api.github.com/users/bf4/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/Bug","name":"Bug","color":"e11d21"},{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/C:%20Caching","name":"C: Caching","color":"CCCCCC"},{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/Needs%20Team%20Discussion","name":"Needs Team Discussion","color":"5319e7"},{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/V:%200.10.x","name":"V: 0.10.x","color":"bfd4f2"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2016-01-28T21:59:27Z","updated_at":"2016-03-16T11:42:03Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1478","html_url":"https://github.com/rails-api/active_model_serializers/pull/1478","diff_url":"https://github.com/rails-api/active_model_serializers/pull/1478.diff","patch_url":"https://github.com/rails-api/active_model_serializers/pull/1478.patch"},"body":"This is kind of a dumb fix for caching that demonstrates the\r\nneed in Rails for Serializer::_cache to be set after the controller is loaded\r\nwhen Serializer::cache has already been called.\r\n\r\n\r\n## Notes on the issue\r\n\r\n```ruby\r\n# Initialize app before any serializers are defined, for sanity's sake.\r\n# Otherwise, you have to manually set perform caching.\r\n#\r\n# Details:\r\n#\r\n# 1. Upon load, when AMS.config.perform_caching is true,\r\n#    serializers inherit the cache store from ActiveModelSerializers.config.cache_store\r\n# 1. If the serializers are loaded before Rails is initialized (`Rails.application.initialize!`),\r\n#    these values are nil, and are not applied to the already loaded serializers\r\n# 1. If Rails is initialized before any serializers are loaded, then the configs are set,\r\n#    and are used when serializers are loaded\r\n# 1. In either case, `ActiveModelSerializers.config.cache_store`, and\r\n#    `ActiveModelSerializers.config.perform_caching` can be set at any time before the serializers\r\n#    are loaded,\r\n#    e.g.  `ActiveModel::Serializer.config.cache_store ||=\r\n#      ActiveSupport::Cache.lookup_store(ActionController::Base.cache_store ||\r\n#      Rails.cache || :memory_store)`\r\n#    and `ActiveModelSerializers.config.perform_caching = true`\r\n# 1. If the serializers are loaded before Rails is initialized, then,\r\n#    you can set the `_cache` store directly on the serializers.\r\n#    `ActiveModel::Serializer._cache ||=\r\n#      ActiveSupport::Cache.lookup_store(ActionController::Base.cache_store ||\r\n#      Rails.cache || :memory_store`\r\n#    is sufficient.\r\n#    Setting `_cache` to a truthy value will cause the CachedSerializer\r\n#    to consider it cached, which will apply to all serializers (bug? :bug: )\r\n#\r\n# This happens, in part, because the cache store is set for a serializer\r\n# when `cache` is called, and cache is usually called when the serializer is defined.\r\n#\r\n# So, there's now a 'workaround', something to debug, and a starting point.\r\nRails.application.initialize!\r\n\r\n# HACK: Serializer::cache depends on the ActionController-dependent configs being set.\r\nActiveSupport.on_load(:action_controller) do\r\n  require_relative 'fixtures'\r\nend\r\n```"},"comment":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/comments/197278571","html_url":"https://github.com/rails-api/active_model_serializers/pull/1478#issuecomment-197278571","issue_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1478","id":197278571,"user":{"login":"NullVoxPopuli","id":199018,"avatar_url":"https://avatars.githubusercontent.com/u/199018?v=3","gravatar_id":"","url":"https://api.github.com/users/NullVoxPopuli","html_url":"https://github.com/NullVoxPopuli","followers_url":"https://api.github.com/users/NullVoxPopuli/followers","following_url":"https://api.github.com/users/NullVoxPopuli/following{/other_user}","gists_url":"https://api.github.com/users/NullVoxPopuli/gists{/gist_id}","starred_url":"https://api.github.com/users/NullVoxPopuli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NullVoxPopuli/subscriptions","organizations_url":"https://api.github.com/users/NullVoxPopuli/orgs","repos_url":"https://api.github.com/users/NullVoxPopuli/repos","events_url":"https://api.github.com/users/NullVoxPopuli/events{/privacy}","received_events_url":"https://api.github.com/users/NullVoxPopuli/received_events","type":"User","site_admin":false},"created_at":"2016-03-16T11:42:02Z","updated_at":"2016-03-16T11:42:02Z","body":"@bf4, could you rebase this and fix the rubocop warnings?"}}