<commit_before>require 'rails/generators'
module Forem
  module Generators
    class InstallGenerator < Rails::Generators::Base
      class_option "user-class", :type => :string
      class_option "no-migrate", :type => :boolean
      class_option "current-user-helper", :type => :string

      source_root File.expand_path("../install/templates", __FILE__)
      desc "Used to install Forem"

      def install_migrations
        puts "Copying over Forem migrations..."
        Dir.chdir(Rails.root) do
          `rake forem:install:migrations`
        end
      end

      def add_forem_admin_migration
        # Is there a cleaner way to do this?
        if options["user-class"]
          @user_class = options["user-class"]
        else
          @user_class = ask("What is your user class called? [User]")
        end

        if @user_class.blank?
          @user_class = 'User'
        else
          @user_class = @user_class
        end

        puts "Adding forem_admin migration..."

        last_migration = Dir[Rails.root + "db/migrate/*.rb"].sort.last.split("/").last
        current_migration_number = /^(\d+)_/.match(last_migration)[1]
        next_migration_number = current_migration_number.to_i + 1
        template "forem_admin_migration.rb", "#{Rails.root}/db/migrate/#{next_migration_number}_add_forem_admin.rb"
      end

      def determine_current_user_helper
        if options["current-user-helper"]
          current_user_helper = options["current-user-helper"]
        else
          current_user_helper = ask("What is the current_user helper called in your app? [current_user]")
        end
        current_user_helper = :current_user if current_user_helper.blank?
        puts "Defining forem_user method inside ApplicationController..."

        forem_user_method = %Q{
  def forem_user
    #{current_user_helper}
  end
  helper_method :forem_user

}

        inject_into_file("#{Rails.root}/app/controllers/application_controller.rb",
                         forem_user_method,
                         :after => "ActionController::Base\n")

      end

      def add_forem_initializer
        path = "#{Rails.root}/config/initializers/forem.rb"
        if File.exists?(path)
          puts "Skipping config/initializers/forem.rb creation, as file already exists!"
        else
          puts "Adding forem initializer (config/initializers/forem.rb)..."
          template "initializer.rb", path
        end
      end

      def run_migrations
        unless options["no-migrate"]
          puts "Running rake db:migrate"
          `rake db:migrate`
        end
      end

      def seed_database
        unless options["no-migrate"]
          puts "Creating default forum and topic"<commit_msg>make sure that forem.rb is loaded when we seed the database so the user_class is initialized<commit_after>require 'rails/generators'
module Forem
  module Generators
    class InstallGenerator < Rails::Generators::Base
      class_option "user-class", :type => :string
      class_option "no-migrate", :type => :boolean
      class_option "current-user-helper", :type => :string

      source_root File.expand_path("../install/templates", __FILE__)
      desc "Used to install Forem"

      def install_migrations
        puts "Copying over Forem migrations..."
        Dir.chdir(Rails.root) do
          `rake forem:install:migrations`
        end
      end

      def add_forem_admin_migration
        # Is there a cleaner way to do this?
        if options["user-class"]
          @user_class = options["user-class"]
        else
          @user_class = ask("What is your user class called? [User]")
        end

        if @user_class.blank?
          @user_class = 'User'
        else
          @user_class = @user_class
        end

        puts "Adding forem_admin migration..."

        last_migration = Dir[Rails.root + "db/migrate/*.rb"].sort.last.split("/").last
        current_migration_number = /^(\d+)_/.match(last_migration)[1]
        next_migration_number = current_migration_number.to_i + 1
        template "forem_admin_migration.rb", "#{Rails.root}/db/migrate/#{next_migration_number}_add_forem_admin.rb"
      end

      def determine_current_user_helper
        if options["current-user-helper"]
          current_user_helper = options["current-user-helper"]
        else
          current_user_helper = ask("What is the current_user helper called in your app? [current_user]")
        end
        current_user_helper = :current_user if current_user_helper.blank?
        puts "Defining forem_user method inside ApplicationController..."

        forem_user_method = %Q{
  def forem_user
    #{current_user_helper}
  end
  helper_method :forem_user

}

        inject_into_file("#{Rails.root}/app/controllers/application_controller.rb",
                         forem_user_method,
                         :after => "ActionController::Base\n")

      end

      def add_forem_initializer
        path = "#{Rails.root}/config/initializers/forem.rb"
        if File.exists?(path)
          puts "Skipping config/initializers/forem.rb creation, as file already exists!"
        else
          puts "Adding forem initializer (config/initializers/forem.rb)..."
          template "initializer.rb", path
        end
      end

      def run_migrations
        unless options["no-migrate"]
          puts "Running rake db:migrate"
          `rake db:migrate`
        end
      end

      def seed_database
        load "#{Rails.root}/config/initializers/forem.rb"
        unless options["no-migrate"]
          puts "Creating default forum and topic"
{'fraction_non_alphanumeric': 0.09705093833780161, 'fraction_numerical': 0.0007149240393208221, 'mean_word_length': 2.387409200968523, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '6528261', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1638, 'n_words': 374}