Class: RmaReasonCode
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- RmaReasonCode
- 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
- #code ⇒ String(255) readonly
Has many collapse
- #restocked_rma_items ⇒ ActiveRecord::Relation<RmaItem>
- #returned_rma_items ⇒ ActiveRecord::Relation<RmaItem>
Has and belongs to many collapse
- #product_categories ⇒ ActiveRecord::Relation<ProductCategory>
- #product_lines ⇒ ActiveRecord::Relation<ProductLine>
Class Method Summary collapse
-
.active ⇒ ActiveRecord::Relation<RmaReasonCode>
A relation of RmaReasonCodes that are active.
-
.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).
-
.for_any_product_categories ⇒ ActiveRecord::Relation<RmaReasonCode>
A relation of RmaReasonCodes that are for any product categories.
-
.for_any_product_lines ⇒ ActiveRecord::Relation<RmaReasonCode>
A relation of RmaReasonCodes that are for any product lines.
-
.for_product_category_ids ⇒ ActiveRecord::Relation<RmaReasonCode>
A relation of RmaReasonCodes that are for product category ids.
-
.for_product_category_ids_exact ⇒ ActiveRecord::Relation<RmaReasonCode>
A relation of RmaReasonCodes that are for product category ids exact.
-
.for_product_line_ids ⇒ ActiveRecord::Relation<RmaReasonCode>
A relation of RmaReasonCodes that are for product line ids.
-
.for_product_line_ids_exact ⇒ ActiveRecord::Relation<RmaReasonCode>
A relation of RmaReasonCodes that are for product line ids exact.
- .for_select(include_advanced: true) ⇒ void
- .ransackable_scopes(_auth_object = nil) ⇒ void
-
.search_code_or_desc ⇒ ActiveRecord::Relation<RmaReasonCode>
A relation of RmaReasonCodes that are search code or desc.
- .select_options(include_advanced: true) ⇒ void
Instance Method Summary collapse
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes, ransortable_attributes, #to_relation
Methods included from Models::Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Instance Attribute Details
#code ⇒ String(255) (readonly)
42 |
# File 'app/models/rma_reason_code.rb', line 42 validates :code, uniqueness: true, presence: true |
Class Method Details
.active ⇒ ActiveRecord::Relation<RmaReasonCode>
A relation of RmaReasonCodes that are active. Active Record Scope
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').
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_categories ⇒ ActiveRecord::Relation<RmaReasonCode>
A relation of RmaReasonCodes that are for any product categories. Active Record Scope
105 |
# File 'app/models/rma_reason_code.rb', line 105 scope :for_any_product_categories, -> { where(EMPTY_PC_SQL) } |
.for_any_product_lines ⇒ ActiveRecord::Relation<RmaReasonCode>
A relation of RmaReasonCodes that are for any product lines. Active Record Scope
104 |
# File 'app/models/rma_reason_code.rb', line 104 scope :for_any_product_lines, -> { where(EMPTY_PL_SQL) } |
.for_product_category_ids ⇒ ActiveRecord::Relation<RmaReasonCode>
A relation of RmaReasonCodes that are for product category ids. Active Record Scope
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_exact ⇒ ActiveRecord::Relation<RmaReasonCode>
A relation of RmaReasonCodes that are for product category ids exact. Active Record Scope
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_ids ⇒ ActiveRecord::Relation<RmaReasonCode>
A relation of RmaReasonCodes that are for product line ids. Active Record Scope
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_exact ⇒ ActiveRecord::Relation<RmaReasonCode>
A relation of RmaReasonCodes that are for product line ids exact. Active Record Scope
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
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
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_desc ⇒ ActiveRecord::Relation<RmaReasonCode>
A relation of RmaReasonCodes that are search code or desc. Active Record Scope
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
142 143 144 145 146 |
# File 'app/models/rma_reason_code.rb', line 142 def self.(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_description ⇒ void
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_categories ⇒ ActiveRecord::Relation<ProductCategory>
39 |
# File 'app/models/rma_reason_code.rb', line 39 has_and_belongs_to_many :product_categories |
#product_lines ⇒ ActiveRecord::Relation<ProductLine>
37 |
# File 'app/models/rma_reason_code.rb', line 37 has_and_belongs_to_many :product_lines |
#restocked_rma_items ⇒ 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_items ⇒ 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
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_s ⇒ String
168 169 170 |
# File 'app/models/rma_reason_code.rb', line 168 def to_s code_and_description end |