Sha256: 0ce1b30781b12040e36460c48e614bbbc20f539b1c0382fd795a3b13c80127a5

Contents?: true

Size: 976 Bytes

Versions: 4

Compression:

Stored size: 976 Bytes

Contents

# frozen_string_literal: true

module PagesCore
  module PageModel
    module Attachments
      extend ActiveSupport::Concern

      included do
        has_many :page_files,
                 -> { order("position ASC") },
                 class_name: "PageFile",
                 dependent: :destroy,
                 inverse_of: :page

        has_many :attachments, through: :page_files

        accepts_nested_attributes_for(
          :page_files,
          reject_if: proc { |a| a["attachment_id"].blank? },
          allow_destroy: true
        )
      end

      def attachments
        super.in_locale(locale)
      end

      def attachments?
        attachments.any?
      end

      def page_files
        super.in_locale(locale)
      end

      def page_files_attributes=(attrs)
        ids = page_files.map(&:id)
        super(attrs.reject { |a| a["_destroy"] && ids.exclude?(a["id"]) })
      end

      def files
        page_files
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pages_core-3.15.5 app/models/concerns/pages_core/page_model/attachments.rb
pages_core-3.15.4 app/models/concerns/pages_core/page_model/attachments.rb
pages_core-3.15.3 app/models/concerns/pages_core/page_model/attachments.rb
pages_core-3.15.2 app/models/concerns/pages_core/page_model/attachments.rb