Clean up notification code with some meta-prog

This commit is contained in:
Benjamin Fleischer
2015-11-08 15:49:50 -06:00
parent 21bb306d38
commit 360ecc88fe
5 changed files with 39 additions and 28 deletions

View File

@@ -4,6 +4,11 @@ module ActiveModel
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links])
include ActiveModelSerializers::Logging
delegate :serializable_hash, :as_json, :to_json, to: :adapter
notify :serializable_hash, :render
notify :as_json, :render
notify :to_json, :render
# Primary interface to composing a resource with a serializer and adapter.
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
def initialize(resource, options = {})
@@ -12,24 +17,6 @@ module ActiveModel
options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
end
def serializable_hash(*args)
run_callbacks :render do
adapter.serializable_hash(*args)
end
end
def as_json(*args)
run_callbacks :render do
adapter.as_json(*args)
end
end
def to_json(*args)
run_callbacks :render do
adapter.to_json(*args)
end
end
def serialization_scope=(scope)
serializer_opts[:scope] = scope
end

View File

@@ -1,4 +1,3 @@
require 'active_model_serializers'
require 'rails/railtie'
module ActiveModel