Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
181 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters