mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Move models and serializers in test to fixtures.rb file
This commit is contained in:
parent
0d3b56e9cf
commit
a25c352525
24
test/fixtures.rb
Normal file
24
test/fixtures.rb
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
class Model
|
||||||
|
include ActiveModel::SerializerSupport
|
||||||
|
|
||||||
|
def initialize(hash={})
|
||||||
|
@attributes = hash
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_attribute_for_serialization(name)
|
||||||
|
@attributes[name]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class ModelSerializer < ActiveModel::Serializer
|
||||||
|
attributes :attr1, :attr2
|
||||||
|
|
||||||
|
def attr2
|
||||||
|
attr2 = object.read_attribute_for_serialization(:attr2)
|
||||||
|
if scope
|
||||||
|
attr2 + '-' + scope
|
||||||
|
else
|
||||||
|
attr2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -4,22 +4,6 @@ require 'newbase/active_model_serializers'
|
|||||||
module ActionController
|
module ActionController
|
||||||
module Serialization
|
module Serialization
|
||||||
class ImplicitSerializerTest < ActionController::TestCase
|
class ImplicitSerializerTest < ActionController::TestCase
|
||||||
class Model
|
|
||||||
include ActiveModel::SerializerSupport
|
|
||||||
|
|
||||||
def initialize(hash={})
|
|
||||||
@attributes = hash
|
|
||||||
end
|
|
||||||
|
|
||||||
def read_attribute_for_serialization(name)
|
|
||||||
@attributes[name]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class ModelSerializer < ActiveModel::Serializer
|
|
||||||
attributes :attr1, :attr2
|
|
||||||
end
|
|
||||||
|
|
||||||
class MyController < ActionController::Base
|
class MyController < ActionController::Base
|
||||||
def render_using_implicit_serializer
|
def render_using_implicit_serializer
|
||||||
render :json => Model.new(attr1: 'value1', attr2: 'value2', attr3: 'value3')
|
render :json => Model.new(attr1: 'value1', attr2: 'value2', attr3: 'value3')
|
||||||
@ -36,32 +20,12 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
class ImplicitSerializerScopeTest < ActionController::TestCase
|
class ImplicitSerializerScopeTest < ActionController::TestCase
|
||||||
class Model
|
|
||||||
include ActiveModel::SerializerSupport
|
|
||||||
|
|
||||||
def initialize(hash={})
|
|
||||||
@attributes = hash
|
|
||||||
end
|
|
||||||
|
|
||||||
def read_attribute_for_serialization(name)
|
|
||||||
@attributes[name]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class ModelSerializer < ActiveModel::Serializer
|
|
||||||
attributes :attr1, :attr2
|
|
||||||
|
|
||||||
def attr2
|
|
||||||
object.read_attribute_for_serialization(:attr2) + '-' + scope
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class MyController < ActionController::Base
|
class MyController < ActionController::Base
|
||||||
def render_using_implicit_serializer_and_scope
|
def render_using_implicit_serializer_and_scope
|
||||||
render :json => Model.new(attr1: 'value1', attr2: 'value2', attr3: 'value3')
|
render :json => Model.new(attr1: 'value1', attr2: 'value2', attr3: 'value3')
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
private
|
||||||
|
|
||||||
def current_user
|
def current_user
|
||||||
'current_user'
|
'current_user'
|
||||||
@ -78,26 +42,6 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
class ExplicitSerializerScopeTest < ActionController::TestCase
|
class ExplicitSerializerScopeTest < ActionController::TestCase
|
||||||
class Model
|
|
||||||
include ActiveModel::SerializerSupport
|
|
||||||
|
|
||||||
def initialize(hash={})
|
|
||||||
@attributes = hash
|
|
||||||
end
|
|
||||||
|
|
||||||
def read_attribute_for_serialization(name)
|
|
||||||
@attributes[name]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class ModelSerializer < ActiveModel::Serializer
|
|
||||||
attributes :attr1, :attr2
|
|
||||||
|
|
||||||
def attr2
|
|
||||||
object.read_attribute_for_serialization(:attr2) + '-' + scope
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class MyController < ActionController::Base
|
class MyController < ActionController::Base
|
||||||
def render_using_implicit_serializer_and_explicit_scope
|
def render_using_implicit_serializer_and_explicit_scope
|
||||||
render json: Model.new(attr1: 'value1', attr2: 'value2', attr3: 'value3'), scope: current_admin
|
render json: Model.new(attr1: 'value1', attr2: 'value2', attr3: 'value3'), scope: current_admin
|
||||||
@ -124,26 +68,6 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
class OverridingSerializationScopeTest < ActionController::TestCase
|
class OverridingSerializationScopeTest < ActionController::TestCase
|
||||||
class Model
|
|
||||||
include ActiveModel::SerializerSupport
|
|
||||||
|
|
||||||
def initialize(hash={})
|
|
||||||
@attributes = hash
|
|
||||||
end
|
|
||||||
|
|
||||||
def read_attribute_for_serialization(name)
|
|
||||||
@attributes[name]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class ModelSerializer < ActiveModel::Serializer
|
|
||||||
attributes :attr1, :attr2
|
|
||||||
|
|
||||||
def attr2
|
|
||||||
object.read_attribute_for_serialization(:attr2) + '-' + scope
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class MyController < ActionController::Base
|
class MyController < ActionController::Base
|
||||||
def render_overriding_serialization_scope
|
def render_overriding_serialization_scope
|
||||||
render json: Model.new(attr1: 'value1', attr2: 'value2', attr3: 'value3')
|
render json: Model.new(attr1: 'value1', attr2: 'value2', attr3: 'value3')
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
require 'bundler/setup'
|
require 'bundler/setup'
|
||||||
require 'newbase/active_model_serializers'
|
require 'newbase/active_model_serializers'
|
||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
|
require 'newbase/fixtures'
|
||||||
|
|
||||||
module TestHelper
|
module TestHelper
|
||||||
Routes = ActionDispatch::Routing::RouteSet.new
|
Routes = ActionDispatch::Routing::RouteSet.new
|
||||||
|
|||||||
@ -4,20 +4,6 @@ require 'newbase/active_model/serializer'
|
|||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
class AttributesTest < ActiveModel::TestCase
|
class AttributesTest < ActiveModel::TestCase
|
||||||
class Model
|
|
||||||
def initialize(hash={})
|
|
||||||
@attributes = hash
|
|
||||||
end
|
|
||||||
|
|
||||||
def read_attribute_for_serialization(name)
|
|
||||||
@attributes[name]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class ModelSerializer < ActiveModel::Serializer
|
|
||||||
attributes :attr1, :attr2
|
|
||||||
end
|
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
model = Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' })
|
model = Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' })
|
||||||
@model_serializer = ModelSerializer.new(model)
|
@model_serializer = ModelSerializer.new(model)
|
||||||
|
|||||||
@ -4,24 +4,16 @@ require 'newbase/active_model/serializer'
|
|||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
class RootAsOptionTest < ActiveModel::TestCase
|
class RootAsOptionTest < ActiveModel::TestCase
|
||||||
class Model
|
|
||||||
def initialize(hash={})
|
|
||||||
@attributes = hash
|
|
||||||
end
|
|
||||||
|
|
||||||
def read_attribute_for_serialization(name)
|
|
||||||
@attributes[name]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class ModelSerializer < ActiveModel::Serializer
|
|
||||||
attributes :attr1, :attr2
|
|
||||||
end
|
|
||||||
ModelSerializer.root = true
|
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
|
@old_root = ModelSerializer._root
|
||||||
@model = Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' })
|
@model = Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' })
|
||||||
@serializer = ModelSerializer.new(@model, root: 'initialize')
|
@serializer = ModelSerializer.new(@model, root: 'initialize')
|
||||||
|
ModelSerializer._root = true
|
||||||
|
end
|
||||||
|
|
||||||
|
def teardown
|
||||||
|
ModelSerializer._root = @old_root
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_root_is_not_displayed_using_serializable_hash
|
def test_root_is_not_displayed_using_serializable_hash
|
||||||
|
|||||||
@ -4,19 +4,6 @@ require 'newbase/active_model/serializer'
|
|||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
class ScopeTest < ActiveModel::TestCase
|
class ScopeTest < ActiveModel::TestCase
|
||||||
class Model
|
|
||||||
def initialize(hash={})
|
|
||||||
@attributes = hash
|
|
||||||
end
|
|
||||||
|
|
||||||
def read_attribute_for_serialization(name)
|
|
||||||
@attributes[name]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class ModelSerializer < ActiveModel::Serializer
|
|
||||||
end
|
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@serializer = ModelSerializer.new(nil, scope: current_user)
|
@serializer = ModelSerializer.new(nil, scope: current_user)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -4,13 +4,6 @@ require 'newbase/active_model/serializer_support'
|
|||||||
module ActiveModel
|
module ActiveModel
|
||||||
module SerializerSupport
|
module SerializerSupport
|
||||||
class Test < ActiveModel::TestCase
|
class Test < ActiveModel::TestCase
|
||||||
class Model
|
|
||||||
include ActiveModel::SerializerSupport
|
|
||||||
end
|
|
||||||
|
|
||||||
class ModelSerializer < ActiveModel::Serializer
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_active_model_returns_its_serializer
|
def test_active_model_returns_its_serializer
|
||||||
assert_equal ModelSerializer, Model.new.active_model_serializer
|
assert_equal ModelSerializer, Model.new.active_model_serializer
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user