mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Move reusable code to a module
This commit is contained in:
parent
64ed05c484
commit
0e876624ec
@ -1,3 +1,4 @@
|
|||||||
|
require 'active_model/serializable'
|
||||||
require "active_support/core_ext/class/attribute"
|
require "active_support/core_ext/class/attribute"
|
||||||
require 'active_support/dependencies'
|
require 'active_support/dependencies'
|
||||||
require 'active_support/descendants_tracker'
|
require 'active_support/descendants_tracker'
|
||||||
@ -15,6 +16,8 @@ module ActiveModel
|
|||||||
class ArraySerializer
|
class ArraySerializer
|
||||||
extend ActiveSupport::DescendantsTracker
|
extend ActiveSupport::DescendantsTracker
|
||||||
|
|
||||||
|
include ActiveModel::Serializable
|
||||||
|
|
||||||
attr_reader :object, :options
|
attr_reader :object, :options
|
||||||
|
|
||||||
class_attribute :root
|
class_attribute :root
|
||||||
@ -33,35 +36,8 @@ module ActiveModel
|
|||||||
@object, @options = object, options
|
@object, @options = object, options
|
||||||
end
|
end
|
||||||
|
|
||||||
def meta_key
|
def serialize
|
||||||
@options[:meta_key].try(:to_sym) || :meta
|
serializable_array
|
||||||
end
|
|
||||||
|
|
||||||
def include_meta(hash)
|
|
||||||
hash[meta_key] = @options[:meta] if @options.has_key?(:meta)
|
|
||||||
end
|
|
||||||
|
|
||||||
def as_json(*args)
|
|
||||||
@options[:hash] = hash = {}
|
|
||||||
@options[:unique_values] = {}
|
|
||||||
|
|
||||||
if root = @options[:root]
|
|
||||||
hash.merge!(root => serializable_array)
|
|
||||||
include_meta hash
|
|
||||||
hash
|
|
||||||
else
|
|
||||||
serializable_array
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_json(*args)
|
|
||||||
if perform_caching?
|
|
||||||
cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'to-json']) do
|
|
||||||
super
|
|
||||||
end
|
|
||||||
else
|
|
||||||
super
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializable_array
|
def serializable_array
|
||||||
|
|||||||
34
lib/active_model/serializable.rb
Normal file
34
lib/active_model/serializable.rb
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
module ActiveModel
|
||||||
|
module Serializable
|
||||||
|
def meta_key
|
||||||
|
options[:meta_key].try(:to_sym) || :meta
|
||||||
|
end
|
||||||
|
|
||||||
|
def include_meta(hash)
|
||||||
|
hash[meta_key] = options[:meta] if options.has_key?(:meta)
|
||||||
|
end
|
||||||
|
|
||||||
|
def as_json(*args)
|
||||||
|
options[:hash] = hash = {}
|
||||||
|
options[:unique_values] = {}
|
||||||
|
|
||||||
|
if root = options[:root]
|
||||||
|
hash.merge!(root => serialize)
|
||||||
|
include_meta hash
|
||||||
|
hash
|
||||||
|
else
|
||||||
|
serialize
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_json(*args)
|
||||||
|
if perform_caching?
|
||||||
|
cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'to-json']) do
|
||||||
|
super
|
||||||
|
end
|
||||||
|
else
|
||||||
|
super
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user