-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/adapter #612
Merged
Merged
Feature/adapter #612
Changes from 9 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
f00fe55
* Rename NullAdapter to SimpleAdapter
56725b4
Add NullAdapater
6cc4fa0
* Configure adapter using ActiveModel::Serializer.config.adapter
553c470
Serializer should be available wiithing adapter to inspect attributes…
7b7d4d8
Test for SimpleAdapter#serializable_hash
2c7906e
Test for NullAdapter#serializable_hash
a6f9dae
Concrete adapter should provide serializable hash for Adapter#to_json…
b1f7a5c
Move Adapter.adapter_for to Serializer.adapter
fa4ee9d
Add Serializer#associations
466c7d5
Wrap association into Serializers
597765e
start implementing json_api adapter to understand how associations sh…
1b718b6
fix spelling
c1fdfc1
First try to implement ArraySerializer
85b4b85
Remove 1.9.3 from CI build as we dropped support
85ff812
Include Enumerable to ArraySerializer
3dd4928
* Do not ingerit array serializer from Serializer
6496b08
rename simple adapter to json
6bb4501
JsonApi adapter: serialize association
e45e5a8
Remove 'Adapter' suffix from adapters since they are in Adapter:: nam…
b4a313e
Merge remote-tracking branch 'upstream/master' into feature/adapter
1396093
Object.const_get differs on 2.0 and 2.1.2. So rescue from NameError
77847d7
User String#constantize instead of Object.const_get
7293072
Revert "Remove 1.9.3 from CI build as we dropped support"
ff37b62
test for json adapter
45a47a1
safe_constantize instead of rescue
258b595
clean up code
71a43a4
Pass options to associations
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module ActiveModel | ||
class Serializer | ||
class Adapter | ||
extend ActiveSupport::Autoload | ||
autoload :SimpleAdapter | ||
autoload :NullAdapter | ||
|
||
attr_reader :serializer | ||
|
||
def initialize(serializer) | ||
@serializer = serializer | ||
end | ||
|
||
def serializable_hash(options = {}) | ||
raise NotImplementedError, 'This is abstract method. Should be implemented at concrete adapter.' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "This is an abstract method." and "Should be implemented on the concrete adapter." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you |
||
end | ||
|
||
def to_json(options={}) | ||
serializable_hash(options).to_json | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module ActiveModel | ||
class Serializer | ||
class Adapter | ||
class SimpleAdapter < Adapter | ||
def serializable_hash(options = {}) | ||
serializer.attributes.each_with_object({}) do |(attr, value), h| | ||
h[attr] = value | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModel | ||
class Serializer | ||
class Adapter | ||
class NullAdapterTest < Minitest::Test | ||
def setup | ||
profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) | ||
serializer = ProfileSerializer.new(profile) | ||
|
||
@adapter = NullAdapter.new(serializer) | ||
end | ||
|
||
def test_serializable_hash | ||
assert_equal({}, @adapter.serializable_hash) | ||
end | ||
|
||
def test_it_returns_empty_json | ||
assert_equal('{}', @adapter.to_json) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModel | ||
class Serializer | ||
class AdapterTest < Minitest::Test | ||
def setup | ||
profile = Profile.new | ||
@serializer = ProfileSerializer.new(profile) | ||
@adapter = ActiveModel::Serializer::Adapter.new(@serializer) | ||
end | ||
|
||
def test_serializable_hash_is_abstract_method | ||
assert_raises(NotImplementedError) do | ||
@adapter.serializable_hash(only: [:name]) | ||
end | ||
end | ||
|
||
def test_serializer | ||
assert_equal @serializer, @adapter.serializer | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module ActiveModel | ||
class Serializer | ||
class AdapterForTest < Minitest::Test | ||
def setup | ||
@previous_adapter = ActiveModel::Serializer.config.adapter | ||
end | ||
|
||
def teardown | ||
ActiveModel::Serializer.config.adapter = @previous_adapter | ||
end | ||
|
||
def test_returns_default_adapter | ||
adapter = ActiveModel::Serializer.adapter | ||
assert_equal ActiveModel::Serializer::Adapter::SimpleAdapter, adapter | ||
end | ||
|
||
def test_overwrite_adapter_with_symbol | ||
ActiveModel::Serializer.config.adapter = :null | ||
|
||
adapter = ActiveModel::Serializer.adapter | ||
assert_equal ActiveModel::Serializer::Adapter::NullAdapter, adapter | ||
ensure | ||
|
||
end | ||
|
||
def test_overwrite_adapter_with_class | ||
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::NullAdapter | ||
|
||
adapter = ActiveModel::Serializer.adapter | ||
assert_equal ActiveModel::Serializer::Adapter::NullAdapter, adapter | ||
end | ||
|
||
def test_raises_exception_if_invalid_symbol_given | ||
ActiveModel::Serializer.config.adapter = :unknown | ||
|
||
assert_raises ArgumentError do | ||
ActiveModel::Serializer.adapter | ||
end | ||
end | ||
|
||
def test_raises_exception_if_it_does_not_know_hot_to_infer_adapter | ||
ActiveModel::Serializer.config.adapter = 42 | ||
|
||
assert_raises ArgumentError do | ||
ActiveModel::Serializer.adapter | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should return serializers