Class: Report::ItemsSpecMatrix::Command

Inherits:
BaseCommand
  • Object
show all
Defined in:
app/services/report/items_spec_matrix/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#item_keysObject (readonly)

Returns the value of attribute item_keys.



13
14
15
# File 'app/services/report/items_spec_matrix/command.rb', line 13

def item_keys
  @item_keys
end

Instance Method Details

#executeObject



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/services/report/items_spec_matrix/command.rb', line 15

def execute
  return unless valid?

  # Give me a bunch of item and i will pull all the specs and item in a chart so you can quickly compare
  items = Item.non_publications.joins(:product_category).order(ProductCategory[:lineage_expanded], :sku)
  items = items.where(id: item_ids) if item_ids.present?
  items = items.by_product_line_id(product_line_ids) if product_line_ids.present?
  items = items.by_product_category_id_direct(product_category_ids) if product_category_ids.present?
  items = items.active_in_catalog_ids(catalog_ids) if catalog_ids.present?
  items = items.public_and_active_in_catalog_id([1, 2]) if public_eq
  items = items.where(condition: condition_in) if condition_in.present?
  items = items.includes(:product_category, :primary_product_line)
  @item_keys = []
  @results = {}
  @locale_eq = Mobility.locale if @locale_eq.blank?

  items.each do |item|
    item_key = {
      item_id: item.id,
      item_sku: item.sku,
      item_name: item.name,
      item_state: item.item_state,
      item: item,
      product_category_id: item.product_category_id,
      product_category_name: item.product_category&.name,
      primary_product_line_id: item.primary_product_line_id,
      primary_product_line_name: item.primary_product_line&.name
    }
    Mobility.with_locale(@locale_eq) do
      (item.rendered_product_specifications || {}).each do |token, spec|
        next if tokens.present? && !tokens.include?(token.to_s)
        next if %w[sku product_category_name].include? token
        next if spec_grouping_in.present? && spec_grouping_in.exclude?(spec[:grouping])

        group_key = spec[:grouping] || 'Other'
        @results[group_key] ||= {}
        token_key = { token: token,
                      name: spec[:name],
                      grouping: spec[:grouping],
                      method: spec[:method],
                      units: spec[:units] }
        @results[group_key][token_key] ||= {}

        @item_keys << item_key unless @item_keys.include?(item_key)
        @results[group_key][token_key][item_key] = spec
        @results[group_key][token_key] = Hash[@results[group_key][token_key].sort_by { |k, _v| k[:item_sku] }]
        @results[group_key] = Hash[@results[group_key].sort_by { |k, _v| k[:name] }]
      end
    end
  end
  @item_keys = @item_keys.sort_by { |ik| ik[:item_sku] }
  @results = Hash[@results.sort]
  @results
end

#success?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/services/report/items_spec_matrix/command.rb', line 70

def success?
  valid? && @results.present?
end