# frozen_string_literal: true resources = YAML.load_file Rails.root.join('db', 'seeds_development.yml') REQUEST_ATTRIBUTES = { ip: '203.0.113.0', user_agent: 'Hopefully not IE', referrer: 'localhost' }.freeze puts '---- Wiping thumbs directory' FileUtils.rm_rf(Rails.root.join('public', 'system')) unless Rails.env.production? # just to be sure... puts '---- Generating users' resources['users'].each do |user| User.create! name: user['name'], email: user['email'], password: user['password'], password_confirmation: user['password'], role: user['role'] puts "#{user['name']} (role: #{user['role']}) with password '#{user['password']}' and email '#{user['email']}'" end def create_post post = Post.new yield post post.assign_attributes REQUEST_ATTRIBUTES.merge(user: User.all.sample) post.save end puts '---- Generating posts' Dir.foreach(Rails.root.join('test', 'fixtures', 'image_files')).each do |image_file| next if image_file[0] == '.' # skip current/parent directories and hidden files. image_path = Rails.root.join('test', 'fixtures', 'image_files', image_file) tags = resources['tags'].sample(2).push('safe').join(',') post_attributes = {tag_input: tags, media_type: 'image', image_attributes: {file: File.open(image_path)}, description: 'Inserted by test fixtures'} .merge(REQUEST_ATTRIBUTES).merge(user: User.all.sample) Post.create!(post_attributes) end Dir.foreach(Rails.root.join('test', 'fixtures', 'pastes')).each do |paste_file| next if paste_file[0] == '.' paste_path = Rails.root.join('test', 'fixtures', 'pastes', paste_file) tags = resources['tags'].sample(2).push('safe').join(',') post_attributes = {tag_input: tags, media_type: 'paste', paste_attributes: {file: File.open(paste_path)}, description: 'Inserted by test fixtures'} .merge(REQUEST_ATTRIBUTES).merge(user: User.all.sample) Post.create!(post_attributes) end # puts '---- Inserting remote images' # resources['remote_images'].each do |remote_image| # create_image do |image| # image.tag_input = remote_image['tags'] # image.remote_image_url = remote_image['url'] # image.description = remote_image['description'] # end # end rescue nil puts '---- Generating comments for post #0' resources['comments'].each do |text| Comment.create!({ body: text, post: Post.first, user: User.all.sample, anonymous: [true, false].sample }.merge(REQUEST_ATTRIBUTES)) end puts '---- Generating forum posts' resources['forum_posts'].each do |forum| forum.each_pair do |forum_name, topics| topics.each do |t| title = t.first[0] posts = t.first[1] op = User.all.sample topic = Topic.create! title: title, forum: Forum.find_by(short_name: forum_name), user: op, anonymous: false ForumPost.create!({ topic: topic, body: posts.shift, user: op, anonymous: false }.merge(REQUEST_ATTRIBUTES)) posts.each do |post_body| ForumPost.create!({ topic: topic, body: post_body, user: User.all.sample, anonymous: [true, false].sample }.merge(REQUEST_ATTRIBUTES)) end end end end