Sha256: 612ba31d5029237a370676ba9e37a8d650a82a973473f15d440f09d161bb6e9b

Contents?: true

Size: 1.62 KB

Versions: 11

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

class Comfy::Cms::File < ActiveRecord::Base

  self.table_name = "comfy_cms_files"

  include Comfy::Cms::WithCategories

  VARIANT_SIZE = {
    redactor: { resize: "100x75^",   gravity: "center", crop: "100x75+0+0" },
    thumb:    { resize: "200x150^",  gravity: "center", crop: "200x150+0+0" },
    icon:     { resize: "28x28^",    gravity: "center", crop: "28x28+0+0" }
  }.freeze

  # temporary place to store attachment
  attr_accessor :file

  has_one_attached :attachment

  # -- Relationships -----------------------------------------------------------
  belongs_to :site

  # -- Callbacks ---------------------------------------------------------------
  before_validation :assign_label, on: :create
  before_create :assign_position
  after_save :process_attachment

  # -- Validations -------------------------------------------------------------
  validates :label, presence: true
  validates :file, presence: true, on: :create

  # -- Scopes ------------------------------------------------------------------
  # When we need to grab only files with image attachments.
  # Don't forget to include `with_attached_attachment` before calling this
  scope :with_images, -> {
    where("active_storage_blobs.content_type LIKE 'image/%'").references(:blob)
  }

protected

  def assign_position
    max = Comfy::Cms::File.maximum(:position)
    self.position = max ? max + 1 : 0
  end

  # TODO: Change db schema not to set blank string
  def assign_label
    return if label.present?
    self.label = file&.original_filename
  end

  def process_attachment
    return if @file.blank?
    attachment.attach(@file)
  end

end

Version data entries

11 entries across 11 versions & 6 rubygems

Version Path
PixelForce_ETS-0.0.2 app/models/comfy/cms/file.rb
PixelForce_ETS-0.0.1 app/models/comfy/cms/file.rb
kcyEtsTest2-0.0.4 app/models/comfy/cms/file.rb
kcyEtsTest2-0.0.3 app/models/comfy/cms/file.rb
kcyEtsTest2-0.0.2 app/models/comfy/cms/file.rb
kcyEtsTest2-0.0.1 app/models/comfy/cms/file.rb
kcyEtsTest-0.0.1 app/models/comfy/cms/file.rb
kcyEtsTest-2.2.19 app/models/comfy/cms/file.rb
ComfortableMexicanSofa-2.0.18 app/models/comfy/cms/file.rb
kcy1-1.0 app/models/comfy/cms/file.rb
comfortable_mexican_sofa-2.0.18 app/models/comfy/cms/file.rb