Class: Queries::SourceFilterQuery

Inherits:
Query
  • Object
show all
Includes:
Arel::Nodes
Defined in:
lib/queries/source_filter_query.rb

Instance Attribute Summary

Attributes inherited from Query

#dynamic_limit, #project_id, #query_string, #terms

Instance Method Summary (collapse)

Methods inherited from Query

#build_terms, #end_wildcard, #fragments, #identifier_table, #initialize, #integers, #named, #no_digits, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #result, #scope, #start_and_end_wildcard, #start_wildcard, #strings, #wildcard_wrapped_integers, #with_id, #with_identifier_like, #with_project_id, #years

Constructor Details

This class inherits a constructor from Queries::Query

Instance Method Details

- (ActiveRecord::Relation) all

Returns:

  • (ActiveRecord::Relation)


51
52
53
# File 'lib/queries/source_filter_query.rb', line 51

def all 
  Source.where(where_sql).limit(500).uniq.order(:cached)
end

- (ActiveRecord::Relation) by_project_all

Returns:

  • (ActiveRecord::Relation)


56
57
58
# File 'lib/queries/source_filter_query.rb', line 56

def by_project_all
  Source.where(where_sql).limit(500).uniq.order(:cached).joins(:project_sources).where(member_of_project_id.to_sql)
end

- (ActiveRecord::Relation?) cached

cached matches full query string wildcarded

Returns:

  • (ActiveRecord::Relation, nil)


42
43
44
45
46
47
48
# File 'lib/queries/source_filter_query.rb', line 42

def cached
  if !terms.empty?
    table[:cached].matches_any(terms)
  else
    nil
  end
end

- (ActiveRecord::Relation?) fragment_year_matches

Returns if user provides 5 or fewer strings and any number of years look for any string && year

Returns:

  • (ActiveRecord::Relation, nil)

    if user provides 5 or fewer strings and any number of years look for any string && year



30
31
32
33
34
35
36
37
38
# File 'lib/queries/source_filter_query.rb', line 30

def fragment_year_matches
  if fragments.any?
    s = table[:cached].matches_any(fragments)
    s = s.and(table[:year].eq_any(years)) if !years.empty? 
    s
  else
    nil
  end 
end

- (Object) member_of_project_id



68
69
70
# File 'lib/queries/source_filter_query.rb', line 68

def member_of_project_id
  project_sources_table[:project_id].eq(project_id)
end

- (ActiveRecord::Relation) or_clauses

Returns:

  • (ActiveRecord::Relation)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/queries/source_filter_query.rb', line 8

def or_clauses
  clauses = [
    only_ids,               # only intgers provided
    cached,                 # should hit titles when provided alone, unfragmented string matches
    fragment_year_matches   # keyword style ANDs years
  ].compact

  
  a = clauses.shift
  clauses.each do |b|
    a = a.or(b)
  end
  a
end

- (Object) project_sources_table



64
65
66
# File 'lib/queries/source_filter_query.rb', line 64

def project_sources_table
  ProjectSource.arel_table
end

- (Object) table



60
61
62
# File 'lib/queries/source_filter_query.rb', line 60

def table
  Source.arel_table
end

- (String) where_sql

Returns:

  • (String)


24
25
26
# File 'lib/queries/source_filter_query.rb', line 24

def where_sql
  or_clauses.to_sql
end