<commit_before>require 'active_support/core_ext/string/inflections'
require 'rails_admin/generic_support'

module RailsAdmin
  class AbstractModel
    cattr_accessor :all_models, :all_abstract_models
    @@all_models = nil
    @@all_abstract_models = nil
    # Returns all models for a given Rails app

    # self.all_abstract_models
    def self.all
      @@all_abstract_models ||= all_models.map{ |model| new(model) }
    end

    def self.all_models
      return @@all_models if @@all_models
      
      raise Exception.new('Nested call to AbstractModel.all_models, this should NEVER happen. Please post stack to github') if @semaphore
      
      @semaphore = true
      
      if RailsAdmin::Config.included_models.any?
        # Whitelist approach, use only models explicitly listed
        possible_models = RailsAdmin::Config.included_models.map(&:to_s)
      else
        filenames = Dir.glob(Rails.application.paths["app/models"].map { |path| File.join(Rails.root, path, "**/*.rb") })
        Rails::Application::Railties.engines.each do |engine|
          engine.paths['app/models'].each do |path|
            filenames += Dir.glob(engine.root.join(path, "**/*.rb"))
          end
        end
    
        class_names = []
        filenames.each do |filename|
          class_names += File.read(filename).scan(/class ([\w\d_\-:]+)/).flatten
        end
        possible_models = class_names
      end

      excluded_models = RailsAdmin::Config.excluded_models.map(&:to_s)
      excluded_models << ['History']
      
      models = (possible_models - excluded_models).uniq
      models.sort!{|x, y| x.to_s <=> y.to_s}
      @@all_models = models.map{|model| lookup model }.compact
      @semaphore = false
      @@all_models
    end

    # Given a string +model_name+, finds the corresponding model class
    def self.lookup(model_name)
      (model = model_name.constantize rescue nil) && model.is_a?(Class) && (superclasses(model).include?(ActiveRecord::Base) ? model : nil)
    end

    def initialize(model)
      model = self.class.lookup(model.to_s.camelize) unless model.is_a?(Class)
      @model_name = model.name
      self.extend(GenericSupport)
      ### TODO more ORMs support
      require 'rails_admin/adapters/active_record'
      self.extend(RailsAdmin::Adapters::ActiveRecord)
    end

    def model
      @model_name.constantize
    end

    private

    def self.superclasses(klass)
      superclasses = []
      while klass
        superclasses << klass.superclass if klass && klass.superclass
        klass = klass.superclass
      end
      superclasses
    end
  end
end
<commit_msg>refs #696<commit_after>require 'active_support/core_ext/string/inflections'
require 'rails_admin/generic_support'

module RailsAdmin
  class AbstractModel
    cattr_accessor :all_models, :all_abstract_models
    @@all_models = nil
    @@all_abstract_models = nil
    # Returns all models for a given Rails app

    # self.all_abstract_models
    def self.all
      @@all_abstract_models ||= all_models.map{ |model| new(model) }
    end

    def self.all_models
      return @@all_models if @@all_models
      
      raise Exception.new('Nested call to AbstractModel.all_models, this should NEVER happen. Please post stack to github') if @semaphore
      
      @semaphore = true
      
      if RailsAdmin::Config.included_models.any?
        # Whitelist approach, use only models explicitly listed
        possible_models = RailsAdmin::Config.included_models.map(&:to_s)
      else
        filenames = Dir.glob(Rails.application.paths["app/models"].map { |path| File.join(Rails.root, path, "**/*.rb") })
        Rails::Application::Railties.engines.each do |engine|
          engine.paths['app/models'].each do |path|
            filenames += Dir.glob(engine.root.join(path, "**/*.rb"))
          end
        end
    
        class_names = []
        filenames.each do |filename|
          class_names += File.read(filename).scan(/class ([\w\d_\-:]+)/).flatten
        end
        possible_models = class_names
      end

      excluded_models = RailsAdmin::Config.excluded_models.map(&:to_s)
      excluded_models << ['History']
      
      models = (possible_models - excluded_models).uniq
      models.sort!{|x, y| x.to_s <=> y.to_s}
      @@all_models = models.map{|model| lookup model }.compact
      @semaphore = false
      @@all_models
    end

    # Given a string +model_name+, finds the corresponding model class
    def self.lookup(model_name)
      (model = model_name.constantize rescue nil) && model.is_a?(Class) && (superclasses(model).include?(ActiveRecord::Base) ? model : nil) || nil
    end

    def initialize(model)
      model = self.class.lookup(model.to_s.camelize) unless model.is_a?(Class)
      @model_name = model.name
      self.extend(GenericSupport)
      ### TODO more ORMs support
      require 'rails_admin/adapters/active_record'
      self.extend(RailsAdmin::Adapters::ActiveRecord)
    end

    def model
      @model_name.constantize
    end

    private

    def self.superclasses(klass)
      superclasses = []
      while klass
        superclasses << klass.superclass if klass && klass.superclass
        klass = klass.superclass
      end
      superclasses
    end
  end
end

{'fraction_non_alphanumeric': 0.10894867391719137, 'fraction_numerical': 0.0005724098454493417, 'mean_word_length': 2.776657060518732, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, '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': '2866428', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1592, 'n_words': 350}