-
Notifications
You must be signed in to change notification settings - Fork 900
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14498 from imtayadeway/api/options-serializer
Extract Api::OptionsSerializer
- Loading branch information
Showing
6 changed files
with
70 additions
and
33 deletions.
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,47 @@ | ||
module Api | ||
class OptionsSerializer | ||
attr_reader :klass, :data, :config | ||
|
||
def initialize(klass, data = {}) | ||
@klass = klass | ||
@data = data | ||
@config = CollectionConfig.new | ||
end | ||
|
||
def serialize | ||
{ | ||
:attributes => attributes, | ||
:virtual_attributes => virtual_attributes, | ||
:relationships => relationships, | ||
:subcollections => subcollections, | ||
:data => data | ||
} | ||
end | ||
|
||
private | ||
|
||
def attributes | ||
return [] unless klass | ||
options_attribute_list(klass.attribute_names - klass.virtual_attribute_names) | ||
end | ||
|
||
def virtual_attributes | ||
return [] unless klass | ||
options_attribute_list(klass.virtual_attribute_names) | ||
end | ||
|
||
def relationships | ||
return [] unless klass | ||
(klass.reflections.keys | klass.virtual_reflections.keys.collect(&:to_s)).sort | ||
end | ||
|
||
def subcollections | ||
return [] unless klass | ||
Array(config[config.name_for_klass(klass)].subcollections).sort | ||
end | ||
|
||
def options_attribute_list(attrlist) | ||
attrlist.sort.select { |attr| !Api.encrypted_attribute?(attr) } | ||
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,15 @@ | ||
RSpec.describe Api::OptionsSerializer do | ||
it "returns some default values when the class is nil" do | ||
actual = described_class.new(nil).serialize | ||
|
||
expected = { | ||
:attributes => [], | ||
:virtual_attributes => [], | ||
:relationships => [], | ||
:subcollections => [], | ||
:data => {} | ||
} | ||
|
||
expect(actual).to eq(expected) | ||
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