Sha256: fe6a556c5764a1a97834e54b827d8f6093c8a2eb31bf0d1b623847d4ceb5917e

Contents?: true

Size: 1 KB

Versions: 3

Compression:

Stored size: 1 KB

Contents

require 'active_support/concern'
require 'active_storage/attached'

module ActiveStorageSupport
  module SupportForBase64
    extend ActiveSupport::Concern
    class_methods do
      def has_one_base64_attached(name, dependent: :purge_later)
        has_one_attached name, dependent: dependent

        add_helper_method(ActiveStorageSupport::Base64One, name, dependent: dependent)
      end

      def has_many_base64_attached(name, dependent: :purge_later)
        has_many_attached name, dependent: dependent

        add_helper_method(ActiveStorageSupport::Base64Many, name, dependent: dependent)
      end

      def add_helper_method(type, name, dependent:)
        class_eval <<-CODE, __FILE__, __LINE__ + 1
          def #{name}
            @active_storage_attached_#{name} ||=
              #{type}.new("#{name}", self, dependent: #{dependent == :purge_later ? ':purge_later' : 'false'})
          end

          def #{name}=(data)
            #{name}.attach(data)
          end
        CODE
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_storage_base64-0.1.4 lib/active_storage_support/support_for_base64.rb
active_storage_base64-0.1.3 lib/active_storage_support/support_for_base64.rb
active_storage_base64-0.1.2 lib/active_storage_support/support_for_base64.rb