mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Make Serializable module which implements as_json
This commit is contained in:
parent
e6993c677b
commit
7ba05c02e3
@ -1,7 +1,10 @@
|
||||
require 'active_model/serializable'
|
||||
require 'active_model/serializer'
|
||||
|
||||
module ActiveModel
|
||||
class ArraySerializer
|
||||
include Serializable
|
||||
|
||||
class << self
|
||||
attr_accessor :_root
|
||||
|
||||
@ -31,15 +34,5 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
alias serializable_object serializable_array
|
||||
|
||||
def as_json(options={})
|
||||
if root = options[:root] || self.root
|
||||
hash = { root.to_s => serializable_array }
|
||||
hash[meta_key.to_s] = meta if meta
|
||||
hash
|
||||
else
|
||||
serializable_array
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
13
lib/active_model/serializable.rb
Normal file
13
lib/active_model/serializable.rb
Normal file
@ -0,0 +1,13 @@
|
||||
module ActiveModel
|
||||
module Serializable
|
||||
def as_json(options={})
|
||||
if root = options[:root] || self.root
|
||||
hash = { root.to_s => serializable_object }
|
||||
hash[meta_key.to_s] = meta if meta
|
||||
hash
|
||||
else
|
||||
serializable_object
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,8 +1,11 @@
|
||||
require 'active_model/array_serializer'
|
||||
require 'active_model/serializable'
|
||||
require 'active_model/serializer/associations'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
include Serializable
|
||||
|
||||
class << self
|
||||
def inherited(base)
|
||||
base._attributes = []
|
||||
@ -120,15 +123,5 @@ module ActiveModel
|
||||
hash.merge! associations
|
||||
end
|
||||
alias serializable_object serializable_hash
|
||||
|
||||
def as_json(options={})
|
||||
if root = options[:root] || self.root
|
||||
hash = { root.to_s => serializable_hash }
|
||||
hash[meta_key.to_s] = meta if meta
|
||||
hash
|
||||
else
|
||||
serializable_hash
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user