<commit_before>class AdminDataController  < ApplicationController
  
  unloadable
  
  before_filter :secure_it
  before_filter :admin_data_ensure_update_allowed, :only => [:destroy, :delete, :edit]


  def migration_information
    @data = ActiveRecord::Base.connection.select_all('select * from schema_migrations');
    render :file =>   "#{RAILS_ROOT}/vendor/plugins/admin_data/lib/views/migration_information.html.erb"        
  end
  
  def table_structure

    @klass = Object.const_get(params[:klass])
    
    @types = ActiveRecord::Base.connection.native_database_types    

    if ActiveRecord::Base.connection.respond_to?(:pk_and_sequence_for)
      pk, pk_seq = ActiveRecord::Base.connection.pk_and_sequence_for(@klass.table_name)
    end
    pk ||= 'id'

    column_specs = @klass.columns.map do |column|
      raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" if @types[column.type].nil?
      next if column.name == pk
      spec = {}
      spec[:name]      = column.name.inspect
      spec[:type]      = column.type.to_s
      spec[:limit]     = column.limit.inspect if column.limit != @types[column.type][:limit] && column.type != :decimal
      spec[:precision] = column.precision.inspect if !column.precision.nil?
      spec[:scale]     = column.scale.inspect if !column.scale.nil?
      spec[:null]      = 'false' if !column.null
      spec[:default]   = default_string(column.default) if column.has_default?
      (spec.keys - [:name, :type]).each{ |k| spec[k].insert(0, "#{k.inspect} => ")}
      spec
    end.compact


    # find all migration keys used in this table
    keys = [:name, :limit, :precision, :scale, :default, :null] & column_specs.map(&:keys).flatten

    # figure out the lengths for each column based on above keys
    lengths = keys.map{ |key| column_specs.map{ |spec| spec[key] ? spec[key].length + 2 : 0 }.max }

    # the string we're going to sprintf our values against, with standardized column widths
    format_string = lengths.map{ |len| "%-#{len}s" }

    # find the max length for the 'type' column, which is special
    type_length = column_specs.map{ |column| column[:type].length }.max

    # add column type definition to our format string
    format_string.unshift "    %-#{type_length}s "

    format_string *= ''

    @records = []
    column_specs.each do |colspec|
      values = keys.zip(lengths).map{ |key, len| colspec.key?(key) ? colspec[key] + ", " : " " * len }
      values.unshift colspec[:type]
      @records << ((format_string % values).gsub(/,\s*$/, ''))
    end
    
    if (indexes = ActiveRecord::Base.connection.indexes(@klass.table_name)).any?
      add_index_statements = indexes.map do |index|
        statment_parts = [ ('add_index ' + index.table.inspect) ]
        statment_parts << index.columns.inspect
        statment_parts << (':name => ' + index.name.inspect)
        statment_parts << ':unique => true' if index.unique

        '  ' + statment_parts.join(', ')
      end
      @records << ""
      add_index_statements.sort.each { |index| @records << index }
    end  
    
    render :file =>   "#{RAILS_ROOT}/vendor/plugins/admin_data/lib/views/table_structure.html.erb"        
  end

  def quick_search
    session[:admin_data_search_type] = 'quick'          
    @klass = Object.const_get(params[:klass])
    
    params[:query] = params[:query].strip
    
    if params[:query].blank?
      @records = @klass.send( :paginate,
                              :page => params[:page],
                              :order => "#{@klass.table_name}.id desc",
                              :per_page => 25)            
    else
      @records = @klass.paginate( :page => params[:page],
                                  :per_page => 25,
                                  :order => "#{@klass.table_name}.id desc",
                                  :conditions => build_quick_search_conditions(@klass,params[:query]))
    end
    
    render :file =>   "#{RAILS_ROOT}/vendor/plugins/admin_data/lib/views/list.html.erb"    
  end
  
  
  def advance_search
    session[:admin_data_search_type] = 'advance'    
    @klass = Object.const_get(params[:klass])
    
    if !params[:adv_search].blank?
      @records = @klass.paginate( :page => params[:page],
                                  :per_page => 25,
                                  :order => "#{@klass.table_name}.id desc",
                                  :conditions => build_advance_search_conditions(@klass,params[:adv_search]))
    else
      @records = @klass.send( :paginate,
                              :page => params[:page],
                              :per_page => 25,
                              :order => "#{@klass.table_name}.id desc")      
    end<commit_msg>fixing the edge case when no klass or wrong klass is supplied in advance search page<commit_after>class AdminDataController  < ApplicationController
  
  unloadable
  
  before_filter :secure_it
  before_filter :admin_data_ensure_update_allowed, :only => [:destroy, :delete, :edit]


  def migration_information
    @data = ActiveRecord::Base.connection.select_all('select * from schema_migrations');
    render :file =>   "#{RAILS_ROOT}/vendor/plugins/admin_data/lib/views/migration_information.html.erb"        
  end
  
  def table_structure

    @klass = Object.const_get(params[:klass])
    
    @types = ActiveRecord::Base.connection.native_database_types    

    if ActiveRecord::Base.connection.respond_to?(:pk_and_sequence_for)
      pk, pk_seq = ActiveRecord::Base.connection.pk_and_sequence_for(@klass.table_name)
    end
    pk ||= 'id'

    column_specs = @klass.columns.map do |column|
      raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" if @types[column.type].nil?
      next if column.name == pk
      spec = {}
      spec[:name]      = column.name.inspect
      spec[:type]      = column.type.to_s
      spec[:limit]     = column.limit.inspect if column.limit != @types[column.type][:limit] && column.type != :decimal
      spec[:precision] = column.precision.inspect if !column.precision.nil?
      spec[:scale]     = column.scale.inspect if !column.scale.nil?
      spec[:null]      = 'false' if !column.null
      spec[:default]   = default_string(column.default) if column.has_default?
      (spec.keys - [:name, :type]).each{ |k| spec[k].insert(0, "#{k.inspect} => ")}
      spec
    end.compact


    # find all migration keys used in this table
    keys = [:name, :limit, :precision, :scale, :default, :null] & column_specs.map(&:keys).flatten

    # figure out the lengths for each column based on above keys
    lengths = keys.map{ |key| column_specs.map{ |spec| spec[key] ? spec[key].length + 2 : 0 }.max }

    # the string we're going to sprintf our values against, with standardized column widths
    format_string = lengths.map{ |len| "%-#{len}s" }

    # find the max length for the 'type' column, which is special
    type_length = column_specs.map{ |column| column[:type].length }.max

    # add column type definition to our format string
    format_string.unshift "    %-#{type_length}s "

    format_string *= ''

    @records = []
    column_specs.each do |colspec|
      values = keys.zip(lengths).map{ |key, len| colspec.key?(key) ? colspec[key] + ", " : " " * len }
      values.unshift colspec[:type]
      @records << ((format_string % values).gsub(/,\s*$/, ''))
    end
    
    if (indexes = ActiveRecord::Base.connection.indexes(@klass.table_name)).any?
      add_index_statements = indexes.map do |index|
        statment_parts = [ ('add_index ' + index.table.inspect) ]
        statment_parts << index.columns.inspect
        statment_parts << (':name => ' + index.name.inspect)
        statment_parts << ':unique => true' if index.unique

        '  ' + statment_parts.join(', ')
      end
      @records << ""
      add_index_statements.sort.each { |index| @records << index }
    end  
    
    render :file =>   "#{RAILS_ROOT}/vendor/plugins/admin_data/lib/views/table_structure.html.erb"        
  end

  def quick_search
    session[:admin_data_search_type] = 'quick'          
    @klass = Object.const_get(params[:klass])
    
    params[:query] = params[:query].strip
    
    if params[:query].blank?
      @records = @klass.send( :paginate,
                              :page => params[:page],
                              :order => "#{@klass.table_name}.id desc",
                              :per_page => 25)            
    else
      @records = @klass.paginate( :page => params[:page],
                                  :per_page => 25,
                                  :order => "#{@klass.table_name}.id desc",
                                  :conditions => build_quick_search_conditions(@klass,params[:query]))
    end
    
    render :file =>   "#{RAILS_ROOT}/vendor/plugins/admin_data/lib/views/list.html.erb"    
  end
  
  
  def advance_search
    session[:admin_data_search_type] = 'advance'    
    begin
      @klass = Object.const_get(params[:klass])
    rescue TypeError => e # in case no params[:klass] is supplied
      redirect_to admin_data_path and return
    rescue NameError => e # in case wrong params[:klass] is supplied
      redirect_to admin_data_path and return
    end

    
    if !params[:adv_search].blank?
      @records = @klass.paginate( :page => params[:page],
                                  :per_page => 25,
                                  :order => "#{@klass.table_name}.id desc",
                                  :conditions => build_advance_search_conditions(@klass,params[:adv_search]))
    else
      @records = @klass.send( :paginate,
                              :page => params[:page],
                              :per_page => 25,
                              :order => "#{@klass.table_name}.id desc")      
    end
{'fraction_non_alphanumeric': 0.13319776309100154, 'fraction_numerical': 0.0022369089984748346, 'mean_word_length': 2.381230663458233, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 47, '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': '7122076', 'n_tokens_mistral': 3032, 'n_tokens_neox': 3000, 'n_words': 618}