mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
* Configure adapter using ActiveModel::Serializer.config.adapter
* Get adapter instance using ActiveModel::Serializer::Adapter.adapter_for(serializer)
This commit is contained in:
parent
56725b45a6
commit
6cc4fa0258
@ -57,7 +57,13 @@ by AMS. If you want to use a different adapter, such as a HalAdapter, you can
|
||||
change this in an initializer:
|
||||
|
||||
```ruby
|
||||
ActiveModel::Serializer.default_adapter = ActiveModel::Serializer::Adapter::HalAdapter
|
||||
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::HalAdapter
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```ruby
|
||||
ActiveModel::Serializer.config.adapter = :hal
|
||||
```
|
||||
|
||||
You won't need to implement an adapter unless you wish to use a new format or
|
||||
|
||||
@ -12,7 +12,7 @@ module ActionController
|
||||
if serializer
|
||||
# omg hax
|
||||
object = serializer.new(resource)
|
||||
adapter = ActiveModel::Serializer::Adapter::SimpleAdapter.new(object)
|
||||
adapter = ActiveModel::Serializer::Adapter.adapter_for(object)
|
||||
|
||||
super(adapter, options)
|
||||
else
|
||||
|
||||
@ -16,6 +16,24 @@ module ActiveModel
|
||||
def to_json(options = {})
|
||||
raise NotImplementedError, 'This is abstract method. Should be implemented at concrete adapter.'
|
||||
end
|
||||
|
||||
def self.adapter_for(serializer)
|
||||
adapter_class = case serializer.config.adapter
|
||||
when Symbol
|
||||
class_name = "ActiveModel::Serializer::Adapter::#{serializer.config.adapter.to_s.classify}Adapter"
|
||||
if Object.const_defined?(class_name)
|
||||
Object.const_get(class_name)
|
||||
end
|
||||
when Class
|
||||
serializer.config.adapter
|
||||
end
|
||||
unless adapter_class
|
||||
valid_adapters = self.constants.map { |klass| ":#{klass.to_s.sub('Adapter', '').downcase}" }
|
||||
raise ArgumentError, "Unknown adapter: #{serializer.config.adapter}. Valid adapters are: #{valid_adapters}"
|
||||
end
|
||||
|
||||
adapter_class.new(serializer)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -6,6 +6,7 @@ module ActiveModel
|
||||
|
||||
included do |base|
|
||||
base.config.array_serializer = ActiveModel::Serializer::ArraySerializer
|
||||
base.config.adapter = :simple
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -21,5 +21,54 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class AdapterForTest < Minitest::Test
|
||||
def setup
|
||||
profile = Profile.new
|
||||
@serializer = ProfileSerializer.new(profile)
|
||||
@previous_adapter = ActiveModel::Serializer.config.adapter
|
||||
end
|
||||
|
||||
def teardown
|
||||
ActiveModel::Serializer.config.adapter = @previous_adapter
|
||||
end
|
||||
|
||||
def test_returns_default_adapter
|
||||
adapter = Adapter.adapter_for(@serializer)
|
||||
assert_kind_of ActiveModel::Serializer::Adapter::SimpleAdapter, adapter
|
||||
end
|
||||
|
||||
def test_overwrite_adapter_with_symbol
|
||||
ActiveModel::Serializer.config.adapter = :null
|
||||
|
||||
adapter = Adapter.adapter_for(@serializer)
|
||||
assert_kind_of ActiveModel::Serializer::Adapter::NullAdapter, adapter
|
||||
ensure
|
||||
|
||||
end
|
||||
|
||||
def test_overwrite_adapter_with_class
|
||||
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::NullAdapter
|
||||
|
||||
adapter = Adapter.adapter_for(@serializer)
|
||||
assert_kind_of ActiveModel::Serializer::Adapter::NullAdapter, adapter
|
||||
end
|
||||
|
||||
def test_raises_exception_if_invalid_symbol_given
|
||||
ActiveModel::Serializer.config.adapter = :unknown
|
||||
|
||||
assert_raises ArgumentError do
|
||||
Adapter.adapter_for(@serializer)
|
||||
end
|
||||
end
|
||||
|
||||
def test_raises_exception_if_it_does_not_know_hot_to_infer_adapter
|
||||
ActiveModel::Serializer.config.adapter = 42
|
||||
|
||||
assert_raises ArgumentError do
|
||||
Adapter.adapter_for(@serializer)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -6,6 +6,10 @@ module ActiveModel
|
||||
def test_array_serializer
|
||||
assert_equal ActiveModel::Serializer::ArraySerializer, ActiveModel::Serializer.config.array_serializer
|
||||
end
|
||||
|
||||
def test_adapter
|
||||
assert_equal :simple, ActiveModel::Serializer.config.adapter
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user