Skip to content

Commit

Permalink
make identifiers concern a proper concern
Browse files Browse the repository at this point in the history
  • Loading branch information
mjy committed Jan 15, 2019
1 parent d14c810 commit a3112ca
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
44 changes: 44 additions & 0 deletions lib/queries/concerns/identifiers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Helpers for queries that reference Identifier
module Queries::Concerns::Identifiers

extend ActiveSupport::Concern

# @return [Arel::Table]
def identifier_table
::Identifier.arel_table
end

# @return [Arel::Nodes::Grouping]
def with_identifier_like
a = [ start_and_end_wildcard ]
a = a + wildcard_wrapped_integers
identifier_table[:cached].matches_any(a)
end

# @return [Arel::Nodes::Equality]
def with_identifier
identifier_table[:cached].eq(query_string)
end

# @return [Arel::Nodes::Equality]
def with_identifier_wildcard_end
identifier_table[:cached].matches(end_wildcard)
end

# @return [Arel::Nodes::Equality]
def with_identifier_wildcard_anywhere
identifier_table[:cached].matches(start_and_end_wildcard)
end

#
# Autocomplete
#
def autocomplete_identifier_cached_exact
base_query.joins(:identifiers).where(with_identifier.to_sql)
end

def autocomplete_identifier_cached_like
base_query.joins(:identifiers).where(with_identifier_like.to_sql)
end

end
2 changes: 1 addition & 1 deletion lib/queries/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
module Queries
class Query
include Arel::Nodes
include Queries::Query::Identifiers
include Queries::Concerns::Identifiers

attr_accessor :query_string
attr_accessor :terms
Expand Down

0 comments on commit a3112ca

Please sign in to comment.