mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Ported serializers.
This commit is contained in:
51
lib/action_controller/serialization.rb
Normal file
51
lib/action_controller/serialization.rb
Normal file
@@ -0,0 +1,51 @@
|
||||
module ActionController
|
||||
# Action Controller Serialization
|
||||
#
|
||||
# Overrides render :json to check if the given object implements +active_model_serializer+
|
||||
# as a method. If so, use the returned serializer instead of calling +to_json+ in the object.
|
||||
#
|
||||
# This module also provides a serialization_scope method that allows you to configure the
|
||||
# +serialization_scope+ of the serializer. Most apps will likely set the +serialization_scope+
|
||||
# to the current user:
|
||||
#
|
||||
# class ApplicationController < ActionController::Base
|
||||
# serialization_scope :current_user
|
||||
# end
|
||||
#
|
||||
# If you need more complex scope rules, you can simply override the serialization_scope:
|
||||
#
|
||||
# class ApplicationController < ActionController::Base
|
||||
# private
|
||||
#
|
||||
# def serialization_scope
|
||||
# current_user
|
||||
# end
|
||||
# end
|
||||
#
|
||||
module Serialization
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
include ActionController::Renderers
|
||||
|
||||
included do
|
||||
class_attribute :_serialization_scope
|
||||
end
|
||||
|
||||
def serialization_scope
|
||||
send(_serialization_scope)
|
||||
end
|
||||
|
||||
def _render_option_json(json, options)
|
||||
if json.respond_to?(:active_model_serializer) && (serializer = json.active_model_serializer)
|
||||
json = serializer.new(json, serialization_scope)
|
||||
end
|
||||
super
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def serialization_scope(scope)
|
||||
self._serialization_scope = scope
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
253
lib/active_model/serializer.rb
Normal file
253
lib/active_model/serializer.rb
Normal file
@@ -0,0 +1,253 @@
|
||||
require "active_support/core_ext/class/attribute"
|
||||
require "active_support/core_ext/string/inflections"
|
||||
require "active_support/core_ext/module/anonymous"
|
||||
require "set"
|
||||
|
||||
module ActiveModel
|
||||
# Active Model Array Serializer
|
||||
#
|
||||
# It serializes an array checking if each element that implements
|
||||
# the +active_model_serializer+ method passing down the current scope.
|
||||
class ArraySerializer
|
||||
attr_reader :object, :scope
|
||||
|
||||
def initialize(object, scope)
|
||||
@object, @scope = object, scope
|
||||
end
|
||||
|
||||
def serializable_array
|
||||
@object.map do |item|
|
||||
if item.respond_to?(:active_model_serializer) && (serializer = item.active_model_serializer)
|
||||
serializer.new(item, scope)
|
||||
else
|
||||
item
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def as_json(*args)
|
||||
serializable_array.as_json(*args)
|
||||
end
|
||||
end
|
||||
|
||||
# Active Model Serializer
|
||||
#
|
||||
# Provides a basic serializer implementation that allows you to easily
|
||||
# control how a given object is going to be serialized. On initialization,
|
||||
# it expects to object as arguments, a resource and a scope. For example,
|
||||
# one may do in a controller:
|
||||
#
|
||||
# PostSerializer.new(@post, current_user).to_json
|
||||
#
|
||||
# The object to be serialized is the +@post+ and the scope is +current_user+.
|
||||
#
|
||||
# We use the scope to check if a given attribute should be serialized or not.
|
||||
# For example, some attributes maybe only be returned if +current_user+ is the
|
||||
# author of the post:
|
||||
#
|
||||
# class PostSerializer < ActiveModel::Serializer
|
||||
# attributes :title, :body
|
||||
# has_many :comments
|
||||
#
|
||||
# private
|
||||
#
|
||||
# def attributes
|
||||
# hash = super
|
||||
# hash.merge!(:email => post.email) if author?
|
||||
# hash
|
||||
# end
|
||||
#
|
||||
# def author?
|
||||
# post.author == scope
|
||||
# end
|
||||
# end
|
||||
#
|
||||
class Serializer
|
||||
module Associations #:nodoc:
|
||||
class Config < Struct.new(:name, :options) #:nodoc:
|
||||
def serializer
|
||||
options[:serializer]
|
||||
end
|
||||
end
|
||||
|
||||
class HasMany < Config #:nodoc:
|
||||
def serialize(collection, scope)
|
||||
collection.map do |item|
|
||||
serializer.new(item, scope).serializable_hash
|
||||
end
|
||||
end
|
||||
|
||||
def serialize_ids(collection, scope)
|
||||
# use named scopes if they are present
|
||||
# return collection.ids if collection.respond_to?(:ids)
|
||||
|
||||
collection.map do |item|
|
||||
item.read_attribute_for_serialization(:id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class HasOne < Config #:nodoc:
|
||||
def serialize(object, scope)
|
||||
object && serializer.new(object, scope).serializable_hash
|
||||
end
|
||||
|
||||
def serialize_ids(object, scope)
|
||||
object && object.read_attribute_for_serialization(:id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class_attribute :_attributes
|
||||
self._attributes = Set.new
|
||||
|
||||
class_attribute :_associations
|
||||
self._associations = []
|
||||
|
||||
class_attribute :_root
|
||||
class_attribute :_embed
|
||||
self._embed = :objects
|
||||
class_attribute :_root_embed
|
||||
|
||||
class << self
|
||||
# Define attributes to be used in the serialization.
|
||||
def attributes(*attrs)
|
||||
self._attributes += attrs
|
||||
end
|
||||
|
||||
def associate(klass, attrs) #:nodoc:
|
||||
options = attrs.extract_options!
|
||||
self._associations += attrs.map do |attr|
|
||||
unless method_defined?(attr)
|
||||
class_eval "def #{attr}() object.#{attr} end", __FILE__, __LINE__
|
||||
end
|
||||
|
||||
options[:serializer] ||= const_get("#{attr.to_s.camelize}Serializer")
|
||||
klass.new(attr, options)
|
||||
end
|
||||
end
|
||||
|
||||
# Defines an association in the object should be rendered.
|
||||
#
|
||||
# The serializer object should implement the association name
|
||||
# as a method which should return an array when invoked. If a method
|
||||
# with the association name does not exist, the association name is
|
||||
# dispatched to the serialized object.
|
||||
def has_many(*attrs)
|
||||
associate(Associations::HasMany, attrs)
|
||||
end
|
||||
|
||||
# Defines an association in the object should be rendered.
|
||||
#
|
||||
# The serializer object should implement the association name
|
||||
# as a method which should return an object when invoked. If a method
|
||||
# with the association name does not exist, the association name is
|
||||
# dispatched to the serialized object.
|
||||
def has_one(*attrs)
|
||||
associate(Associations::HasOne, attrs)
|
||||
end
|
||||
|
||||
# Define how associations should be embedded.
|
||||
#
|
||||
# embed :objects # Embed associations as full objects
|
||||
# embed :ids # Embed only the association ids
|
||||
# embed :ids, :include => true # Embed the association ids and include objects in the root
|
||||
#
|
||||
def embed(type, options={})
|
||||
self._embed = type
|
||||
self._root_embed = true if options[:include]
|
||||
end
|
||||
|
||||
# Defines the root used on serialization. If false, disables the root.
|
||||
def root(name)
|
||||
self._root = name
|
||||
end
|
||||
|
||||
def inherited(klass) #:nodoc:
|
||||
return if klass.anonymous?
|
||||
|
||||
name = klass.name.demodulize.underscore.sub(/_serializer$/, '')
|
||||
|
||||
klass.class_eval do
|
||||
alias_method name.to_sym, :object
|
||||
root name.to_sym unless self._root == false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :object, :scope
|
||||
|
||||
def initialize(object, scope)
|
||||
@object, @scope = object, scope
|
||||
end
|
||||
|
||||
# Returns a json representation of the serializable
|
||||
# object including the root.
|
||||
def as_json(*)
|
||||
if _root
|
||||
hash = { _root => serializable_hash }
|
||||
hash.merge!(associations) if _root_embed
|
||||
hash
|
||||
else
|
||||
serializable_hash
|
||||
end
|
||||
end
|
||||
|
||||
# Returns a hash representation of the serializable
|
||||
# object without the root.
|
||||
def serializable_hash
|
||||
if _embed == :ids
|
||||
attributes.merge(association_ids)
|
||||
elsif _embed == :objects
|
||||
attributes.merge(associations)
|
||||
else
|
||||
attributes
|
||||
end
|
||||
end
|
||||
|
||||
# Returns a hash representation of the serializable
|
||||
# object associations.
|
||||
def associations
|
||||
hash = {}
|
||||
|
||||
_associations.each do |association|
|
||||
associated_object = send(association.name)
|
||||
hash[association.name] = association.serialize(associated_object, scope)
|
||||
end
|
||||
|
||||
hash
|
||||
end
|
||||
|
||||
# Returns a hash representation of the serializable
|
||||
# object associations ids.
|
||||
def association_ids
|
||||
hash = {}
|
||||
|
||||
_associations.each do |association|
|
||||
associated_object = send(association.name)
|
||||
hash[association.name] = association.serialize_ids(associated_object, scope)
|
||||
end
|
||||
|
||||
hash
|
||||
end
|
||||
|
||||
# Returns a hash representation of the serializable
|
||||
# object attributes.
|
||||
def attributes
|
||||
hash = {}
|
||||
|
||||
_attributes.each do |name|
|
||||
hash[name] = @object.read_attribute_for_serialization(name)
|
||||
end
|
||||
|
||||
hash
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Array
|
||||
# Array uses ActiveModel::ArraySerializer.
|
||||
def active_model_serializer
|
||||
ActiveModel::ArraySerializer
|
||||
end
|
||||
end
|
||||
28
lib/active_model_serializers.rb
Normal file
28
lib/active_model_serializers.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
require "active_model"
|
||||
require "active_model/serializer"
|
||||
|
||||
ActiveModel::Serialization.class_eval do
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
module ClassMethods #:nodoc:
|
||||
def active_model_serializer
|
||||
return @active_model_serializer if defined?(@active_model_serializer)
|
||||
@active_model_serializer = "#{self.name}Serializer".safe_constantize
|
||||
end
|
||||
end
|
||||
|
||||
# Returns a model serializer for this object considering its namespace.
|
||||
def active_model_serializer
|
||||
self.class.active_model_serializer
|
||||
end
|
||||
end
|
||||
|
||||
require "action_controller"
|
||||
|
||||
module ActionController
|
||||
autoload :Serialization, "action_controller/serialization"
|
||||
end
|
||||
|
||||
ActiveSupport.on_load(:action_controller) do
|
||||
include ::ActionController::Serialization
|
||||
end
|
||||
9
lib/rails/generators/rails/serializer/USAGE
Normal file
9
lib/rails/generators/rails/serializer/USAGE
Normal file
@@ -0,0 +1,9 @@
|
||||
Description:
|
||||
Generates a serializer for the given resource with tests.
|
||||
|
||||
Example:
|
||||
`rails generate serializer Account name created_at`
|
||||
|
||||
For TestUnit it creates:
|
||||
Serializer: app/serializers/account_serializer.rb
|
||||
TestUnit: test/unit/account_serializer_test.rb
|
||||
@@ -0,0 +1,39 @@
|
||||
module Rails
|
||||
module Generators
|
||||
class SerializerGenerator < NamedBase
|
||||
check_class_collision :suffix => "Serializer"
|
||||
|
||||
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
||||
|
||||
class_option :parent, :type => :string, :desc => "The parent class for the generated serializer"
|
||||
|
||||
def create_serializer_file
|
||||
template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
|
||||
end
|
||||
|
||||
hook_for :test_framework
|
||||
|
||||
private
|
||||
|
||||
def attributes_names
|
||||
attributes.select { |attr| !attr.reference? }.map { |a| a.name.to_sym }
|
||||
end
|
||||
|
||||
def association_names
|
||||
attributes.select { |attr| attr.reference? }.map { |a| a.name.to_sym }
|
||||
end
|
||||
|
||||
def parent_class_name
|
||||
if options[:parent]
|
||||
options[:parent]
|
||||
elsif (n = Rails::Generators.namespace) && n.const_defined?(:ApplicationSerializer)
|
||||
"ApplicationSerializer"
|
||||
elsif Object.const_defined?(:ApplicationSerializer)
|
||||
"ApplicationSerializer"
|
||||
else
|
||||
"ActiveModel::Serializer"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,9 @@
|
||||
<% module_namespacing do -%>
|
||||
class <%= class_name %>Serializer < <%= parent_class_name %>
|
||||
<% if attributes.any? -%> attributes <%= attributes_names.map(&:inspect).join(", ") %>
|
||||
<% end -%>
|
||||
<% association_names.each do |attribute| -%>
|
||||
has_one :<%= attribute %>
|
||||
<% end -%>
|
||||
end
|
||||
<% end -%>
|
||||
@@ -0,0 +1,13 @@
|
||||
require 'rails/generators/test_unit'
|
||||
|
||||
module TestUnit
|
||||
module Generators
|
||||
class SerializerGenerator < Base
|
||||
check_class_collision :suffix => "SerializerTest"
|
||||
|
||||
def create_test_files
|
||||
template 'unit_test.rb', File.join('test/unit', class_path, "#{file_name}_serializer_test.rb")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,9 @@
|
||||
require 'test_helper'
|
||||
|
||||
<% module_namespacing do -%>
|
||||
class <%= class_name %>SerializerTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
||||
<% end -%>
|
||||
Reference in New Issue
Block a user