Skip to content
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

Fix: Goo attributes loader helper after nerver serialize patch #168

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions lib/ontologies_linked_data/models/base.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -44,57 +44,59 @@ def self.goo_attrs_to_load(attributes = [], level = 0)
# Get attributes, either provided, all, or default # Get attributes, either provided, all, or default
default_attrs = if !attributes.empty? default_attrs = if !attributes.empty?
if attributes.first == :all if attributes.first == :all
(self.attributes + self.hypermedia_settings[:serialize_default]).uniq (self.attributes + hypermedia_settings[:serialize_default]).uniq
else else
attributes - self.hypermedia_settings[:serialize_never] attributes - hypermedia_settings[:serialize_never]
end end
elsif self.hypermedia_settings[:serialize_default].empty? elsif hypermedia_settings[:serialize_default].empty?
self.attributes self.attributes
else else
self.hypermedia_settings[:serialize_default].dup hypermedia_settings[:serialize_default].dup
end end


embed_attrs = {} embed_attrs = {}
extra_attrs = [] extra_attrs = []
if level == 0 if level.zero?
# Also include attributes that are embedded # Also include attributes that are embedded
self.hypermedia_settings[:embed].each do |e| hypermedia_settings[:embed].each do |e|

next unless default_attrs.include?(e) next unless default_attrs.include?(e)

default_attrs.delete(e) default_attrs.delete(e)
embed_class = self.range(e) attributes.delete(e)
embed_class = range(e)
next if embed_class.nil? || !embed_class.ancestors.include?(LinkedData::Models::Base) next if embed_class.nil? || !embed_class.ancestors.include?(LinkedData::Models::Base)
#hack to avoid nested unmapped queries in class
if (self.model_name == :class) # HACK: to avoid nested unmapped queries in class
if attributes && attributes.include?(:properties) if model_name == :class && attributes.include?(:properties)
attributes = attributes.dup attributes = attributes.dup
attributes.delete :properties attributes.delete :properties
end end
end
embed_attrs[e] = embed_class.goo_attrs_to_load(attributes, level += 1) embed_attrs[e] = embed_class.goo_attrs_to_load(attributes, level += 1)
end end
end end


# Merge embedded with embedded values # Merge embedded with embedded values
embed_values = self.hypermedia_settings[:embed_values].first embed_values = hypermedia_settings[:embed_values].first
embed_attrs.merge!(embed_values.dup) if embed_values embed_attrs.merge!(embed_values.dup) if embed_values


# Include attributes needed for caching (if enabled) # Include attributes needed for caching (if enabled)
if LinkedData.settings.enable_http_cache if LinkedData.settings.enable_http_cache
cache_attributes = self.cache_settings[:cache_load] cache_attributes = cache_settings[:cache_load]
extra_attrs.concat(cache_attributes.to_a) unless cache_attributes.nil? && cache_attributes.empty? extra_attrs.concat(cache_attributes.to_a) unless cache_attributes.nil? && cache_attributes.empty?
end end


# Include attributes needed for security (if enabled) # Include attributes needed for security (if enabled)
if LinkedData.settings.enable_security if LinkedData.settings.enable_security
access_control_attributes = self.access_control_load_attrs access_control_attributes = access_control_load_attrs
extra_attrs.concat(access_control_attributes.to_a) unless access_control_attributes.nil? && access_control_attributes.empty? unless access_control_attributes.nil? && access_control_attributes.empty?
extra_attrs.concat(access_control_attributes.to_a)
end
end end


# These attributes need to be loaded to support link generation # These attributes need to be loaded to support link generation
links_load = self.hypermedia_settings[:links_load] links_load = hypermedia_settings[:links_load]
unless links_load.nil? || links_load.empty? extra_attrs.concat(links_load) unless links_load.nil? || links_load.empty?
extra_attrs.concat(links_load)
end


# Add extra attrs to appropriate group (embed Hash vs default Array) # Add extra attrs to appropriate group (embed Hash vs default Array)
extra_attrs.each do |attr| extra_attrs.each do |attr|
Expand All @@ -112,16 +114,14 @@ def self.goo_attrs_to_load(attributes = [], level = 0)
end end


# Remove default attrs that are in the embedded # Remove default attrs that are in the embedded
default_attrs = default_attrs - embed_attrs.keys default_attrs -= embed_attrs.keys


# Merge all embedded with the default (provided, all, default) # Merge all embedded with the default (provided, all, default)
default_attrs << embed_attrs if embed_attrs.length > 0 default_attrs << embed_attrs if embed_attrs.length.positive?
default_attrs.uniq! default_attrs.uniq!


# Filter out attributes that should not get loaded # Filter out attributes that should not get loaded
default_attrs = default_attrs - self.hypermedia_settings[:do_not_load] default_attrs - hypermedia_settings[:do_not_load]

return default_attrs
end end


## ##
Expand Down
5 changes: 5 additions & 0 deletions test/models/user/test_user.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ def test_user_default_uuid
u.delete u.delete
end end


def test_user_subscription_load
to_load = LinkedData::Models::User.goo_attrs_to_load([:subscription])
assert_equal [{ subscription: [:ontology, { notification_type: [:type] }], role: [:role] }], to_load
end

end end
Loading