Module: Repertoire::Faceting::Facets::NestedFacet

Includes:
Arel, AbstractFacet
Defined in:
lib/repertoire-faceting/facets/nested_facet.rb

Overview

Implementation of AbstractFacet for facets whose values fall into a nested taxonomy. By default, all facets that group several columns will follow this behavior.

See Repertoire::Faceting::Model::ClassMethods for usage.

Instance Attribute Summary

Attributes included from AbstractFacet

#facet_name

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AbstractFacet

#drop_index, #facet_indexed?, #refresh_index, #stat_table

Class Method Details

.claim?(relation) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/repertoire-faceting/facets/nested_facet.rb', line 14

def self.claim?(relation)
  relation.group_values.size > 1
end

Instance Method Details

#create_indexObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/repertoire-faceting/facets/nested_facet.rb', line 36

def create_index
  levels = group_values.length

  # Construct expressions at each grouping level, right pad with nil
  expr_drills = (0..levels-1).map do |i|
    group_values[0..i] + (i+1..levels-1).collect { "NULL" }
  end

  # Construct indexes for each drill level
  queries = []
  expr_drills.each_with_index do |exprs, level|
    rel = only(:where, :joins)
    exprs.zip(columns).each do |expr, col|
      rel = rel.select("#{expr} AS #{col}")
    end
    rel = rel.group(columns[0..level])

    queries << rel.select(["#{level+1} AS level", "facet.signature(#{table_name}.#{faceting_id})", "now() AS updated_at"]).to_sql
  end

  # Root of tree
  empty_cols = columns.map { |col| "NULL AS #{col}"}
  queries << only(:where).select(empty_cols + ["0 AS level", "facet.signature(#{table_name}.#{faceting_id})", "now() AS updated_at"]).to_sql

  # Give the fullest index first (i.e. leaves of the tree), so the database
  # can resolve types before encountering any NULL values (i.e. values of
  # indeterminate type)
  queries = queries.reverse

  # The full index table is union of indices at each drill level
  sql = queries.join(" UNION ")

  connection.create_materialized_view(facet_index_name, sql)
end

#drill(state) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/repertoire-faceting/facets/nested_facet.rb', line 27

def drill(state)
  return read_index(state, false) if facet_indexed?
  rel = only(:where, :joins)
  grp = bind_nest(group_values, state) do |expr, val|
    rel = rel.where("#{expr} = #{connection.quote(val)}")
  end
  rel.group(grp).select(["#{grp.last} AS #{facet_name}", "facet.signature(#{table_name}.#{faceting_id})"]).arel
end

#signature(state) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/repertoire-faceting/facets/nested_facet.rb', line 18

def signature(state)
  return read_index(state, true) if facet_indexed?
  rel = only(:where, :joins)
  bind_nest(group_values, state) do |expr, val|
    rel = rel.where("#{expr} = #{connection.quote(val)}")
  end
  rel.select("facet.signature(#{table_name}.#{faceting_id})").arel
end