Class: DiscourseDev::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/discourse_dev/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/discourse_dev/config.rb', line 9

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

  if File.exists?(file_path)
    @config = YAML.load_file(file_path)
  else
    puts "I did no detect a custom `config/dev.yml` file, creating one for you where you can amend defaults."
    FileUtils.cp(default_file_path, file_path)
    @config = {}
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



84
85
86
87
# File 'lib/discourse_dev/config.rb', line 84

def method_missing(name)
  name = name.to_s
  config[name] || default_config[name]
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/discourse_dev/config.rb', line 7

def config
  @config
end

#default_configObject (readonly)

Returns the value of attribute default_config.



7
8
9
# File 'lib/discourse_dev/config.rb', line 7

def default_config
  @default_config
end

Instance Method Details

#create_admin_userObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/discourse_dev/config.rb', line 51

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

#set_seedObject



75
76
77
78
# File 'lib/discourse_dev/config.rb', line 75

def set_seed
  seed = self.seed || 1
  Faker::Config.random = Random.new(seed)
end

#start_dateObject



80
81
82
# File 'lib/discourse_dev/config.rb', line 80

def start_date
  DateTime.parse(config["start_date"] || default_config["start_date"])
end

#update!Object



23
24
25
26
27
# File 'lib/discourse_dev/config.rb', line 23

def update!
  update_site_settings
  create_admin_user
  set_seed
end

#update_site_settingsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/discourse_dev/config.rb', line 29

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