<commit_before>module Admin
  module FilePreviewHelper

    def get_type_of_attachment(attachment)
      if defined?(Paperclip) && attachment.is_a?(Paperclip::Attachment)
        :paperclip
      elsif defined?(Dragonfly) && attachment.is_a?(Dragonfly::ActiveModelExtensions::Attachment)
        :dragonfly
      end
    end

    def link_to_detach_attribute(attribute)
      validators = @item.class.validators.delete_if { |i| i.class != ActiveModel::Validations::PresenceValidator }.map { |i| i.attributes }.flatten.map { |i| i.to_s }

      attachment = @item.send(attribute)

      # We are here and we already know we are handling attachments, but
      # `dragonfly` and `paperclip` have different behaviors.

      present = case attachment
                when Paperclip::Attachment
                  attachment.exists?
                else
                  attachment.present?
                end

      return unless present

      field = case get_type_of_attachment(attachment)
              when :dragonfly then attribute
              when :paperclip then "#{attribute}_file_name"
              end

      if !validators.include?(field) && attachment
        attribute_i18n = @item.class.human_attribute_name(attribute)
        message = Typus::I18n.t("Remove")
        label_text = <<-HTML
#{attribute_i18n}
<small>#{link_to message, { :action => 'update', :id => @item.id, :attribute => attribute }, :confirm => Typus::I18n.t("Are you sure?")}</small>
        HTML
        label_text.html_safe
      end
    end

    def typus_file_preview(item, attribute, options = {})
      if (attachment = item.send(attribute))
        adapter = get_type_of_attachment(attachment)
        send("typus_file_preview_for_#{adapter}", attachment, options)
      end
    end

    def typus_file_preview_for_dragonfly(attachment, options = {})<commit_msg>Do not use return unless ...<commit_after>module Admin
  module FilePreviewHelper

    def get_type_of_attachment(attachment)
      if defined?(Paperclip) && attachment.is_a?(Paperclip::Attachment)
        :paperclip
      elsif defined?(Dragonfly) && attachment.is_a?(Dragonfly::ActiveModelExtensions::Attachment)
        :dragonfly
      end
    end

    def link_to_detach_attribute(attribute)
      validators = @item.class.validators.delete_if { |i| i.class != ActiveModel::Validations::PresenceValidator }.map { |i| i.attributes }.flatten.map { |i| i.to_s }

      attachment = @item.send(attribute)

      # We are here and we already know we are handling attachments, but
      # `dragonfly` and `paperclip` have different behaviors.

      present = case attachment
                when Paperclip::Attachment
                  attachment.exists?
                else
                  attachment.present?
                end

      field = case get_type_of_attachment(attachment)
              when :dragonfly then attribute
              when :paperclip then "#{attribute}_file_name"
              end

      if present && !validators.include?(field) && attachment
        attribute_i18n = @item.class.human_attribute_name(attribute)
        message = Typus::I18n.t("Remove")
        label_text = <<-HTML
#{attribute_i18n}
<small>#{link_to message, { :action => 'update', :id => @item.id, :attribute => attribute }, :confirm => Typus::I18n.t("Are you sure?")}</small>
        HTML
        label_text.html_safe
      end
    end

    def typus_file_preview(item, attribute, options = {})
      if (attachment = item.send(attribute))
        adapter = get_type_of_attachment(attachment)
        send("typus_file_preview_for_#{adapter}", attachment, options)
      end
    end

    def typus_file_preview_for_dragonfly(attachment, options = {})
{'fraction_non_alphanumeric': 0.1039136302294197, 'fraction_numerical': 0.004318488529014845, 'mean_word_length': 2.7700915564598168, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '1200619', 'n_tokens_mistral': 1070, 'n_tokens_neox': 1052, 'n_words': 235}