Skip to content
This repository has been archived by the owner on Jan 28, 2022. It is now read-only.

Commit

Permalink
FEATURE: load custom site settings from yaml file.
Browse files Browse the repository at this point in the history
  • Loading branch information
vinothkannans committed Mar 4, 2021
1 parent 662f2f1 commit 4116077
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 8 deletions.
11 changes: 10 additions & 1 deletion lib/discourse_dev/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def initialize(count = DEFAULT_COUNT)
super(::Category, count)
@existing_names = ::Category.pluck(:name)
@parent_category_ids = ::Category.where(parent_category_id: nil).pluck(:id)
@group_count = ::Group.count
end

def data
Expand Down Expand Up @@ -40,7 +41,15 @@ def data
end

def permissions
@permissions || { everyone: :full }
return @permissions if @permissions.present?
return { everyone: :full } if Faker::Boolean.boolean(true_ratio: 0.75)

permission = {}
offset = rand(@group_count)
group = ::Group.offset(offset).first
permission[group.id] = Faker::Number.between(from: 1, to: 3)

permission
end

def create!
Expand Down
46 changes: 46 additions & 0 deletions lib/discourse_dev/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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
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
end
end
3 changes: 3 additions & 0 deletions lib/discourse_dev/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
site_settings:
tagging_enabled: false
verbose_discourse_connect_logging: true
11 changes: 10 additions & 1 deletion lib/discourse_dev/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,20 @@ def data

{
name: name,
allow_membership_requests: Faker::Boolean.boolean,
public_exit: Faker::Boolean.boolean,
public_admission: Faker::Boolean.boolean,
primary_group: Faker::Boolean.boolean
}
end

def create!
super do |group|
if Faker::Boolean.boolean
group.add_owner(::Discourse.system_user)
group.allow_membership_requests = true
group.save!
end
end
end
end
end
8 changes: 2 additions & 6 deletions lib/discourse_dev/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module DiscourseDev
class Record
DEFAULT_COUNT = 30.freeze

attr_reader :model, :type
attr_reader :model, :type, :count

def initialize(model, count = DEFAULT_COUNT)
@model = model
Expand All @@ -23,15 +23,11 @@ def create!
end

def populate!
puts "Creating #{count} sample #{type.downcase}s"
puts "Creating #{count} sample #{type.downcase} records"
count.times { create! }
puts
end

def count
@count || DEFAULT_COUNT
end

def self.populate!
self.new.populate!
end
Expand Down
6 changes: 6 additions & 0 deletions lib/discourse_dev/tasks/dev.rake
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ task 'dev:reset' => ['db:load_config'] do |_, args|
check_environment!

Rake::Task['db:migrate:reset'].invoke
Rake::Task['dev:config'].invoke
Rake::Task['admin:create'].invoke
Rake::Task['dev:populate'].invoke
end

desc 'Initialize development environment'
task 'dev:config' => ['db:load_config'] do |_, args|
DiscourseDev::Config.new.update!
end

desc 'Populate sample content for development environment'
task 'dev:populate' => ['db:load_config'] do |_, args|
Rake::Task['groups:populate'].invoke
Expand Down

0 comments on commit 4116077

Please sign in to comment.