discourse_dev 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b40ff76d084fee2f0eeef50807149a399e6c4bbdf256259dd23a6925d6bdeb7
4
- data.tar.gz: 748af95af548a6d79ee21d96dd68688d5c554cb78391a416b63980805d4b25a3
3
+ metadata.gz: b494dc262cd9afaa6f8f3906f3f9f23cb452cf852504dc095f411ece98858119
4
+ data.tar.gz: c98f3d163fa2e68b6bedd53984924432ecd1234534425d2cba565614d1fa3a59
5
5
  SHA512:
6
- metadata.gz: '0914512344fa239f36c6ae42c4890ef4cad9fd991a234c5f8b301751a84a89633d20f035e64e526935031767842b27e6a36758e8ea982a3672e83e22c2826b4b'
7
- data.tar.gz: d9cfa147a8616124232baef18d01203cd09b6c88a5d5c4c7b9a7527e4ef56d4f79c07c0d2ea13809907ba81b3cc38c4b669307d2a6ebef57b4d21364b96ba1b4
6
+ metadata.gz: 37b9f8b3f1fd4a555e260de5f4470236c2f68bc9141b5b68171d210b933137e1f6364de8213a6e189af25d5f36deb26b6641bcef6e9f8518e147615f366b7b67
7
+ data.tar.gz: d7b3b6502018d94908f0364f191834be6000f0d4341e66504214cfa1d1b1738897f34b8534c037aa3ac8382811b97ad981b484844179c6b9bd6f83fa19fd9995
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ DiscourseDev::Engine.routes.draw do
4
+ get ':username_or_email/become' => 'admin/impersonate#create', constraints: AdminConstraint.new
5
+ end
data/lib/discourse_dev.rb CHANGED
@@ -8,5 +8,6 @@ I18n.load_path += Dir[File.join(__dir__, 'faker', 'locales', '**/*.yml')]
8
8
  I18n.reload! if I18n.backend.initialized?
9
9
 
10
10
  module DiscourseDev
11
- require 'discourse_dev/railtie' if defined?(Rails)
11
+ require 'discourse_dev/railtie'
12
+ require 'discourse_dev/engine'
12
13
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DiscourseDev
4
+ class Engine < Rails::Engine; end
5
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'discourse_dev/record'
4
+ require 'faker'
5
+
6
+ module DiscourseDev
7
+ class Post < Record
8
+
9
+ attr_reader :topic
10
+
11
+ def initialize(topic, count = DEFAULT_COUNT)
12
+ super(::Post, count)
13
+ @topic = topic
14
+
15
+ category = topic.category
16
+ unless category.groups.blank?
17
+ group_ids = category.groups.pluck(:id)
18
+ @user_ids = ::GroupUser.where(group_id: group_ids).pluck(:user_id)
19
+ @user_count = @user_ids.count
20
+ end
21
+ end
22
+
23
+ def data
24
+ {
25
+ topic_id: topic.id,
26
+ raw: Faker::Markdown.sandwich(sentences: 5),
27
+ skip_validations: true
28
+ }
29
+ end
30
+
31
+ def create!
32
+ PostCreator.new(user, data).create!
33
+ end
34
+
35
+ def user
36
+ return User.random if topic.category.groups.blank?
37
+ return Discourse.system_user if @user_ids.blank?
38
+
39
+ position = Faker::Number.between(from: 0, to: @user_count - 1)
40
+ ::User.find(@user_ids[position])
41
+ end
42
+
43
+ def populate!
44
+ @count.times do |i|
45
+ @index = i
46
+ create!
47
+ end
48
+ end
49
+ end
50
+ end
@@ -24,6 +24,17 @@ module DiscourseDev
24
24
  end
25
25
 
26
26
  def populate!
27
+ if current_count >= @count
28
+ puts "Already have #{@count}+ #{type.downcase} records."
29
+
30
+ Rake.application.top_level_tasks.each do |task_name|
31
+ Rake::Task[task_name].reenable
32
+ end
33
+
34
+ Rake::Task['dev:repopulate'].invoke
35
+ return
36
+ end
37
+
27
38
  puts "Creating #{@count} sample #{type.downcase} records"
28
39
 
29
40
  @count.times do |i|
@@ -39,6 +50,10 @@ module DiscourseDev
39
50
  @index
40
51
  end
41
52
 
53
+ def current_count
54
+ model.count
55
+ end
56
+
42
57
  def self.populate!
43
58
  self.new.populate!
44
59
  end
@@ -28,3 +28,16 @@ task 'dev:populate' => ['db:load_config'] do |_, args|
28
28
  Rake::Task['tags:populate'].invoke
29
29
  Rake::Task['topics:populate'].invoke
30
30
  end
31
+
32
+ desc 'Repopulate sample datas in development environment'
33
+ task 'dev:repopulate' => ['db:load_config'] do |_, args|
34
+ require 'highline/import'
35
+
36
+ answer = ask("Do you want to repopulate the database with fresh data? It will recreate DBs and run migration from scratch before generating all the samples. (Y/n) ")
37
+
38
+ if (answer == "" || answer.downcase == 'y')
39
+ Rake::Task['dev:reset'].invoke
40
+ else
41
+ puts "You can run `dev:reset` rake task to do this repopulate action anytime."
42
+ end
43
+ end
@@ -41,7 +41,9 @@ module DiscourseDev
41
41
 
42
42
  def create!
43
43
  @category = Category.random
44
- PostCreator.new(user, data).create!
44
+ post = PostCreator.new(user, data).create!
45
+
46
+ Post.new(post.topic, Faker::Number.between(from: 0, to: 5)).populate!
45
47
  end
46
48
 
47
49
  def user
@@ -53,5 +55,10 @@ module DiscourseDev
53
55
  position = Faker::Number.between(from: 0, to: user_count - 1)
54
56
  ::User.find(user_ids[position] || Discourse::SYSTEM_USER_ID)
55
57
  end
58
+
59
+ def current_count
60
+ category_definition_topic_ids = ::Category.pluck(:topic_id)
61
+ ::Topic.where(archetype: :regular).where.not(id: category_definition_topic_ids).count
62
+ end
56
63
  end
57
64
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DiscourseDev
4
- VERSION = "0.0.4"
4
+ VERSION = "0.0.5"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discourse_dev
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vinoth Kannan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-11 00:00:00.000000000 Z
11
+ date: 2021-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faker
@@ -65,12 +65,15 @@ files:
65
65
  - LICENSE.txt
66
66
  - README.md
67
67
  - Rakefile
68
+ - config/routes.rb
68
69
  - discourse_dev.gemspec
69
70
  - lib/discourse_dev.rb
70
71
  - lib/discourse_dev/category.rb
71
72
  - lib/discourse_dev/config.rb
72
73
  - lib/discourse_dev/config.yml
74
+ - lib/discourse_dev/engine.rb
73
75
  - lib/discourse_dev/group.rb
76
+ - lib/discourse_dev/post.rb
74
77
  - lib/discourse_dev/railtie.rb
75
78
  - lib/discourse_dev/record.rb
76
79
  - lib/discourse_dev/tag.rb