Skip to content

Commit

Permalink
Data attributes autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
mjy committed Aug 23, 2019
1 parent 59db10e commit b4f4eee
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 55 deletions.
25 changes: 8 additions & 17 deletions app/controllers/data_attributes_controller.rb
Expand Up @@ -74,29 +74,20 @@ def list

# GET /data_attributes/search
def search
if params[:id].blank?
redirect_to data_attribute_path, notice: 'You must select an item from the list with a click or tab press before clicking show.'
if @data_attribute = DataAttribute.find(params[:id])
redirect_to url_for(@data_attribute.attribute_subject.metamorphosize)
else
redirect_to data_attribute_path(params[:id])
redirect_to data_attribute_path, notice: 'You must select an item from the list with a click or tab press before clicking show.'
end
end

def autocomplete
@data_attributes = DataAttribute.find_for_autocomplete(
params.merge(project_id: sessions_current_project_id)
).limit(20)

data = @data_attributes.collect do |t|
str = render_to_string(partial: 'tag', locals: {data_attribute: t})
{id: t.id,
label: str,
response_values: {
params[:method] => t.id},
label_html: str
}
end
render json: {} and return if params[:term].blank?

render json: data
@data_attributes = Queries::DataAttribute::Autocomplete.new(
params.require(:term),
project_id: sessions_current_project_id
).autocomplete
end

def value_autocomplete
Expand Down
76 changes: 40 additions & 36 deletions app/helpers/data_attributes_helper.rb
Expand Up @@ -11,51 +11,55 @@ def data_attribute_annotation_tag(data_attribute)
content_tag(:span, s.html_safe, class: [:annotation__data_attribute])
end

def data_attribute_autocomplete_tag(data_attribute)
return nil if data_attribute.nil?
[
[ content_tag(:span, data_attribute.predicate_name, class: [:feedback, 'feedback-thin', 'feedback-primary']),
content_tag(:span, data_attribute.value, class: [:feedback, 'feedback-thin', 'feedback-primary']),
content_tag(:span, data_attribute.type, class: [:feedback, 'feedback-thin', 'feedback-secondary']),
content_tag(:span, data_attribute.attribute_subject_type, class: [:feedback, 'feedback-thin', 'feedback-light'])].join(' ').html_safe,
content_tag(:div, object_tag(data_attribute.annotated_object.metamorphosize))
].join.html_safe
end

# TODO deprecate
def data_attribute_predicate_tag(data_attribute)
return nil if data_attribute.nil?
data_attribute.predicate_name
#if data_attribute.type == 'InternalAttribute'
# controlled_vocabulary_term_tag(data_attribute.metamorphosize.predicate)
#elsif data_attribute.type == 'ImportAttribute'
# data_attribute.import_predicate
#else
# nil
#end
end
end

def data_attribute_list_tag(object)
return nil unless object.has_data_attributes? && object.data_attributes.any?
content_tag(:h3, 'Data attributes') +
content_tag(:ul, class: 'annotations__data_attribute_list') do
object.data_attributes.collect{|a|
content_tag(:li, data_attribute_annotation_tag(a))
}.join.html_safe
end
def data_attribute_list_tag(object)
return nil unless object.has_data_attributes? && object.data_attributes.any?
content_tag(:h3, 'Data attributes') +
content_tag(:ul, class: 'annotations__data_attribute_list') do
object.data_attributes.collect{|a|
content_tag(:li, data_attribute_annotation_tag(a))
}.join.html_safe
end
end

def add_data_attribute_link(object: nil, attribute: nil)
link_to('Add data attribute', new_data_attribute_path(
data_attribute: {
attribute_subject_type: object.class.base_class.name,
attribute_subject_id: object.id})) if object.has_data_attributes?
end

def data_attribute_link(data_attribute)
return nil if data_attribute.nil?
link_to(object_tag(data_attribute).html_safe, data_attribute.annotated_object.metamorphosize)
end
def add_data_attribute_link(object: nil, attribute: nil)
link_to('Add data attribute', new_data_attribute_path(
data_attribute: {
attribute_subject_type: object.class.base_class.name,
attribute_subject_id: object.id})) if object.has_data_attributes?
end

def data_attributes_search_form
render('/data_attributes/quick_search_form')
end
def data_attribute_link(data_attribute)
return nil if data_attribute.nil?
link_to(object_tag(data_attribute).html_safe, data_attribute.annotated_object.metamorphosize)
end

def data_attribute_edit_link(data_attribute)
if data_attribute.metamorphosize.editable?
link_to 'Edit', edit_data_attribute_path(data_attribute)
else
content_tag(:em, 'Edit')
end
def data_attributes_search_form
render('/data_attributes/quick_search_form')
end

def data_attribute_edit_link(data_attribute)
if data_attribute.metamorphosize.editable?
link_to 'Edit', edit_data_attribute_path(data_attribute)
else
content_tag(:em, 'Edit')
end
end

end
19 changes: 19 additions & 0 deletions app/views/data_attributes/autocomplete.json.jbuilder
@@ -0,0 +1,19 @@
json.array! @data_attributes do |i|
v = data_attribute_autocomplete_tag(i)

json.id i.id

json.label data_attribute_tag(i)

json.label_html v
json.attribute_subject_global_id i.attribute_subject.to_global_id.to_s

json.attribute_subject_object_id i.attribute_subject_id
json.attribute_subject_object_type i.attribute_subject_type

json.response_values do
if params[:method]
json.set! params[:method], i.id
end
end
end
114 changes: 114 additions & 0 deletions lib/queries/data_attribute/autocomplete.rb
@@ -0,0 +1,114 @@
module Queries
module DataAttribute
class Autocomplete < Queries::Query

attr_accessor :term_key, :term_value

# @param [Hash] args
def initialize(string, project_id: nil)
super
set_key_value
end

# @return [Scope]
def base_query
::DataAttribute.select('data_attributes.*')
end

def set_key_value
@term_key, @term_value = query_string.split(' ')
end

def autocomplete_internal_exact_key_value
return nil if term_value.nil? || term_key.nil?
::InternalAttribute.joins(:predicate).where(
predicate_table[:name].eq(term_key).and(
table[:value].eq(term_value))
).limit(20)
end

def autocomplete_import_exact_key_value
return nil if term_value.nil? || term_key.nil?
::ImportAttribute.where(
import_predicate: term_key,
value: term_value
).limit(20)
end

def autocomplete_internal_exact_key_wildcard_value
return nil if term_value.nil? || term_key.nil?
::InternalAttribute.joins(:predicate).where(
predicate_table[:name].eq(term_key).and(
table[:value].matches('%' + term_value + '%'))
).limit(20)
end

def autocomplete_import_exact_key_wildcard_value
return nil if term_value.nil? || term_key.nil?
::ImportAttribute.where(
import_predicate: term_key,
value: '%' + term_value + '%'
).limit(20)
end

def autocomplete_internal_wildcard_key_value
return nil if term_value.nil? || term_key.nil?
::InternalAttribute.joins(:predicate).where(
predicate_table[:name].matches('%' + term_key + '%').and(
table[:value].matches('%' + term_value + '%'))
).limit(20)
end

def autocomplete_import_wildcard_key_value
return nil if term_value.nil? || term_key.nil?
::ImportAttribute.where(
import_predicate: '%' + term_key + '%',
value: '%' + term_value + '%'
).limit(20)
end

# @return [Array]
def autocomplete
queries = [
autocomplete_internal_exact_key_value,
autocomplete_import_exact_key_value,
autocomplete_internal_exact_key_wildcard_value,
autocomplete_import_exact_key_wildcard_value,
autocomplete_internal_wildcard_key_value,
autocomplete_import_wildcard_key_value
]

queries.compact!

updated_queries = []
queries.each_with_index do |q ,i|
a = q.where(with_project_id.to_sql) if project_id
a ||= q
updated_queries[i] = a
end

result = []
updated_queries.each do |q|
result += q.to_a
result.uniq!
break if result.count > 40
end
result[0..40]
end

# @return [Arel::Table]
def table
::DataAttribute.arel_table
end

# @return [Arel::Table]
def predicate_table
::Predicate.arel_table
end




end
end
end
2 changes: 0 additions & 2 deletions lib/queries/taxon_name/filter.rb
Expand Up @@ -226,9 +226,7 @@ def taxon_name_classification_facet
# @return Scope
def type_metadata_facet
return nil if type_metadata.nil?

subquery = ::TypeMaterial.where(::TypeMaterial.arel_table[:protonym_id].eq(::TaxonName.arel_table[:id])).arel.exists

::TaxonName.where(type_metadata ? subquery : subquery.not)
end

Expand Down

0 comments on commit b4f4eee

Please sign in to comment.