Class: DiscourseDev::Config
- Inherits:
-
Object
- Object
- DiscourseDev::Config
show all
- Defined in:
- lib/discourse_dev/config.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/discourse_dev/config.rb', line 10
def initialize
default_file_path = File.join(DiscourseDev.root, "config", "dev.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
89
90
91
92
93
|
# File 'lib/discourse_dev/config.rb', line 89
def method_missing(name)
name = name.to_s
return config[name] if config.keys.include?(name)
default_config[name]
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
8
9
10
|
# File 'lib/discourse_dev/config.rb', line 8
def config
@config
end
|
#default_config ⇒ Object
Returns the value of attribute default_config.
8
9
10
|
# File 'lib/discourse_dev/config.rb', line 8
def default_config
@default_config
end
|
#file_path ⇒ Object
Returns the value of attribute file_path.
8
9
10
|
# File 'lib/discourse_dev/config.rb', line 8
def file_path
@file_path
end
|
Instance Method Details
#create_admin_user ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/discourse_dev/config.rb', line 53
def create_admin_user
puts "Creating default admin user account..."
settings = config["admin"]
if settings.present?
create_admin_user_from_settings(settings)
else
create_admin_user_from_input
end
end
|
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/discourse_dev/config.rb', line 111
def create_admin_user_from_input
begin
email = ask("Email: ")
password = ask("Password (optional, press ENTER to skip): ")
username = UserNameSuggester.suggest(email)
admin = ::User.new(
email: email,
username: username
)
if password.present?
admin.password = password
else
puts "Once site is running use https://localhost:9292/user/#{username}/become to access the account in development"
end
admin.name = ask("Full name: ") if SiteSetting.full_name_required
saved = admin.save
if saved
File.open(file_path, 'a') do | file|
file.puts("admin:")
file.puts(" username: #{admin.username}")
file.puts(" email: #{admin.email}")
file.puts(" password: #{password}") if password.present?
end
else
say(admin.errors.full_messages.join("\n"))
end
end while !saved
admin.active = true
admin.save
admin.grant_admin!
if admin.trust_level < 1
admin.change_trust_level!(1)
end
admin.email_tokens.update_all confirmed: true
admin.activate
say("\nAdmin account created successfully with username #{admin.username}")
end
|
#create_admin_user_from_settings(settings) ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/discourse_dev/config.rb', line 95
def create_admin_user_from_settings(settings)
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
end
|
#create_new_user ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/discourse_dev/config.rb', line 65
def create_new_user
settings = config["new_user"]
if settings.present?
email = settings["email"] || "[email protected]"
new_user = ::User.create!(
email: email,
username: settings["username"] || UserNameSuggester.suggest(email)
)
new_user.email_tokens.update_all confirmed: true
new_user.activate
end
end
|
#set_seed ⇒ Object
80
81
82
83
|
# File 'lib/discourse_dev/config.rb', line 80
def set_seed
seed = self.seed || 1
Faker::Config.random = Random.new(seed)
end
|
#start_date ⇒ Object
85
86
87
|
# File 'lib/discourse_dev/config.rb', line 85
def start_date
DateTime.parse(config["start_date"] || default_config["start_date"])
end
|
#update! ⇒ Object
24
25
26
27
28
29
|
# File 'lib/discourse_dev/config.rb', line 24
def update!
update_site_settings
create_admin_user
create_new_user
set_seed
end
|
#update_site_settings ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/discourse_dev/config.rb', line 31
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
|