Files
warp_engine/app/models/warp_engine/software_image.rb
T
mr.zeroandClaude Fable 5 7abfb89bca Phase 2: move the 8 catalog models into the WarpEngine engine
- Software, Release, ReleaseAsset, ExternalLink, PlatformLink, Image,
  SoftwareImage, Download now live in the engine under WarpEngine::, on top
  of WarpEngine::ApplicationRecord; table names unchanged (empty prefix)
- each model runs an ActiveSupport load hook (:warp_engine_<model>) as a
  host extension point
- WarpEngine::Image reads its upload path from WarpEngine.config
- host references fully qualified (services, serializers, admin, specs);
  admin registrations renamed with as: so /admin URLs and route helpers are
  byte-identical; factories pinned to the namespaced classes
- Member#image now class_name: "WarpEngine::Image"

Verified: full suite green, /api/software and /api/builds byte-identical to
the phase-0 baselines, admin routes unchanged.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-04 18:51:12 +02:00

46 lines
1.3 KiB
Ruby

module WarpEngine
class SoftwareImage < ApplicationRecord
belongs_to :software
belongs_to :image, optional: true
attr_accessor :file_upload
before_validation :create_image_from_upload, if: -> { file_upload.present? }
validates :image_id, presence: true
validates :image_id, uniqueness: { scope: :software_id }
validates :position, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
before_save :unset_other_defaults, if: -> { is_default? && is_default_changed? }
default_scope { order(:position) }
def self.ransackable_attributes(auth_object = nil)
%w[created_at id image_id is_default position software_id updated_at]
end
def self.ransackable_associations(auth_object = nil)
%w[image software]
end
ActiveSupport.run_load_hooks(:warp_engine_software_image, self)
private
def create_image_from_upload
img = Image.new(file_upload: file_upload)
if img.save
self.image = img
else
errors.add(:file_upload, img.errors.full_messages.join(", "))
end
end
def unset_other_defaults
SoftwareImage.where(software_id: software_id, is_default: true)
.where.not(id: id)
.update_all(is_default: false)
end
end
end