Class: Facet

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/facet.rb

Overview

== Schema Information

Table name: facets
Database name: primary

id :integer not null, primary key
description :string
grouping_spec_tokens :string default([]), is an Array
name :string
product_category_ids :string
product_line_ids :string
sort_keys :string default(["price asc", "item_sku asc"]), is an Array
use_product_picture :boolean default(FALSE), not null
created_at :datetime not null
updated_at :datetime not null

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Has and belongs to many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #creator, #should_not_save_version, #stamp_record, #updater

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Class Method Details

.sort_keys_select_optionsObject

Class method: returns all database columns as sort options



72
73
74
75
76
# File 'app/models/facet.rb', line 72

def self.sort_keys_select_options
  column_options = ViewProductCatalog.columns.map(&:name).sort.flat_map { |n| ["#{n} asc", "#{n} desc"] }
  spec_token_options = ProductSpecification.token_select_options.flat_map { |t| ["spec:#{t} asc", "spec:#{t} desc"] }
  column_options + spec_token_options
end

Instance Method Details

#applicable_itemsObject



85
86
87
# File 'app/models/facet.rb', line 85

def applicable_items
  Item.by_product_line_id(product_line_ids).by_product_category_id(product_category_ids).order(:sku)
end

#deep_dupObject



27
28
29
30
31
# File 'app/models/facet.rb', line 27

def deep_dup
  deep_clone(include: [:product_lines, :product_categories]) do |original, copy|
    copy.name = "#{original.name} (copy)" if copy.is_a?(Facet)
  end
end

#product_categoriesActiveRecord::Relation<ProductCategory>

Returns:

See Also:



21
# File 'app/models/facet.rb', line 21

has_and_belongs_to_many :product_categories

#product_linesActiveRecord::Relation<ProductLine>

Returns:

See Also:



20
# File 'app/models/facet.rb', line 20

has_and_belongs_to_many :product_lines

#refresh_specs(token: nil, items: nil) ⇒ Object



89
90
91
92
93
94
95
96
# File 'app/models/facet.rb', line 89

def refresh_specs(token: nil, items: nil)
  tokens = ([token].compact.presence || grouping_spec_tokens).map(&:to_sym)
  (items || applicable_items).each do |item|
    item.update_rendered_product_specifications(tokens: tokens)
  end
  # Also touch product line
  product_lines.each(&:purge_edge_cache)
end

#sort_keys_select_optionsObject

Instance method: returns sort options with this facet's grouping tokens prioritized



79
80
81
82
83
# File 'app/models/facet.rb', line 79

def sort_keys_select_options
  # Place this facet's grouping tokens at the top
  my_spec_options = tokens.flat_map { |t| ["spec:#{t} asc", "spec:#{t} desc"] }
  my_spec_options | self.class.sort_keys_select_options
end

#to_sObject



58
59
60
# File 'app/models/facet.rb', line 58

def to_s
  "#{name} [#{id}]"
end

#tokensObject



62
63
64
# File 'app/models/facet.rb', line 62

def tokens
  grouping_spec_tokens.map(&:presence).compact
end

#tokens_select_optionsObject

Provide a sorted list respecting the selected order



67
68
69
# File 'app/models/facet.rb', line 67

def tokens_select_options
  tokens | ProductSpecification.token_select_options
end