Class: RmaReasonCode

Inherits:
ApplicationRecord show all
Defined in:
app/models/rma_reason_code.rb

Overview

== Schema Information

Table name: rma_reason_codes
Database name: primary

id :integer not null, primary key
active :boolean default(TRUE), not null
advanced_reason_code :boolean default(FALSE), not null
aliases :string default([]), is an Array
category :string(255)
code :string(255)
customer_fault :boolean
default_location :string(255)
description :text
name :string(255)
replacement_order_type :string(2) default("TO")
created_at :datetime
updated_at :datetime
creator_id :integer
updater_id :integer

Indexes

index_rma_reason_codes_on_aliases (aliases) USING gin
index_rma_reason_codes_on_code (code) UNIQUE
index_rma_reason_codes_on_customer_fault (customer_fault)

Constant Summary collapse

EMPTY_PL_SQL =

Empty pl sql.

%{
  NOT EXISTS(
    SELECT 1 FROM product_lines_rma_reason_codes plrrc
    WHERE
      plrrc.rma_reason_code_id = rma_reason_codes.id
  )
}
EMPTY_PC_SQL =

Empty pc sql.

%{
  NOT EXISTS(
    SELECT 1 FROM product_categories_rma_reason_codes pcrrc
    WHERE
      pcrrc.rma_reason_code_id = rma_reason_codes.id
)}

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Has many collapse

Has and belongs to many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransortable_attributes, #to_relation

Methods included from Models::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#codeString(255) (readonly)

Returns:

  • (String(255))


42
# File 'app/models/rma_reason_code.rb', line 42

validates :code, uniqueness: true, presence: true

Class Method Details

.activeActiveRecord::Relation<RmaReasonCode>

A relation of RmaReasonCodes that are active. Active Record Scope

Returns:

See Also:



66
# File 'app/models/rma_reason_code.rb', line 66

scope :active, -> { where(active: true) }

.find_by_alias(raw) ⇒ RmaReasonCode?

Looks up the internal reason code mapped to an external/retailer reason
code via the +aliases+ column (seeded with Amazon's return-reason
vocabulary in db/migrate/20240228062357_populate_rma_aliases.rb). Aliases
are matched verbatim after the same normalization the column gets
(strip/blank per element), so callers should pass the retailer code as
received (e.g. 'DAMAGED_BY_FC').

Parameters:

  • raw (String, nil)

    the external reason code to map.

Returns:

  • (RmaReasonCode, nil)

    the matching code, or nil when unmapped.



131
132
133
134
135
136
# File 'app/models/rma_reason_code.rb', line 131

def self.find_by_alias(raw)
  normalized = Heatwave::Normalizers.default(raw)
  return nil if normalized.nil?

  where("? = ANY(aliases)", normalized).first
end

.for_any_product_categoriesActiveRecord::Relation<RmaReasonCode>

A relation of RmaReasonCodes that are for any product categories. Active Record Scope

Returns:

See Also:



105
# File 'app/models/rma_reason_code.rb', line 105

scope :for_any_product_categories, -> { where(EMPTY_PC_SQL) }

.for_any_product_linesActiveRecord::Relation<RmaReasonCode>

A relation of RmaReasonCodes that are for any product lines. Active Record Scope

Returns:

See Also:



104
# File 'app/models/rma_reason_code.rb', line 104

scope :for_any_product_lines, -> { where(EMPTY_PL_SQL) }

.for_product_category_idsActiveRecord::Relation<RmaReasonCode>

A relation of RmaReasonCodes that are for product category ids. Active Record Scope

Returns:

See Also:



88
89
90
91
# File 'app/models/rma_reason_code.rb', line 88

scope :for_product_category_ids, ->(*ids) {
  all_ids = ProductCategory.self_and_ancestors_ids(ids.flatten)
  for_product_category_ids_exact(all_ids)
}

.for_product_category_ids_exactActiveRecord::Relation<RmaReasonCode>

A relation of RmaReasonCodes that are for product category ids exact. Active Record Scope

Returns:

See Also:



92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/rma_reason_code.rb', line 92

scope :for_product_category_ids_exact, ->(*ids) {
  where(%{
    EXISTS(
      SELECT 1 FROM product_categories_rma_reason_codes pcrrc
      WHERE
        pcrrc.rma_reason_code_id = rma_reason_codes.id
        AND pcrrc.product_category_id IN (:product_category_ids)
    )
    OR #{EMPTY_PC_SQL}
  },
        product_category_ids: ids.flatten)
}

.for_product_line_idsActiveRecord::Relation<RmaReasonCode>

A relation of RmaReasonCodes that are for product line ids. Active Record Scope

Returns:

See Also:



70
71
72
73
# File 'app/models/rma_reason_code.rb', line 70

scope :for_product_line_ids, ->(*ids) {
  all_ids = ProductLine.self_and_ancestors_ids(ids.flatten)
  for_product_line_ids_exact(all_ids)
}

.for_product_line_ids_exactActiveRecord::Relation<RmaReasonCode>

A relation of RmaReasonCodes that are for product line ids exact. Active Record Scope

Returns:

See Also:



74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/rma_reason_code.rb', line 74

scope :for_product_line_ids_exact, ->(*ids) {
  where(%{
    EXISTS(
      SELECT 1 FROM product_lines_rma_reason_codes plrrc
      WHERE
        plrrc.rma_reason_code_id = rma_reason_codes.id
        AND plrrc.product_line_id IN (:product_line_ids)
    )
	  OR #{EMPTY_PL_SQL}
  },
        product_line_ids: ids.flatten)
}

.for_select(include_advanced: true) ⇒ void

Parameters:

  • include_advanced (Object) (defaults to: true)
  • include_advanced (Object) (defaults to: true)

Returns:

  • (void)
  • (void)


159
160
161
162
163
164
165
# File 'app/models/rma_reason_code.rb', line 159

def self.for_select(include_advanced: true)
  rrcs = RmaReasonCode.active
  rrcs = rrcs.where.not(advanced_reason_code: true) unless include_advanced
  rrcs.map do |e|
    [e.code_and_description, e.code, { 'data-default-location' => e.default_location }]
  end
end

.ransackable_scopes(_auth_object = nil) ⇒ void

Parameters:

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

Returns:

  • (void)
  • (void)


118
119
120
# File 'app/models/rma_reason_code.rb', line 118

def self.ransackable_scopes(_auth_object = nil)
  %i[search_code_or_desc for_product_category_ids for_product_line_ids]
end

.search_code_or_descActiveRecord::Relation<RmaReasonCode>

A relation of RmaReasonCodes that are search code or desc. Active Record Scope

Returns:

See Also:



106
107
108
109
110
111
112
# File 'app/models/rma_reason_code.rb', line 106

scope :search_code_or_desc, ->(term) {
  where(%(
    code = :code
    OR description ILIKE :description
    OR name ILIKE :description
  ), code: term.upcase, description: "%#{term}%")
}

.select_options(include_advanced: true) ⇒ void

Parameters:

  • include_advanced (Object) (defaults to: true)
  • include_advanced (Object) (defaults to: true)

Returns:

  • (void)
  • (void)


142
143
144
145
146
# File 'app/models/rma_reason_code.rb', line 142

def self.select_options(include_advanced: true)
  rrcs = RmaReasonCode.active
  rrcs = rrcs.where.not(advanced_reason_code: true) unless include_advanced
  rrcs.order(:code).map { |r| [r.to_s, r.code] }
end

Instance Method Details

#code_and_descriptionvoid

This method returns an undefined value.



149
150
151
152
153
# File 'app/models/rma_reason_code.rb', line 149

def code_and_description
  s = "[#{code}] #{description.presence || name}"
  s += " (#{aliases.join(', ')})" if aliases.present?
  s
end

#product_categoriesActiveRecord::Relation<ProductCategory>

Returns:



39
# File 'app/models/rma_reason_code.rb', line 39

has_and_belongs_to_many :product_categories

#product_linesActiveRecord::Relation<ProductLine>

Returns:



37
# File 'app/models/rma_reason_code.rb', line 37

has_and_belongs_to_many :product_lines

#restocked_rma_itemsActiveRecord::Relation<RmaItem>

Returns:

  • (ActiveRecord::Relation<RmaItem>)


34
# File 'app/models/rma_reason_code.rb', line 34

has_many :restocked_rma_items, class_name: "RmaItem", foreign_key: "restocking_reason", primary_key: "code"

#returned_rma_itemsActiveRecord::Relation<RmaItem>

Returns:

  • (ActiveRecord::Relation<RmaItem>)


32
# File 'app/models/rma_reason_code.rb', line 32

has_many :returned_rma_items, class_name: "RmaItem", foreign_key: "returned_reason", primary_key: "code"

#should_archive?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'app/models/rma_reason_code.rb', line 173

def should_archive?
  returned_rma_items.present? || restocked_rma_items.present?
end

#to_sString

Returns:

  • (String)


168
169
170
# File 'app/models/rma_reason_code.rb', line 168

def to_s
  code_and_description
end