Sha256: f2967b30a7fe7808c550e6dca619e24783b1e5311e21ec9890b7012128f43080

Contents?: true

Size: 1.95 KB

Versions: 8

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

class Occams::Cms::File < ActiveRecord::Base
  self.table_name = 'occams_cms_files'

  include Occams::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
  # active_storage attachment behavior changed in rails 6 - see PR#892 for details
  if Rails::VERSION::MAJOR >= 6
    before_save :process_attachment
  else
    after_save :process_attachment
  end

  after_save :clear_page_content_cache

  # -- 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)
  }

private

  def clear_page_content_cache
    Occams::Cms::Page.where(id: site.pages.pluck(:id)).update_all(content_cache: nil)
  end

protected

  def assign_position
    max = Occams::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?

    self.attachment = @file
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
occams-1.0.7.1 app/models/occams/cms/file.rb
occams-1.0.7 app/models/occams/cms/file.rb
occams-1.0.6.1 app/models/occams/cms/file.rb
occams-1.0.6 app/models/occams/cms/file.rb
occams-1.0.5 app/models/occams/cms/file.rb
occams-1.0.4 app/models/occams/cms/file.rb
occams-1.0.3 app/models/occams/cms/file.rb
occams-1.0.2 app/models/occams/cms/file.rb