lib/echo_uploads/model.rb
def echo_uploads_data=(data)
      parsed = JSON.parse Base64.decode64(data)
      # parsed will look like:
      # { 'attr1' => [ {'id' => 1, 'key' => 'abc...'} ] }
      unless parsed.is_a? Hash
        raise ArgumentError, "Invalid JSON structure in: #{parsed.inspect}"
      end
      parsed.each do |attr, attr_data|
        # If the :map option was passed, there may be multiple variants of the uploaded
        # file. Even if not, attr_data is still a one-element array.
        unless attr_data.is_a? Array
          raise ArgumentError, "Invalid JSON structure in: #{parsed.inspect}"
        end
        attr_data.each do |variant_data|
          unless variant_data.is_a? Hash
            raise ArgumentError, "Invalid JSON structure in: #{parsed.inspect}"
          end
          if meta = ::EchoUploads::File.where(
            id: variant_data['id'], key: variant_data['key'], temporary: true
          ).first
            if send("#{attr}_tmp_metadata").nil?
              send "#{attr}_tmp_metadata=", []
            end
            send("#{attr}_tmp_metadata") << meta
          end
        end
      end
    end
def echo_uploads_data=(data)
      parsed = JSON.parse Base64.decode64(data)
      # parsed will look like:
      # { 'attr1' => [ {'id' => 1, 'key' => 'abc...'} ] }
      unless parsed.is_a? Hash
        raise ArgumentError, "Invalid JSON structure in: #{parsed.inspect}"
      end
      parsed.each do |attr, attr_data|
        # If the :map option was passed, there may be multiple variants of the uploaded
        # file. Even if not, attr_data is still a one-element array.
        unless attr_data.is_a? Array
          raise ArgumentError, "Invalid JSON structure in: #{parsed.inspect}"
        end
        attr_data.each do |variant_data|
          unless variant_data.is_a? Hash
            raise ArgumentError, "Invalid JSON structure in: #{parsed.inspect}"
          end
          if meta = ::EchoUploads::File.where(
            id: variant_data['id'], key: variant_data['key'], temporary: true
          ).first
            if send("#{attr}_tmp_metadata").nil?
              send "#{attr}_tmp_metadata=", []
            end
            send("#{attr}_tmp_metadata") << meta
          end
        end
      end
    end
Pass in a hash that's been encoded as JSON and then Base64.