Class: Tiler::Widgets::CommentsQuery

Inherits:
Query::Base show all
Defined in:
lib/tiler/widgets/comments.rb

Constant Summary collapse

MAX_LIMIT =
100
DEFAULT_ROTATE_SECONDS =
8

Constants inherited from Query::Base

Query::Base::SAFE_COLUMN_RE

Instance Attribute Summary

Attributes inherited from Query::Base

#config, #panel, #source

Instance Method Summary collapse

Methods inherited from Query::Base

#initialize

Constructor Details

This class inherits a constructor from Tiler::Query::Base

Instance Method Details

#callObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tiler/widgets/comments.rb', line 10

def call
  quote_col  = config["quote_column"]
  name_col   = config["name_column"]
  avatar_col = config["avatar_column"]
  limit      = clamp_limit(config["limit"])

  items =
    if quote_col.present? && safe_col?(quote_col)
      cols = [ Arel.sql(json_extract(quote_col)) ]
      cols << (name_col.present? && safe_col?(name_col) ? Arel.sql(json_extract(name_col)) : Arel.sql("NULL"))
      cols << (avatar_col.present? && safe_col?(avatar_col) ? Arel.sql(json_extract(avatar_col)) : Arel.sql("NULL"))

      base_scope.order(recorded_at: :desc).limit(limit)
                .pluck(*cols)
                .map do |row|
                  {
                    quote:  row[0].to_s,
                    name:   row[1].nil? ? nil : row[1],
                    avatar: safe_url(row[2])
                  }
                end
    else
      []
    end

  items = items.reject { |i| i[:quote].to_s.strip.empty? }

  {
    items:          items,
    rotate_seconds: rotate_seconds
  }
end