Class: Workarea::Search::Admin

Inherits:
Object
  • Object
show all
Includes:
Elasticsearch::Document
Defined in:
app/models/workarea/search/admin.rb,
app/models/workarea/search/admin/user.rb,
app/models/workarea/search/admin/order.rb,
app/models/workarea/search/admin/content.rb,
app/models/workarea/search/admin/release.rb,
app/models/workarea/search/admin/segment.rb,
app/models/workarea/search/admin/navigation.rb,
app/models/workarea/search/admin/releasable.rb,
app/models/workarea/search/admin/pricing_sku.rb,
app/models/workarea/search/admin/content_page.rb,
app/models/workarea/search/admin/content_asset.rb,
app/models/workarea/search/admin/inventory_sku.rb,
app/models/workarea/search/admin/catalog_product.rb,
app/models/workarea/search/admin/fulfillment_sku.rb,
app/models/workarea/search/admin/navigation_menu.rb,
app/models/workarea/search/admin/catalog_category.rb,
app/models/workarea/search/admin/pricing_discount.rb,
app/models/workarea/search/admin/payment_transaction.rb,
app/models/workarea/search/admin/search_customization.rb

Defined Under Namespace

Modules: Releasable Classes: CatalogCategory, CatalogProduct, Content, ContentAsset, ContentPage, FulfillmentSku, InventorySku, Navigation, NavigationMenu, Order, PaymentTransaction, PricingDiscount, PricingSku, Release, SearchCustomization, Segment, User

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Elasticsearch::Document

all, #as_bulk_document, current_index_prefix, #destroy, #initialize, #save

Class Method Details

.find_for_model(model) ⇒ Object



59
60
61
62
# File 'app/models/workarea/search/admin.rb', line 59

def self.find_for_model(model)
  subclass = model.model_name.singular_route_key.camelize
  "Workarea::Search::Admin::#{subclass}".constantize.new(model)
end

.for(model) ⇒ Object



53
54
55
56
57
# File 'app/models/workarea/search/admin.rb', line 53

def self.for(model)
  find_for_model(model)
rescue NameError
  find_for_model(model._root) rescue nil
end

.jump_to(params, size = Workarea.config.default_admin_jump_to_result_count) ⇒ Object



6
7
8
9
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
42
43
44
45
46
47
48
49
50
51
# File 'app/models/workarea/search/admin.rb', line 6

def self.jump_to(params, size = Workarea.config.default_admin_jump_to_result_count)
  query = {
    aggs: {
      grouped_by_type: {
        filter: {
          match_phrase_prefix: {
            jump_to_search_text: {
              query: params[:q],
              max_expansions: 10
            }
          }
        },
        aggs: {
          type: {
            terms: { field: 'facets.type', size: Workarea.config.jump_to_type_limit },
            aggs: {
              top: {
                top_hits: {
                  size: Workarea.config.jump_to_results_per_type,
                  sort: [
                    { _score: { order: 'desc' } },
                    { updated_at: { order: 'desc' } }
                  ]
                }
              }
            }
          }
        }
      }
    }
  }

  aggregation = search(query)['aggregations']['grouped_by_type']['type']
  aggregation['buckets']
    .reduce([]) { |m, b| m + b['top']['hits']['hits'] }
    .sort_by { |r| [r['_source']['jump_to_position'] || 999, r['_score'] || 0] }
    .map do |result|
      {
        label: result['_source']['jump_to_text'],
        type: result['_source']['facets']['type'],
        model_class: result['_source']['model_class'],
        route_helper: result['_source']['jump_to_route_helper'],
        to_param: result['_source']['jump_to_param']
      }
    end
end

Instance Method Details

#as_documentObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/workarea/search/admin.rb', line 140

def as_document
  {
    id: id,
    name: name,
    facets: facets,
    created_at: created_at,
    updated_at: updated_at,
    keywords: sanitized_keywords,
    search_text: search_text,
    jump_to_text: jump_to_text,
    jump_to_search_text: jump_to_search_text,
    jump_to_position: jump_to_position,
    jump_to_route_helper: jump_to_route_helper,
    jump_to_param: jump_to_param,
    releasable: releasable?
  }
end

#created_atObject



128
129
130
# File 'app/models/workarea/search/admin.rb', line 128

def created_at
  model.created_at
end

#facetsObject



102
103
104
105
106
# File 'app/models/workarea/search/admin.rb', line 102

def facets
  base = { status: status, type: type }
  base[:tags] = model.tags if model.respond_to?(:tags)
  base
end

#idObject



74
75
76
# File 'app/models/workarea/search/admin.rb', line 74

def id
  "#{type}-#{model.id}"
end

#jump_to_paramObject



124
125
126
# File 'app/models/workarea/search/admin.rb', line 124

def jump_to_param
  model.to_param
end

#jump_to_positionObject



116
117
118
# File 'app/models/workarea/search/admin.rb', line 116

def jump_to_position
  999
end

#jump_to_route_helperObject



120
121
122
# File 'app/models/workarea/search/admin.rb', line 120

def jump_to_route_helper
  "#{model.model_name.singular_route_key}_path"
end

#jump_to_search_textObject



112
113
114
# File 'app/models/workarea/search/admin.rb', line 112

def jump_to_search_text
  search_text
end

#jump_to_textObject



108
109
110
# File 'app/models/workarea/search/admin.rb', line 108

def jump_to_text
  name
end

#keywordsObject



94
95
96
# File 'app/models/workarea/search/admin.rb', line 94

def keywords
  [model.id, *model.try(:tags)]
end

#nameObject



78
79
80
# File 'app/models/workarea/search/admin.rb', line 78

def name
  model.name
end

#releasable?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'app/models/workarea/search/admin.rb', line 136

def releasable?
  model.is_a?(Workarea::Releasable)
end

#sanitized_keywordsObject



98
99
100
# File 'app/models/workarea/search/admin.rb', line 98

def sanitized_keywords
  keywords.reject(&:blank?).map(&:to_s).map(&:downcase).map(&:strip)
end

#search_textObject

Raises:

  • (NotImplementedError)


90
91
92
# File 'app/models/workarea/search/admin.rb', line 90

def search_text
  raise(NotImplementedError, "#{self.class} must implement #search_text")
end

#should_be_indexed?Boolean

Allows subclass instances to specify whether they should be included in the Admin index. For example, allow system Content to be included, but not Content that belongs to a model.

Returns:

  • (Boolean)


70
71
72
# File 'app/models/workarea/search/admin.rb', line 70

def should_be_indexed?
  true
end

#statusObject



86
87
88
# File 'app/models/workarea/search/admin.rb', line 86

def status
  nil
end

#typeObject



82
83
84
# File 'app/models/workarea/search/admin.rb', line 82

def type
  model.class.name.demodulize.underscore
end

#updated_atObject



132
133
134
# File 'app/models/workarea/search/admin.rb', line 132

def updated_at
  model.updated_at
end