Skip to content

Commit

Permalink
Merge pull request #42 from ontoportal-lirmm/feature/support-multi-la…
Browse files Browse the repository at this point in the history
…ngual-search

Feature: Make map_attribute support showing all languages - Support multi language search
  • Loading branch information
syphax-bouazzouni committed Jul 20, 2023
2 parents 81ce88c + a81f12c commit 1d78bde
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
13 changes: 9 additions & 4 deletions lib/goo/base/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,13 @@ def self.range_object(attr,id)



def self.map_attributes(inst,equivalent_predicates=nil)
def self.map_attributes(inst,equivalent_predicates=nil, include_languages: false)
if (inst.kind_of?(Goo::Base::Resource) && inst.unmapped.nil?) ||
(!inst.respond_to?(:unmapped) && inst[:unmapped].nil?)
raise ArgumentError, "Resource.map_attributes only works for :unmapped instances"
end
klass = inst.respond_to?(:klass) ? inst[:klass] : inst.class
unmapped = inst.respond_to?(:klass) ? inst[:unmapped] : inst.unmapped
unmapped = inst.respond_to?(:klass) ? inst[:unmapped] : inst.unmapped(include_languages: include_languages)
list_attrs = klass.attributes(:list)
unmapped_string_keys = Hash.new
unmapped.each do |k,v|
Expand Down Expand Up @@ -398,13 +398,18 @@ def self.map_attributes(inst,equivalent_predicates=nil)
object = unmapped_string_keys[attr_uri]
end

object = object.map {|o| o.is_a?(RDF::URI) ? o : o.object}
if object.is_a?(Hash)
object = object.transform_values{|values| Array(values).map{|o|o.is_a?(RDF::URI) ? o : o.object}}
else
object = object.map {|o| o.is_a?(RDF::URI) ? o : o.object}
end

if klass.range(attr)
object = object.map { |o|
o.is_a?(RDF::URI) ? klass.range_object(attr,o) : o }
end
object = object.first unless list_attrs.include?(attr)

object = object.first unless list_attrs.include?(attr) || include_languages
if inst.respond_to?(:klass)
inst[attr] = object
else
Expand Down
2 changes: 1 addition & 1 deletion lib/goo/base/settings/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def read_only(attributes)
end

def show_all_languages?(args)
args.first.is_a?(Hash) && args.first.keys.include?(:show_languages) && args.first[:show_languages]
args.first.is_a?(Hash) && args.first.keys.include?(:include_languages) && args.first[:include_languages]
end

def not_show_all_languages?(values, args)
Expand Down
9 changes: 3 additions & 6 deletions lib/goo/sparql/mixins/solution_lang_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,16 @@ def model_group_by_lang(model)

def group_by_lang(values)

return values.to_a if is_a_uri?(values.first)
return values.to_a if values.all?{|x| x.is_a?(RDF::URI) || !x.respond_to?(:language) }

values = values.group_by { |x| x.language ? x.language.to_s.downcase : :none }
values = values.group_by { |x| x.respond_to?(:language) && x.language ? x.language.to_s.downcase : :none }

no_lang = values[:none] || []
return no_lang if !no_lang.empty? && no_lang.all? { |x| !x.plain? }
return no_lang if !no_lang.empty? && no_lang.all? { |x| x.respond_to?(:plain?) && !x.plain? }

values
end

def is_a_uri?(value)
value.is_a?(RDF::URI) && value.valid?
end

def object_language(new_value)
new_value.language || :no_lang if new_value.is_a?(RDF::Literal)
Expand Down

0 comments on commit 1d78bde

Please sign in to comment.