Sha256: d6a3c2403c1f2d19ffd9e9f6e96c74260126339f28b4b1c10c463a1f434807f8

Contents?: true

Size: 1.39 KB

Versions: 13

Compression:

Stored size: 1.39 KB

Contents

module Formstrap
  module Blob
    extend ActiveSupport::Concern

    included do
      class << self
        def search(query)
          where("active_storage_blobs.filename LIKE ?", "%#{query}%")
        end

        def not_attached_to_variant
          not_attached_to("ActiveStorage::VariantRecord")
        end

        def not_attached_to(arg)
          case arg
          when String
            record_types = [arg]
          when Class
            record_types = [arg.to_s]
          when Array
            record_types = arg.map(&:to_s)
          end

          left_outer_joins(:attachments)
            .where.not(active_storage_attachments: {record_type: record_types})
            .or(is_orphan)
        end

        def by_mimetypes_string(mimetype_string)
          return where({}) if mimetype_string.blank?
          by_mimetypes(mimetype_string.split(","))
        end

        private

        def is_orphan
          left_outer_joins(:attachments)
            .where(active_storage_attachments: {id: nil})
        end

        def by_mimetypes(mimetypes = [])
          results = self

          mimetypes.map.with_index do |mimetype, index|
            content_type = mimetype.tr("*", "%")
            query = where(arel_table[:content_type].matches(content_type))
            results = (index == 0) ? query : results.or(query)
          end

          results
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
formstrap-0.4.6 app/models/concerns/formstrap/blob.rb
formstrap-0.4.5 app/models/concerns/formstrap/blob.rb
formstrap-0.4.4 app/models/concerns/formstrap/blob.rb
formstrap-0.4.3 app/models/concerns/formstrap/blob.rb
formstrap-0.4.2 app/models/concerns/formstrap/blob.rb
formstrap-0.3.5 app/models/concerns/formstrap/blob.rb
formstrap-0.3.4 app/models/concerns/formstrap/blob.rb
formstrap-0.3.3 app/models/concerns/formstrap/blob.rb
formstrap-0.3.2 app/models/concerns/formstrap/blob.rb
formstrap-0.3.1 app/models/concerns/formstrap/blob.rb
formstrap-0.3.0 app/models/concerns/formstrap/blob.rb
formstrap-0.2.1 app/models/concerns/formstrap/blob.rb
formstrap-0.2.0 app/models/concerns/formstrap/blob.rb