Sha256: 64ec521d25ddc1c97d6f63bfae300b381147badc50bede987a58fb85d589a32a

Contents?: true

Size: 1.52 KB

Versions: 16

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

class Comfy::Cms::Fragment < ActiveRecord::Base

  self.table_name = "comfy_cms_fragments"

  has_many_attached :attachments

  serialize :content

  attr_reader :files

  # -- Callbacks ---------------------------------------------------------------
  after_save  :remove_attachments,
              :add_attachments

  # -- Relationships -----------------------------------------------------------
  belongs_to :record, polymorphic: true, touch: true

  # -- Validations -------------------------------------------------------------
  validates :identifier,
    presence:   true,
    uniqueness: { scope: :record }

  # -- Instance Methods --------------------------------------------------------

  # Temporary accessor for uploaded files. We can only attach to persisted
  # records so we are deffering it to the after_save callback.
  # Note: hijacking dirty tracking to force trigger callbacks later.
  def files=(files)
    @files = [files].flatten.compact
    content_will_change! if @files.present?
  end

  def file_ids_destroy=(ids)
    @file_ids_destroy = [ids].flatten.compact
    content_will_change! if @file_ids_destroy.present?
  end

protected

  def remove_attachments
    return unless @file_ids_destroy.present?
    attachments.where(id: @file_ids_destroy).destroy_all
  end

  def add_attachments
    return if @files.blank?

    # If we're dealing with a single file
    if tag == "file"
      @files = [@files.first]
      attachments&.purge_later
    end

    attachments.attach(@files)
  end

end

Version data entries

16 entries across 16 versions & 6 rubygems

Version Path
PixelForce_ETS-0.0.2 app/models/comfy/cms/fragment.rb
PixelForce_ETS-0.0.1 app/models/comfy/cms/fragment.rb
kcyEtsTest2-0.0.4 app/models/comfy/cms/fragment.rb
kcyEtsTest2-0.0.3 app/models/comfy/cms/fragment.rb
kcyEtsTest2-0.0.2 app/models/comfy/cms/fragment.rb
kcyEtsTest2-0.0.1 app/models/comfy/cms/fragment.rb
kcyEtsTest-0.0.1 app/models/comfy/cms/fragment.rb
kcyEtsTest-2.2.19 app/models/comfy/cms/fragment.rb
ComfortableMexicanSofa-2.0.18 app/models/comfy/cms/fragment.rb
kcy1-1.0 app/models/comfy/cms/fragment.rb
comfortable_mexican_sofa-2.0.18 app/models/comfy/cms/fragment.rb
comfortable_mexican_sofa-2.0.17 app/models/comfy/cms/fragment.rb
comfortable_mexican_sofa-2.0.16 app/models/comfy/cms/fragment.rb
comfortable_mexican_sofa-2.0.15 app/models/comfy/cms/fragment.rb
comfortable_mexican_sofa-2.0.14 app/models/comfy/cms/fragment.rb
comfortable_mexican_sofa-2.0.13 app/models/comfy/cms/fragment.rb