Sha256: 8abf79285d878b42d8d9c3893c7c88deb6a9bfbdd7724123b451ea807c8271e3

Contents?: true

Size: 858 Bytes

Versions: 4

Compression:

Stored size: 858 Bytes

Contents

module Spina
  class Attachment < ApplicationRecord

    has_one :page_part, as: :page_partable
    has_and_belongs_to_many :attachment_collections, join_table: 'spina_attachment_collections_attachments'

    attr_accessor :_destroy

    scope :sorted, -> { order('created_at DESC') }
    scope :file_attached, -> { where('file IS NOT NULL') }

    mount_uploader :file, FileUploader

    def name
      file.file.try(:filename)
    end

    alias_method :old_update_attributes, :update_attributes
    def update_attributes(attributes)
      if attributes["_destroy"] == "1" && attributes["file"].blank?
        self.page_part.destroy
      else
        old_update_attributes(attributes)
      end
    end

    def self.order_by_ids(ids)
      sql = sanitize_sql_for_assignment({id: ids})
      order("CASE WHEN #{sql} THEN 0 ELSE 1 END")
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spina-0.12.0 app/models/spina/attachment.rb
spina-0.11.1 app/models/spina/attachment.rb
spina-0.11.0 app/models/spina/attachment.rb
spina-0.10.0 app/models/spina/attachment.rb