Sha256: f2b163d58672687a72d1081d7ee7b8de96ebe1f7830232f1fdb661b6425696d2

Contents?: true

Size: 1.72 KB

Versions: 3

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

require 'rails'

module DiscourseDev
  class Config
    attr_reader :config, :default_config

    def initialize
      @default_config = YAML.load_file(File.join(File.expand_path(__dir__), "config.yml"))
      file_path = File.join(Rails.root, "config", "dev.yml")

      if File.exists?(file_path)
        @config = YAML.load_file(file_path)
      else
        @config = {}
      end
    end

    def update!
      update_site_settings
      create_admin_user
      set_seed
    end

    def update_site_settings
      puts "Updating site settings..."

      site_settings = config["site_settings"] || {}

      site_settings.each do |key, value|
        puts "#{key} = #{value}"
        SiteSetting.set(key, value)
      end

      keys = site_settings.keys

      default_config["site_settings"].each do |key, value|
        next if keys.include?(key)

        puts "#{key} = #{value}"
        SiteSetting.set(key, value)
      end

      SiteSetting.refresh!
    end

    def create_admin_user
      puts "Creating default admin user account..."

      settings = config["admin"]

      if settings.present?
        email = settings["email"]

        admin = ::User.create!(
          email: email,
          username: settings["username"] || UserNameSuggester.suggest(email),
          password: settings["password"]
        )
        admin.grant_admin!
        if admin.trust_level < 1
          admin.change_trust_level!(1)
        end
        admin.email_tokens.update_all confirmed: true
        admin.activate
      else
        Rake::Task['admin:create'].invoke
      end
    end

    def set_seed
      seed = config["seed"] || default_config["seed"] || 1
      Faker::Config.random = Random.new(seed)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
discourse_dev-0.0.7 lib/discourse_dev/config.rb
discourse_dev-0.0.5 lib/discourse_dev/config.rb
discourse_dev-0.0.4 lib/discourse_dev/config.rb