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_exact_id, #autocomplete_ordered_wildcard_pieces_in_cached, #autocomplete_start_date, #build_terms, #cached, #combine_or_clauses, #end_wildcard, #exactly_named, #fragments, #integers, #match_ordered_wildcard_pieces_in_cached, #match_wildcard_end_in_cached, #match_wildcard_in_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_cached, #with_cached_like, #with_id, #with_project_id, #with_start_date, #year_letter, #years

Constructor Details

- (Filter) initialize(params)

Returns a new instance of Filter

Parameters:

  • params (Hash)


10
11
12
13
14
15
16
# File 'lib/queries/source/filter.rb', line 10

def initialize(params)
  @query_string = params[:query_term]
  @project_id = params[:project_id]
  @author_ids = params[:author_ids] || []
  @recent = params[:recent].blank? ? nil : true 
  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

- (Object) recent

Returns the value of attribute recent



7
8
9
# File 'lib/queries/source/filter.rb', line 7

def recent
  @recent
end

Instance Method Details

- (ActiveRecord::Relation) all

Returns:

  • (ActiveRecord::Relation)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/queries/source/filter.rb', line 117

def all
  a = or_clauses
  b = merge_clauses
  q = nil
  if a && b
    q = b.where(a).distinct
  elsif a
    q = ::Source.where(a).distinct
  elsif b
    q = b.distinct
  else
    q = ::Source.all
  end
  q = q.order(updated_at: :desc) if recent
  q 
end

- (ActiveRecord::Relation) by_project_all

Returns:

  • (ActiveRecord::Relation)


93
94
95
# File 'lib/queries/source/filter.rb', line 93

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



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

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/queries/source/filter.rb', line 66

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)


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

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

- (Object) merge_clauses



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

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)


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/queries/source/filter.rb', line 19

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)


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

def project_sources_table
  ::ProjectSource.arel_table
end

- (Object) role_table



102
103
104
# File 'lib/queries/source/filter.rb', line 102

def role_table
  ::Role.arel_table
end

- (Arel::Table) table

Returns:

  • (Arel::Table)


98
99
100
# File 'lib/queries/source/filter.rb', line 98

def table
  ::Source.arel_table
end

- (String) where_sql

Returns:

  • (String)


49
50
51
52
# File 'lib/queries/source/filter.rb', line 49

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