Module: Models::SearchableViewWithLineItems::ClassMethods

Defined in:
app/concerns/models/searchable_view_with_line_items.rb

Overview

ActiveSupport::Concern mixin: class methods.

Instance Method Summary collapse

Instance Method Details

#clean_array_params(params) ⇒ Array<Integer>

Normalizes scope arguments into a clean list of integer IDs.

Parameters:

  • params (Array<Object>, Object)

    raw scope argument(s) — strings, integers, or nested arrays

Returns:

  • (Array<Integer>)

    flattened, compacted, de-duplicated IDs



34
35
36
# File 'app/concerns/models/searchable_view_with_line_items.rb', line 34

def clean_array_params(params)
  [params].flatten.compact.uniq.map(&:to_i)
end

#item_sql_checkString

SQL EXISTS fragment matching resources that have any of the given items
among their line items.

Returns:

  • (String)

    SQL EXISTS fragment; expects the item ID array as a bound parameter



60
61
62
63
64
65
66
67
68
69
# File 'app/concerns/models/searchable_view_with_line_items.rb', line 60

def item_sql_check
  %{
  EXISTS (select 1
          from line_items li
          where
            li.resource_id = #{table_name}.id
            and li.resource_type = '#{main_resource_class}'
            and li.item_id IN (?))
  }
end

#product_category_sql_checkString

Uses ltree for efficient descendant queries on items' denormalized paths

Returns:

  • (String)

    SQL EXISTS fragment matching resources whose line items
    belong to the given product categories (or any of their descendants);
    expects the product category ID array as a bound parameter



76
77
78
79
80
81
82
83
84
85
86
# File 'app/concerns/models/searchable_view_with_line_items.rb', line 76

def product_category_sql_check
  %{
    EXISTS (select 1
            from line_items li
            inner join items i on li.item_id = i.id
            where
              li.resource_id = #{table_name}.id
              and li.resource_type = '#{main_resource_class}'
              and i.pc_path_ids <@ ANY(SELECT ltree_path_ids FROM product_categories WHERE id = ANY(ARRAY[?]::integer[])))
  }
end

#product_line_sql_checkString

Uses ltree for efficient descendant queries on items' denormalized paths

Returns:

  • (String)

    SQL EXISTS fragment matching resources whose line items
    belong to the given product lines (or any of their descendants);
    expects the product line ID array twice as bound parameters



43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/concerns/models/searchable_view_with_line_items.rb', line 43

def product_line_sql_check
  %{
    EXISTS (select 1
            from line_items li
            inner join items i on i.id = li.item_id
            where
              li.resource_id = #{table_name}.id
              and li.resource_type = '#{main_resource_class}'
              and (i.primary_pl_path_ids <@ ANY(SELECT ltree_path_ids FROM product_lines WHERE id = ANY(ARRAY[?]::integer[]))
                   OR i.all_pl_paths_ids && ARRAY(SELECT ltree_path_ids FROM product_lines WHERE id = ANY(ARRAY[?]::integer[]))::ltree[]))
  }
end

#ransackable_scopes(_auth_object = nil) ⇒ Array<Symbol>

Extend the parent concern's ransackable_scopes with line-item scopes.
Must live in ClassMethods (not included do def self.) so that super
in the including model's own ransackable_scopes properly traverses here.

Parameters:

  • _auth_object (Object, nil) (defaults to: nil)

    unused authorization context (kept for Ransack compatibility)

Returns:

  • (Array<Symbol>)

    the whitelist of scopes Ransack may call on this class



24
25
26
27
28
# File 'app/concerns/models/searchable_view_with_line_items.rb', line 24

def ransackable_scopes(_auth_object = nil)
  super + %i[contains_item_ids not_contains_item_ids
             contains_product_line_ids not_contains_product_line_ids
             contains_product_category_ids not_contains_product_category_ids]
end