<commit_before>require 'bureaucrat/utils'
require 'bureaucrat/validation'
require 'bureaucrat/widgets'
require 'bureaucrat/forms'

# TODO: needs more testing
module Bureaucrat
  module Formsets
    TOTAL_FORM_COUNT = :'TOTAL_FORMS'
    INITIAL_FORM_COUNT = :'INITIAL_FORMS'
    ORDERING_FIELD_NAME = :'ORDER'
    DELETION_FIELD_NAME = :'DELETE'

    class ManagementForm < Forms::Form
      include Fields
      include Widgets

      field TOTAL_FORM_COUNT, IntegerField.new(:widget => HiddenInput)
      field INITIAL_FORM_COUNT, IntegerField.new(:widget => HiddenInput)
    end

    class BaseFormSet
      include Utils
      include Validation
      include Fields
      include Forms

      def self.default_prefix
        'form'
      end

      attr_accessor :forms, :extra, :can_order, :can_delete, :max_num

      def initialize(data=nil, options={})
        @is_bound = !data.nil?
        @prefix = options.fetch(:prefix, self.class.default_prefix)
        @auto_id = options.fetch(:auto_id, 'id_%s')
        @data = data
        @files = options[:files]
        @initial = options[:initial]
        @error_class = options.fetch(:error_class, ErrorList)
        @errors = nil
        @non_form_errors = nil

        construct_forms
      end

      def to_s
        as_table
      end

      def management_form
        if @data || @files
          form = ManagementForm.new(@data, :auto_id => @auto_id,
                                    :prefix => @prefix)
          raise ValidationError.new('ManagementForm data is missing or has been tampered with') unless
            form.valid?
        else
          form = ManagementForm.new(nil, :auto_id => @auto_id,
                                    :prefix => @prefix,
                                    :initial => {
                                      TOTAL_FORM_COUNT => total_form_count,
                                      INITIAL_FORM_COUNT => initial_form_count
                                    })
        end
        form
      end

      def total_form_count
        if @data || @files
          management_form.cleaned_data[TOTAL_FORM_COUNT]
        else
          n = initial_form_count + self.extra
          (n > self.max_num && self.max_num > 0) ? self.max_num : n
        end
      end

      def initial_form_count
        if @data || @files
          management_form.cleaned_data[INITIAL_FORM_COUNT]
        else
          n = @initial ? @initial.length : 0<commit_msg>Don't include Validations module on base formset (not needed). Raise the correct type of exception on management_form failure.<commit_after>require 'bureaucrat/utils'
require 'bureaucrat/validation'
require 'bureaucrat/widgets'
require 'bureaucrat/forms'

# TODO: needs more testing
module Bureaucrat
  module Formsets
    TOTAL_FORM_COUNT = :'TOTAL_FORMS'
    INITIAL_FORM_COUNT = :'INITIAL_FORMS'
    ORDERING_FIELD_NAME = :'ORDER'
    DELETION_FIELD_NAME = :'DELETE'

    class ManagementForm < Forms::Form
      include Fields
      include Widgets

      field TOTAL_FORM_COUNT, IntegerField.new(:widget => HiddenInput)
      field INITIAL_FORM_COUNT, IntegerField.new(:widget => HiddenInput)
    end

    class BaseFormSet
      include Utils
      include Fields
      include Forms

      def self.default_prefix
        'form'
      end

      attr_accessor :forms, :extra, :can_order, :can_delete, :max_num

      def initialize(data=nil, options={})
        @is_bound = !data.nil?
        @prefix = options.fetch(:prefix, self.class.default_prefix)
        @auto_id = options.fetch(:auto_id, 'id_%s')
        @data = data
        @files = options[:files]
        @initial = options[:initial]
        @error_class = options.fetch(:error_class, ErrorList)
        @errors = nil
        @non_form_errors = nil

        construct_forms
      end

      def to_s
        as_table
      end

      def management_form
        if @data || @files
          form = ManagementForm.new(@data, :auto_id => @auto_id,
                                    :prefix => @prefix)
          raise FieldValidationError.new('ManagementForm data is missing or has been tampered with') unless
            form.valid?
        else
          form = ManagementForm.new(nil, :auto_id => @auto_id,
                                    :prefix => @prefix,
                                    :initial => {
                                      TOTAL_FORM_COUNT => total_form_count,
                                      INITIAL_FORM_COUNT => initial_form_count
                                    })
        end
        form
      end

      def total_form_count
        if @data || @files
          management_form.cleaned_data[TOTAL_FORM_COUNT]
        else
          n = initial_form_count + self.extra
          (n > self.max_num && self.max_num > 0) ? self.max_num : n
        end
      end

      def initial_form_count
        if @data || @files
          management_form.cleaned_data[INITIAL_FORM_COUNT]
        else
          n = @initial ? @initial.length : 0
{'fraction_non_alphanumeric': 0.08135118928642815, 'fraction_numerical': 0.0007995202878273036, 'mean_word_length': 1.8791714614499424, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2853581', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1396, 'n_words': 328}