<commit_before>module AttachmentHelper
  def attachment_url(record, name, *args, filename: nil, format: nil)
    file = record.send(name)

    filename ||= name.to_s

    backend_name = Defile.backends.key(file.backend)
    host = Defile.host || root_url

    File.join(host, defile_app_path, backend_name, *args.map(&:to_s), file.id, filename.parameterize("_"))
  end

  def attachment_image_tag(record, name, *args, fallback: nil, format: nil, **options)
    file = record.send(name)

    if file
      image_tag(attachment_url(record, name, *args, format: format), options)
    elsif fallback
      image_tag(fallback, options)
    end
  end

  def attachment_field(object_name, method, options = {})
    if options[:object]
      cache = options[:object].send(:"#{method}_attachment").cache

      if options[:direct]
        host = Defile.host || root_url
        backend_name = Defile.backends.key(cache)

        options[:data] ||= {}
        options[:data][:direct] = true
        options[:data][:as] = "file"
        options[:data][:url] = File.join(host, defile_app_path, backend_name)
      end

      if options[:presigned] and cache.respond_to?(:presign)
        signature = cache.presign
        options[:data] ||= {}
        options[:data][:direct] = true
        options[:data][:id] = signature.id
        options[:data][:url] = signature.url
        options[:data][:fields] = signature.fields
        options[:data][:as] = signature.as
      end
    end
    hidden_field(object_name, :"#{method}_cache_id", options) +
    file_field(object_name, method, options)
  end
end<commit_msg>hidden field doesn't require other options<commit_after>module AttachmentHelper
  def attachment_url(record, name, *args, filename: nil, format: nil)
    file = record.send(name)

    filename ||= name.to_s

    backend_name = Defile.backends.key(file.backend)
    host = Defile.host || root_url

    File.join(host, defile_app_path, backend_name, *args.map(&:to_s), file.id, filename.parameterize("_"))
  end

  def attachment_image_tag(record, name, *args, fallback: nil, format: nil, **options)
    file = record.send(name)

    if file
      image_tag(attachment_url(record, name, *args, format: format), options)
    elsif fallback
      image_tag(fallback, options)
    end
  end

  def attachment_field(object_name, method, options = {})
    if options[:object]
      cache = options[:object].send(:"#{method}_attachment").cache

      if options[:direct]
        host = Defile.host || root_url
        backend_name = Defile.backends.key(cache)

        options[:data] ||= {}
        options[:data][:direct] = true
        options[:data][:as] = "file"
        options[:data][:url] = File.join(host, defile_app_path, backend_name)
      end

      if options[:presigned] and cache.respond_to?(:presign)
        signature = cache.presign
        options[:data] ||= {}
        options[:data][:direct] = true
        options[:data][:id] = signature.id
        options[:data][:url] = signature.url
        options[:data][:fields] = signature.fields
        options[:data][:as] = signature.as
      end
    end
    hidden_field(object_name, :"#{method}_cache_id", options.slice(:object)) +
    file_field(object_name, method, options)
  end
end
{'fraction_non_alphanumeric': 0.13894023413431916, 'fraction_numerical': 0.0, 'mean_word_length': 3.3467202141900936, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5877751', 'n_tokens_mistral': 1028, 'n_tokens_neox': 1014, 'n_words': 210}