Sha256: 1f9940c302fb82f27b2036592516a46207ec96a1b39c48cceab02c1e37269a5c

Contents?: true

Size: 1.45 KB

Versions: 11

Compression:

Stored size: 1.45 KB

Contents

module Para
  module Library
    class File < Para::ApplicationRecord
      if defined?(ActiveStorage)
        has_one_attached :attachment

        # Return attachment.path or attachment.url depending on the storage
        # backend, allowing 'openuri' and 'roo' libraries to load easily the
        # file at the right path, on filesystem or othe storage systems, like S3.
        #
        def attachment_path
          return unless attachment.attached?

          attachment.url
        end

        alias attachment_url attachment_path

        def attachment_ext
          ::File.extname(attachment.filename.to_s) if attachment.attached?
        end
      else
        has_attached_file :attachment

        validates :attachment, presence: true
        do_not_validate_attachment_file_type :attachment

        # Return attachment.path or attachment.url depending on the storage
        # backend, allowing 'openuri' and 'roo' libraries to load easily the
        # file at the right path, on filesystem or othe storage systems, like S3.
        #
        def attachment_path
          return unless attachment?

          case attachment.options[:storage]
          when :filesystem then attachment.path
          else attachment.url
          end
        end

        def attachment_url
          attachment.url if attachment?
        end

        def attachment_ext
          ::File.extname(attachment_file_name) if attachment.attached?
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
para-0.12.4 app/models/para/library/file.rb
para-0.12.3 app/models/para/library/file.rb
para-0.12.2 app/models/para/library/file.rb
para-0.12.1 app/models/para/library/file.rb
para-0.12.0 app/models/para/library/file.rb
para-0.11.4 app/models/para/library/file.rb
para-0.11.3 app/models/para/library/file.rb
para-0.11.2 app/models/para/library/file.rb
para-0.11.1 app/models/para/library/file.rb
para-0.11.0 app/models/para/library/file.rb
para-0.10.0 app/models/para/library/file.rb