Class: MaterialAlert
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- MaterialAlert
- Defined in:
- app/models/material_alert.rb
Overview
== Schema Information
Table name: material_alerts
Database name: primary
id :bigint not null, primary key
actual_qty :integer
group_name :string
group_type :string
name :string
recommended_qty :integer
signature :string not null
unmaskable :boolean default(FALSE), not null
created_at :datetime not null
updated_at :datetime not null
order_id :bigint
quote_id :bigint
room_configuration_id :bigint
Indexes
idx_material_alerts_order_sig (order_id,signature)
idx_material_alerts_quote_sig (quote_id,signature)
idx_material_alerts_rc_sig (room_configuration_id,signature)
Foreign Keys
fk_rails_... (order_id => orders.id) ON DELETE => cascade
fk_rails_... (quote_id => quotes.id) ON DELETE => cascade
fk_rails_... (room_configuration_id => room_configurations.id) ON DELETE => cascade
Constant Summary collapse
- ITEM_INCLUDES =
Scopes for each resource type with eager loading
We include both :items (through association) and material_alert_items: :item
to ensure items are preloaded regardless of how they're accessed in views.
Additional includes on items:- :primary_image for image_url2
- :orderable_view_product_catalogs for is_available_to_public?
%i[primary_image orderable_view_product_catalogs].freeze
Constants included from Schedulable
Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary collapse
-
#signature ⇒ Object
readonly
Validations.
Belongs to collapse
-
#order ⇒ Order
Associations - exactly one of these will be set.
- #quote ⇒ Quote
- #room_configuration ⇒ RoomConfiguration
Has many collapse
- #items ⇒ ActiveRecord::Relation<Item>
-
#material_alert_items ⇒ ActiveRecord::Relation<MaterialAlertItem>
Join table for items recommended in this alert.
Class Method Summary collapse
-
.create_from_check_result(resource, result, signature) ⇒ MaterialAlert?
Create a MaterialAlert from a check result hash.
-
.for_order ⇒ ActiveRecord::Relation<MaterialAlert>
A relation of MaterialAlerts that are for order.
-
.for_quote ⇒ ActiveRecord::Relation<MaterialAlert>
A relation of MaterialAlerts that are for quote.
-
.for_resource(resource, signature:) ⇒ ActiveRecord::Relation
Find alerts for any resource type.
-
.for_room_configuration ⇒ ActiveRecord::Relation<MaterialAlert>
A relation of MaterialAlerts that are for room configuration.
-
.invalidate_for(resource) ⇒ Integer
Delete all alerts for a resource (used for invalidation).
-
.regenerate_for(resource, check_results:, signature:) ⇒ Array<MaterialAlert>
Regenerate alerts for a resource from check results.
Instance Method Summary collapse
-
#resource ⇒ Object
Returns the associated resource (whichever is set).
-
#room ⇒ Object
Convenience method to match Virtus interface (views use material_alert.room).
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
Methods included from Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Instance Attribute Details
#signature ⇒ Object (readonly)
Validations
Validations:
47 |
# File 'app/models/material_alert.rb', line 47 validates :signature, presence: true |
Class Method Details
.create_from_check_result(resource, result, signature) ⇒ MaterialAlert?
Create a MaterialAlert from a check result hash
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'app/models/material_alert.rb', line 132 def create_from_check_result(resource, result, signature) attrs = { signature: signature, name: result[:name], recommended_qty: result[:recommended_qty], actual_qty: result[:actual_qty], group_name: result[:group_name], group_type: result[:group_type], unmaskable: result[:unmaskable] || false } # Set the appropriate FK based on resource type case resource when Order then attrs[:order] = resource when Quote then attrs[:quote] = resource when RoomConfiguration then attrs[:room_configuration] = resource end alert = create!(attrs) # Add associated items, deduplicating by id to avoid unique constraint violations # when check results return the same item more than once (incident #3635). if result[:items].present? result[:items].compact.uniq(&:id).each_with_index do |item, position| alert.material_alert_items.create!(item: item, position: position) end end alert rescue ActiveRecord::RecordInvalid => e Rails.logger.warn("Failed to create MaterialAlert: #{e.}") nil end |
.for_order ⇒ ActiveRecord::Relation<MaterialAlert>
A relation of MaterialAlerts that are for order. Active Record Scope
58 59 60 61 |
# File 'app/models/material_alert.rb', line 58 scope :for_order, ->(order, signature:) { where(order: order, signature: signature) .includes(items: ITEM_INCLUDES, material_alert_items: { item: ITEM_INCLUDES }) } |
.for_quote ⇒ ActiveRecord::Relation<MaterialAlert>
A relation of MaterialAlerts that are for quote. Active Record Scope
63 64 65 66 |
# File 'app/models/material_alert.rb', line 63 scope :for_quote, ->(quote, signature:) { where(quote: quote, signature: signature) .includes(items: ITEM_INCLUDES, material_alert_items: { item: ITEM_INCLUDES }) } |
.for_resource(resource, signature:) ⇒ ActiveRecord::Relation
Find alerts for any resource type
88 89 90 91 92 93 94 95 96 |
# File 'app/models/material_alert.rb', line 88 def for_resource(resource, signature:) case resource when Order then for_order(resource, signature: signature) when Quote then for_quote(resource, signature: signature) when RoomConfiguration then for_room_configuration(resource, signature: signature) else raise ArgumentError, "Unknown resource type: #{resource.class}" end end |
.for_room_configuration ⇒ ActiveRecord::Relation<MaterialAlert>
A relation of MaterialAlerts that are for room configuration. Active Record Scope
68 69 70 71 |
# File 'app/models/material_alert.rb', line 68 scope :for_room_configuration, ->(rc, signature:) { where(room_configuration: rc, signature: signature) .includes(items: ITEM_INCLUDES, material_alert_items: { item: ITEM_INCLUDES }) } |
.invalidate_for(resource) ⇒ Integer
Delete all alerts for a resource (used for invalidation)
101 102 103 104 105 106 107 108 109 |
# File 'app/models/material_alert.rb', line 101 def invalidate_for(resource) case resource when Order then where(order: resource).delete_all when Quote then where(quote: resource).delete_all when RoomConfiguration then where(room_configuration: resource).delete_all else 0 end end |
.regenerate_for(resource, check_results:, signature:) ⇒ Array<MaterialAlert>
Regenerate alerts for a resource from check results
116 117 118 119 120 121 122 123 124 125 |
# File 'app/models/material_alert.rb', line 116 def regenerate_for(resource, check_results:, signature:) return [] if check_results.blank? transaction do invalidate_for(resource) check_results.filter_map do |result| create_from_check_result(resource, result, signature) end end end |
Instance Method Details
#items ⇒ ActiveRecord::Relation<Item>
44 |
# File 'app/models/material_alert.rb', line 44 has_many :items, through: :material_alert_items |
#material_alert_items ⇒ ActiveRecord::Relation<MaterialAlertItem>
Join table for items recommended in this alert
43 |
# File 'app/models/material_alert.rb', line 43 has_many :material_alert_items, dependent: :destroy |
#order ⇒ Order
Associations - exactly one of these will be set
38 |
# File 'app/models/material_alert.rb', line 38 belongs_to :order, optional: true |
#resource ⇒ Object
Returns the associated resource (whichever is set)
79 80 81 |
# File 'app/models/material_alert.rb', line 79 def resource order || quote || room_configuration end |
#room ⇒ Object
Convenience method to match Virtus interface (views use material_alert.room)
74 75 76 |
# File 'app/models/material_alert.rb', line 74 def room room_configuration end |
#room_configuration ⇒ RoomConfiguration
40 |
# File 'app/models/material_alert.rb', line 40 belongs_to :room_configuration, optional: true |