Class: Queries::Source::Filter

Inherits:
Query
  • Object
show all
Defined in:
lib/queries/source/filter.rb

Instance Attribute Summary (collapse)

Attributes inherited from Query

#dynamic_limit, #options, #project_id, #query_string, #terms

Instance Method Summary (collapse)

Methods inherited from Query

#alphabetic_strings, #autocomplete, #autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_identifier_cached_exact, #autocomplete_identifier_cached_like, #autocomplete_ordered_wildcard_pieces_in_cached, #autocomplete_start_date, #build_terms, #cached, #combine_or_clauses, #end_wildcard, #exactly_named, #fragments, #identifier_table, #integers, #match_ordered_wildcard_pieces_in_cached, #match_wildcard_cached, #named, #no_digits, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #result, #scope, #simple_date, #start_and_end_wildcard, #start_wildcard, #wildcard_pieces, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_id, #with_identifier, #with_identifier_like, #with_project_id, #with_start_date, #year_letter, #years

Constructor Details

- (Filter) initialize(params)

Returns a new instance of Filter

Parameters:

  • params (Hash)


8
9
10
11
12
13
# File 'lib/queries/source/filter.rb', line 8

def initialize(params)
  @query_string = params[:query_term]
  @project_id = params[:project_id]
  @author_ids = params[:author_ids] || []
  build_terms
end

Instance Attribute Details

- (Object) author_ids

Returns the value of attribute author_ids



5
6
7
# File 'lib/queries/source/filter.rb', line 5

def author_ids
  @author_ids
end

Instance Method Details

- (ActiveRecord::Relation) all

Returns:

  • (ActiveRecord::Relation)


65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/queries/source/filter.rb', line 65

def all
  a = or_clauses
  b = merge_clauses
  if a && b
    b.where(a).distinct
  elsif a
    ::Source.where(a).distinct
  elsif b
    b.distinct
  else
    ::Source.all
  end
end

- (ActiveRecord::Relation) by_project_all

Returns:

  • (ActiveRecord::Relation)


106
107
108
# File 'lib/queries/source/filter.rb', line 106

def by_project_all
  ::Source.where(where_sql).limit(500).distinct.order(:cached).joins(:project_sources).where(member_of_project_id.to_sql)
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



54
55
56
57
58
59
60
61
62
# File 'lib/queries/source/filter.rb', line 54

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) matching_author_id



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/queries/source/filter.rb', line 79

def matching_author_id
  return nil if author_ids.empty?
  o = table
  r = ::Role.arel_table

  a = o.alias("a_") 
  b = o.project(a[Arel.star]).from(a)

  c = r.alias('r1')

  b = b.join(c, Arel::Nodes::OuterJoin)
    .on(
      a[:id].eq(c[:role_object_id])
    .and(c[:role_object_type].eq('Source'))
  )

  e = c[:id].not_eq(nil)
  f = c[:person_id].eq_any(author_ids)

  b = b.where(e.and(f))
  b = b.group(a['id'])
  b = b.as('z1_')

  ::Source.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['id'].eq(o['id']))))
end

- (Arel::Nodes::Equatity) member_of_project_id

Returns:

  • (Arel::Nodes::Equatity)


125
126
127
# File 'lib/queries/source/filter.rb', line 125

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

- (Object) merge_clauses



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/queries/source/filter.rb', line 30

def merge_clauses
  clauses = [
    matching_author_id,

    # matching_verbatim_author
  ].compact

  return nil if clauses.empty?

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

- (ActiveRecord::Relation) or_clauses

Returns:

  • (ActiveRecord::Relation)


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/queries/source/filter.rb', line 16

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

- (Arel::Table) project_sources_table

Returns:

  • (Arel::Table)


120
121
122
# File 'lib/queries/source/filter.rb', line 120

def project_sources_table
  ::ProjectSource.arel_table
end

- (Object) role_table



115
116
117
# File 'lib/queries/source/filter.rb', line 115

def role_table
  ::Role.arel_table
end

- (Arel::Table) table

Returns:

  • (Arel::Table)


111
112
113
# File 'lib/queries/source/filter.rb', line 111

def table
  ::Source.arel_table
end

- (String) where_sql

Returns:

  • (String)


47
48
49
50
# File 'lib/queries/source/filter.rb', line 47

def where_sql
  return ::Source.none if or_clauses.nil?
  or_clauses.to_sql
end