Module: Queries::Concerns::Tags

Extended by:
ActiveSupport::Concern
Included in:
Queries::CollectingEvent::Filter, Queries::CollectionObject::Filter, Image::Filter, Person::Autocomplete, Person::Filter, Source::Filter, TaxonName::Filter
Defined in:
lib/queries/concerns/tags.rb

Overview

Helpers and facets for queries that reference Tags.

Test coverage is currently in spec/lib/queries/source/filter_spec.rb.

You must define

def table
  :;Model.arel_table
end

in including modules.

Instance Method Summary collapse

Instance Method Details

#keyword_id_andObject



39
40
41
# File 'lib/queries/concerns/tags.rb', line 39

def keyword_id_and
  [@keyword_id_and].flatten
end

#keyword_id_facetObject

Returns all sources that match all _and ids OR any OR id.

Returns:

  • all sources that match all _and ids OR any OR id



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/queries/concerns/tags.rb', line 58

def keyword_id_facet
  return nil if keyword_id_or.empty? && keyword_id_and.empty?
  k = table.name.classify.safe_constantize

  a = matching_keyword_id_or
  b = matching_keyword_id_and

  if a.nil?
    b
  elsif b.nil?
    a
  else
    k.from("( (#{a.to_sql}) UNION (#{b.to_sql})) as sources")
  end
end

#keyword_id_orObject



43
44
45
# File 'lib/queries/concerns/tags.rb', line 43

def keyword_id_or
  [@keyword_id_or].flatten
end

#keyword_ids=(value = []) ⇒ Object



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

def keyword_ids=(value = [])
  @keyword_ids = value
end

#matching_keyword_id_andObject

merge



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/queries/concerns/tags.rb', line 87

def matching_keyword_id_and
  return nil if keyword_id_and.empty?
  l = table.name
  k = l.classify.safe_constantize
  t = ::Tag.arel_table

  a = table.alias("k_#{l}")

  b = table.project(a[Arel.star]).from(a)
    .join(t)
    .on(
      t[:tag_object_id].eq(a[:id]),
      t[:tag_object_type].eq(k.name)
    )

  i = 0

  keyword_id_and.each do |j|
    t_a = t.alias("tk_#{l[0..5]}_#{i}")
    b = b.join(t_a).on(
      t_a['tag_object_id'].eq(a['id']),
      t_a[:tag_object_type].eq(k),
      t_a[:keyword_id].eq(j)
    )

    i += 1
  end

  b = b.group(a[:id]).having(t[:keyword_id].count.gteq(keyword_id_and.count))
  b = b.as("#{l}_ai")

  k.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b[:id].eq(table[:id]))))
end

#matching_keyword_id_orObject

merge



75
76
77
78
79
80
81
82
83
84
# File 'lib/queries/concerns/tags.rb', line 75

def matching_keyword_id_or
  return nil if keyword_id_or.empty?
  k = table.name.classify.safe_constantize
  t = ::Tag.arel_table

  w = t[:tag_object_id].eq(table[:id]).and( t[:tag_object_type].eq(table.name.classify))
  w = w.and( t[:keyword_id].eq_any(keyword_id_or) ) if keyword_id_or.any? 

  k.where( ::Tag.where(w).arel.exists )
end

#set_tags_params(params) ⇒ Object



32
33
34
35
36
37
# File 'lib/queries/concerns/tags.rb', line 32

def set_tags_params(params)
  @keyword_id_and = params[:keyword_id_and].blank? ? [] : params[:keyword_id_and]
  @keyword_id_or = params[:keyword_id_or].blank? ? [] : params[:keyword_id_or]

  @tags = (params[:tags]&.downcase == 'true' ? true : false) if !params[:tags].nil?
end

#tag_facetObject



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/queries/concerns/tags.rb', line 121

def tag_facet
  return nil if tags.nil?
  k = table.name.classify.safe_constantize

  if tags
    k.joins(:tags).distinct
  else
    k.left_outer_joins(:tags)
      .where(tags: {id: nil})
  end
end

#tag_tableArel::Table

Returns:

  • (Arel::Table)


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

def tag_table 
  ::Tag.arel_table
end