From 4116077b94997fb9329a4ecb977c69a03548b132 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Thu, 4 Mar 2021 19:31:43 +0530 Subject: [PATCH] FEATURE: load custom site settings from yaml file. --- lib/discourse_dev/category.rb | 11 +++++++- lib/discourse_dev/config.rb | 46 ++++++++++++++++++++++++++++++++ lib/discourse_dev/config.yml | 3 +++ lib/discourse_dev/group.rb | 11 +++++++- lib/discourse_dev/record.rb | 8 ++---- lib/discourse_dev/tasks/dev.rake | 6 +++++ 6 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 lib/discourse_dev/config.rb create mode 100644 lib/discourse_dev/config.yml diff --git a/lib/discourse_dev/category.rb b/lib/discourse_dev/category.rb index 7ba0452..4f03983 100644 --- a/lib/discourse_dev/category.rb +++ b/lib/discourse_dev/category.rb @@ -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 @@ -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! diff --git a/lib/discourse_dev/config.rb b/lib/discourse_dev/config.rb new file mode 100644 index 0000000..3ff065e --- /dev/null +++ b/lib/discourse_dev/config.rb @@ -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 diff --git a/lib/discourse_dev/config.yml b/lib/discourse_dev/config.yml new file mode 100644 index 0000000..a3c3ee3 --- /dev/null +++ b/lib/discourse_dev/config.yml @@ -0,0 +1,3 @@ +site_settings: + tagging_enabled: false + verbose_discourse_connect_logging: true diff --git a/lib/discourse_dev/group.rb b/lib/discourse_dev/group.rb index 0baa8d2..b7dd1c4 100644 --- a/lib/discourse_dev/group.rb +++ b/lib/discourse_dev/group.rb @@ -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 diff --git a/lib/discourse_dev/record.rb b/lib/discourse_dev/record.rb index 78bf21a..3ebef6a 100644 --- a/lib/discourse_dev/record.rb +++ b/lib/discourse_dev/record.rb @@ -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 @@ -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 diff --git a/lib/discourse_dev/tasks/dev.rake b/lib/discourse_dev/tasks/dev.rake index c5428bd..bb7253f 100644 --- a/lib/discourse_dev/tasks/dev.rake +++ b/lib/discourse_dev/tasks/dev.rake @@ -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