Skip to content

Commit

Permalink
replace normalize_virtual with normalize_attr_byname
Browse files Browse the repository at this point in the history
rename normalize_attr_byname to normalize_attr; make methods private

rename normalize_direct_array to normalize_array

simplify value retrieval in normalize_hash

move normalize_attr to private
  • Loading branch information
Jillian Tullo committed May 4, 2017
1 parent 4db8408 commit 141c567
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 39 deletions.
47 changes: 10 additions & 37 deletions app/controllers/api/base_controller/normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ module Normalizer
# Object or Hash Normalizer
#

# Note: revisit merging direct and virtual normalize hash methods here once support for
# virtual subcollections is added.

def normalize_hash(type, obj, opts = {})
Environment.fetch_encrypted_attribute_names(obj.class)
attrs = normalize_select_attributes(obj, opts)
Expand All @@ -20,37 +17,21 @@ def normalize_hash(type, obj, opts = {})
end

attrs.each do |k|
value = normalize_direct(type, k, obj.kind_of?(ActiveRecord::Base) ? obj.try(k) : obj[k])
value = normalize_attr(k, obj.kind_of?(ActiveRecord::Base) ? obj.try(k) : obj[k])
result[k] = value unless value.nil?
end
result
end

def normalize_virtual(vtype, name, obj, options = {})
return normalize_virtual_array(vtype, name, obj, options) if obj.kind_of?(Array) || obj.kind_of?(ActiveRecord::Relation)
return normalize_virtual_hash(vtype, obj, options) if obj.respond_to?(:attributes) || obj.respond_to?(:keys)
normalize_attr_byname(name, obj)
end

def normalize_virtual_array(vtype, name, obj, options)
obj.collect { |item| normalize_virtual(vtype, name, item, options) }
end

def normalize_virtual_hash(vtype, obj, options)
Environment.fetch_encrypted_attribute_names(obj.class)
attrs = (obj.respond_to?(:attributes) ? obj.attributes.keys : obj.keys)
attrs.each_with_object({}) do |k, res|
value = normalize_virtual(vtype, k, obj[k], options)
res[k] = value unless options[:ignore_nil] && value.nil?
end
end
private

#
# Let's normalize the attribute based on its name
#
def normalize_attr_byname(attr, value)
def normalize_attr(attr, value)
return if value.nil?
if Environment.normalized_attributes[:time].key?(attr.to_s)
if value.kind_of?(Array) || value.kind_of?(ActiveRecord::Relation)
normalize_array(attr, value)
elsif value.respond_to?(:attributes) || value.respond_to?(:keys)
normalize_hash(attr, value)
elsif Environment.normalized_attributes[:time].key?(attr.to_s)
normalize_time(value)
elsif Environment.normalized_attributes[:url].key?(attr.to_s)
normalize_url(value)
Expand Down Expand Up @@ -116,8 +97,6 @@ def normalize_encrypted
nil
end

private

def normalize_select_attributes(obj, opts)
if opts[:render_attributes].present?
opts[:render_attributes]
Expand All @@ -130,14 +109,8 @@ def normalize_select_attributes(obj, opts)
end
end

def normalize_direct(type, name, obj)
return normalize_direct_array(type, name, obj) if obj.kind_of?(Array) || obj.kind_of?(ActiveRecord::Relation)
return normalize_hash(type, obj) if obj.respond_to?(:attributes) || obj.respond_to?(:keys)
normalize_attr_byname(name, obj)
end

def normalize_direct_array(type, name, obj)
obj.collect { |item| normalize_direct(type, name, item) }
def normalize_array(name, obj)
obj.collect { |item| normalize_attr(name, item) }
end

def new_href(type, current_id, current_href, opts)
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/base_controller/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def fetch_direct_virtual_attribute(type, resource, attr)
return unless attr_accessible?(resource, attr)
virtattr_accessor = virtual_attribute_accessor(type, attr)
value = virtattr_accessor.nil? ? resource.public_send(attr) : send(virtattr_accessor, resource)
result = {attr => normalize_virtual(nil, attr, value, :ignore_nil => true)}
result = {attr => normalize_attr(attr, value)}
# set nil vtype above to "#{type}/#{resource.id}/#{attr}" to support id normalization
[value, result]
end
Expand All @@ -215,7 +215,7 @@ def fetch_indirect_virtual_attribute(_type, resource, base, attr, object_hash)
query_related_objects(base, resource, object_hash)
return unless attr_accessible?(object_hash[base], attr)
value = object_hash[base].public_send(attr)
result = {attr => normalize_virtual(nil, attr, value, :ignore_nil => true)}
result = {attr => normalize_attr(attr, value)}
# set nil vtype above to "#{type}/#{resource.id}/#{base.tr('.', '/')}/#{attr}" to support id normalization
base.split(".").reverse_each { |level| result = {level => result} }
[value, result]
Expand Down

0 comments on commit 141c567

Please sign in to comment.