<commit_before>module SS::Relation::File
  extend ActiveSupport::Concern
  extend SS::Translation

  module ClassMethods
    def belongs_to_file(name, opts = {})
      store = opts[:store_as] || "#{name.to_s.singularize}_id"
      class_name = opts[:class_name] || "SS::File"

      belongs_to name, foreign_key: store, class_name: class_name, dependent: :destroy

      attr_accessor "in_#{name}", "rm_#{name}", "in_#{name}_resizing"
      permit_params "in_#{name}", "rm_#{name}"
      permit_params "in_#{name}_resizing" => []

      before_save "validate_relation_#{name}", if: ->{ send("in_#{name}").present? }
      before_save "save_relation_#{name}", if: ->{ send("in_#{name}").present? }
      before_save "remove_relation_#{name}", if: ->{ send("rm_#{name}").to_s == "1" }
      after_save "update_relation_#{name}_state", if: ->{ send(name).present? }

      define_method("validate_relation_#{name}") do
        file = relation_file(name, opts)
        return true if file.valid?

        file.errors.full_messages.each do |msg|
          errors.add :base, msg
        end
        false
      end

      define_method("save_relation_#{name}") do
        file = relation_file(name, opts)
        file.save
        send("#{store}=", file.id)
      end

      define_method("remove_relation_#{name}") do
        file = send(name)
        file.destroy if file
        unset(store) rescue nil
      end

      define_method("#{name}_file_state") do
        opts[:static_state].presence || try(:state).presence || 'public'
      end

      define_method("update_relation_#{name}_state") do
        return unless respond_to?(:state)
        file = send(name)
        file_state = send("#{name}_file_state")
        file.update_attributes(state: file_state) if file.state != file_state
      end

      define_method("generate_relation_public_#{name}") do
        file = send(name)
        file.generate_public_file if file
      end

      define_method("remove_relation_public_#{name}") do
        file = send(name)
        file.remove_public_file if file<commit_msg>[fix] unable to delete file on some cases<commit_after>module SS::Relation::File
  extend ActiveSupport::Concern
  extend SS::Translation

  module ClassMethods
    def belongs_to_file(name, opts = {})
      store = opts[:store_as] || "#{name.to_s.singularize}_id"
      class_name = opts[:class_name] || "SS::File"

      belongs_to name, foreign_key: store, class_name: class_name, dependent: :destroy

      attr_accessor "in_#{name}", "rm_#{name}", "in_#{name}_resizing"
      permit_params "in_#{name}", "rm_#{name}"
      permit_params "in_#{name}_resizing" => []

      before_save "validate_relation_#{name}", if: ->{ send("in_#{name}").present? }
      before_save "save_relation_#{name}", if: ->{ send("in_#{name}").present? }
      before_save "remove_relation_#{name}", if: ->{ send("rm_#{name}").to_s == "1" }
      after_save "update_relation_#{name}_state", if: ->{ send(name).present? }

      define_method("validate_relation_#{name}") do
        file = relation_file(name, opts)
        return true if file.valid?

        file.errors.full_messages.each do |msg|
          errors.add :base, msg
        end
        false
      end

      define_method("save_relation_#{name}") do
        file = relation_file(name, opts)
        file.save
        send("#{store}=", file.id)
      end

      define_method("remove_relation_#{name}") do
        file = send(name)
        file.destroy if file
        unset(store) rescue nil
        send("#{store}=", nil)
      end

      define_method("#{name}_file_state") do
        opts[:static_state].presence || try(:state).presence || 'public'
      end

      define_method("update_relation_#{name}_state") do
        return unless respond_to?(:state)
        file = send(name)
        file_state = send("#{name}_file_state")
        file.update_attributes(state: file_state) if file.state != file_state
      end

      define_method("generate_relation_public_#{name}") do
        file = send(name)
        file.generate_public_file if file
      end

      define_method("remove_relation_public_#{name}") do
        file = send(name)
        file.remove_public_file if file
{'fraction_non_alphanumeric': 0.14054696789536267, 'fraction_numerical': 0.0004756242568370987, 'mean_word_length': 3.0914396887159534, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '5310729', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1339, 'n_words': 258}