Sha256: 38065d8de6dd062020f79f1d2dc96229d772cd4f4bba74a75ff9b22ccf9e936f

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

require 'discourse_dev/record'
require 'faker'

module DiscourseDev
  class Post < Record

    attr_reader :topic

    def initialize(topic, count = DEFAULT_COUNT)
      super(::Post, count)
      @topic = topic

      category = topic.category
      unless category.groups.blank?
        group_ids = category.groups.pluck(:id)
        @user_ids = ::GroupUser.where(group_id: group_ids).pluck(:user_id)
        @user_count = @user_ids.count
      end
    end

    def data
      {
        topic_id: topic.id,
        raw: Faker::Markdown.sandwich(sentences: 5),
        skip_validations: true
      }
    end

    def create!
      PostCreator.new(user, data).create!
    end

    def user
      return User.random if topic.category.groups.blank?
      return Discourse.system_user if @user_ids.blank?
      
      position = Faker::Number.between(from: 0, to: @user_count - 1)
      ::User.find(@user_ids[position])
    end

    def populate!
      @count.times do |i|
        @index = i
        create!
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
discourse_dev-0.0.5 lib/discourse_dev/post.rb