Skip to content

Commit

Permalink
Merge pull request #14498 from imtayadeway/api/options-serializer
Browse files Browse the repository at this point in the history
Extract Api::OptionsSerializer
  • Loading branch information
abellotti authored Mar 27, 2017
2 parents e6f18b8 + 33d3159 commit 636c309
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 33 deletions.
9 changes: 1 addition & 8 deletions app/controllers/api/base_controller/normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def normalize_attr_byname(attr, value)
normalize_time(value)
elsif Environment.normalized_attributes[:url].key?(attr.to_s)
normalize_url(value)
elsif encrypted_attribute?(attr)
elsif Api.encrypted_attribute?(attr)
normalize_encrypted
elsif Environment.normalized_attributes[:resource].key?(attr.to_s)
normalize_resource(value)
Expand Down Expand Up @@ -100,13 +100,6 @@ def normalize_resource(value)
value.to_s.starts_with?("/") ? "#{@req.base}#{value}" : value
end

#
# Let's determine if an attribute is encrypted
#
def encrypted_attribute?(attr)
Environment.normalized_attributes[:encrypted].key?(attr.to_s) || attr.to_s.include?('password')
end

#
# Let's filter out encrypted attributes, i.e. passwords
#
Expand Down
22 changes: 2 additions & 20 deletions app/controllers/api/base_controller/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -415,26 +415,8 @@ def action_validated?(resource, action_spec)
end

def render_options(resource, data = {})
collection = collection_class(resource)
options =
if collection.blank?
{ :attributes => [], :virtual_attributes => [], :relationships => [] }
else
{
:attributes => options_attribute_list(collection.attribute_names -
collection.virtual_attribute_names),
:virtual_attributes => options_attribute_list(collection.virtual_attribute_names),
:relationships => (collection.reflections.keys |
collection.virtual_reflections.keys.collect(&:to_s)).sort
}
end
options[:subcollections] = Array(collection_config[resource].subcollections).sort
options[:data] = data
render :json => options
end

def options_attribute_list(attrlist)
attrlist.sort.select { |attr| !encrypted_attribute?(attr) }
klass = collection_class(resource)
render :json => OptionsSerializer.new(klass, data).serialize
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ module Api
BadRequestError = Class.new(ApiError)
NotFoundError = Class.new(ApiError)
UnsupportedMediaTypeError = Class.new(ApiError)

def self.encrypted_attribute?(attr)
Environment.normalized_attributes[:encrypted].key?(attr.to_s) || attr.to_s.include?('password')
end
end
47 changes: 47 additions & 0 deletions lib/services/api/options_serializer.rb
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
15 changes: 15 additions & 0 deletions spec/lib/services/api/options_serializer_spec.rb
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
6 changes: 1 addition & 5 deletions spec/support/api_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,7 @@ def expect_options_results(type, data = {})
end

def select_attributes(attrlist)
attrlist.sort.select { |attr| !encrypted_attribute?(attr) }
end

def encrypted_attribute?(attr)
Api::Environment.normalized_attributes[:encrypted].key?(attr.to_s) || attr.to_s.include?('password')
attrlist.sort.select { |attr| !Api.encrypted_attribute?(attr) }
end
end
end
Expand Down

0 comments on commit 636c309

Please sign in to comment.