Class: Queries::ContentFilterQuery

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ContentFilterQuery) initialize(topic_id: nil, otu_id: nil, hours_ago: nil, query_string: nil, most_recent_updates: nil)

Returns a new instance of ContentFilterQuery

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
# File 'lib/queries/content_filter_query.rb', line 12

def initialize(topic_id: nil, otu_id: nil, hours_ago: nil, query_string: nil, most_recent_updates: nil)
  raise ArgumentError.new('missing a filter') if topic_id.nil? && otu_id.nil? && hours_ago.nil? && most_recent_updates.nil? # TODO: support query_string

  @topic_id = topic_id
  @otu_id = otu_id
  @hours_ago = hours_ago.to_i if hours_ago
  @query_string = query_string
  @most_recent_updates = most_recent_updates.to_i
end

Instance Attribute Details

- (Object) hours_ago

Returns the value of attribute hours_ago



8
9
10
# File 'lib/queries/content_filter_query.rb', line 8

def hours_ago
  @hours_ago
end

- (Object) most_recent_updates

Returns the value of attribute most_recent_updates



10
11
12
# File 'lib/queries/content_filter_query.rb', line 10

def most_recent_updates
  @most_recent_updates
end

- (Object) otu_id

Returns the value of attribute otu_id



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

def otu_id
  @otu_id
end

- (Object) query_string

Returns the value of attribute query_string



9
10
11
# File 'lib/queries/content_filter_query.rb', line 9

def query_string
  @query_string
end

- (Object) topic_id

Returns the value of attribute topic_id



6
7
8
# File 'lib/queries/content_filter_query.rb', line 6

def topic_id
  @topic_id
end

Instance Method Details

- (Scope) all

Returns:

  • (Scope)


40
41
42
43
44
45
46
# File 'lib/queries/content_filter_query.rb', line 40

def all 
  q = Content.includes(:otu, :topic)
  s = where_sql
  q = q.where(s).references(:topics, :otus) if s
  q = q.order(updated_at: :desc).limit(most_recent_updates) unless most_recent_updates.zero?
  q
end

- (Object) for_otu



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

def for_otu
  table[:otu_id].eq(otu_id) if otu_id
end

- (Object) for_topic



52
53
54
# File 'lib/queries/content_filter_query.rb', line 52

def for_topic
  table[:topic_id].eq(topic_id) if topic_id
end

- (Object) recent



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

def recent 
  table[:updated_at].gt(hours_ago.hours.ago) if hours_ago
end

- (Object) table



48
49
50
# File 'lib/queries/content_filter_query.rb', line 48

def table
  Content.arel_table  
end

- (Object) where_sql



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/queries/content_filter_query.rb', line 22

def where_sql

  clauses = [
    for_topic,
    for_otu,
    recent,
  ].compact

  return nil if clauses.empty?

  scope = clauses.shift
  clauses.each do |c|
    scope = scope.and(c)
  end
  scope.to_sql
end