Skip to content

Commit

Permalink
Seperate random and demo data into individual seeders
Browse files Browse the repository at this point in the history
  • Loading branch information
leewaa authored and machisuji committed Nov 10, 2015
1 parent 536c178 commit 9fca805
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 217 deletions.
45 changes: 2 additions & 43 deletions app/seeders/demo_data/wiki_seeder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,52 +27,11 @@
# See doc/COPYRIGHT.rdoc for more details.
module DemoData
class WikiSeeder
attr_accessor :user, :project
def self.seed!(project)
user = User.admin.first

def initialize(project)
self.user = User.admin.first
self.project = project
end

def seed!(random: true)
puts ''
print ' ↳ Creating wikis'

if random
create_random_wiki_pages
else
create_demo_wiki
end
end

def create_random_wiki_pages
rand(5).times do
print '.'
wiki_page = WikiPage.create(
wiki: project.wiki,
title: Faker::Lorem.words(5).join(' ')
)

## create some wiki contents
rand(5).times do
print '.'
wiki_content = WikiContent.create(
page: wiki_page,
authorz: user,
text: Faker::Lorem.paragraph(5, true, 3)
)

## create some journal entries
rand(5).times do
wiki_content.reload
wiki_content.text = Faker::Lorem.paragraph(5, true, 3) if rand(99).even?
wiki_content.save!
end
end
end
end

def create_demo_wiki
print '.'
wiki_page = WikiPage.create!(
wiki: project.wiki,
Expand Down
162 changes: 8 additions & 154 deletions app/seeders/demo_data/work_package_seeder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,48 +38,25 @@ def initialize(project)
self.types = project.types.all.reject(&:is_milestone?)
end

def seed!(random: true)
def seed!
puts ''
print ' ↳ Creating work_packages'

if random
seed_random_work_packages
else
seed_demo_work_packages
end
seed_demo_work_packages
end

private

def seed_random_work_packages
rand(50).times do
print '.'
work_package = new_work_package
work_package.priority = IssuePriority.all.sample
work_package.description = Faker::Lorem.paragraph(5, true, 3)
work_package.save!
end

work_package = WorkPackage.first

if repository
add_changeset(work_package)
end

add_time_entries(work_package)
add_attachments(work_package)
add_custom_values(work_package)
make_changes(work_package)
end

def seed_demo_work_packages
work_packages_data = I18n.t('seeders.demo_data.work_packages')

work_packages_data.each do |attributes|
start_date = calculate_start_date(attributes[:start])

print '.'
work_package = new_work_package(
work_package = WorkPackage.create!(
project: project,
author: user,
subject: attributes[:subject],
status: Status.find_by!(name: I18n.t(attributes[:status_name])),
type: Type.find_by!(name: I18n.t(attributes[:type_name])),
Expand All @@ -91,7 +68,9 @@ def seed_demo_work_packages
start_date = calculate_start_date(child_attributes[:start])

print '.'
child = new_work_package(
child = WorkPackage.create!(
project: project,
author: user,
subject: child_attributes[:subject],
status: Status.find_by!(name: I18n.t(child_attributes[:status_name])),
type: Type.find_by!(name: I18n.t(child_attributes[:type_name])),
Expand All @@ -105,131 +84,6 @@ def seed_demo_work_packages
end
end

def add_changeset(work_package)
2.times do |changeset_count|
print '.'
changeset = Changeset.create(
repository: repository,
user: user,
revision: work_package.id * 10 + changeset_count,
scmid: work_package.id * 10 + changeset_count,
user: user,
work_packages: [work_package],
committer: Faker::Name.name,
committed_on: Date.today,
comments: Faker::Lorem.words(8).join(' ')
)

5.times do
print '.'
change = Change.create(
action: Faker::Lorem.characters(1),
path: Faker::Internet.url
)

changeset.file_changes << change
end

repository.changesets << changeset

changeset.save!

rand(5).times do
print '.'
changeset.reload

changeset.committer = Faker::Name.name if rand(99).even?
changeset.committed_on = Date.today + rand(999) if rand(99).even?
changeset.comments = Faker::Lorem.words(8).join(' ') if rand(99).even?

changeset.save!
end
end
end

def add_time_entries(work_package)
5.times do |time_entry_count|
time_entry = TimeEntry.create(
project: project,
user: user,
work_package: work_package,
spent_on: Date.today + time_entry_count,
activity: time_entry_activities.sample,
hours: time_entry_count
)
work_package.time_entries << time_entry
end
end

def add_attachments(work_package)
3.times do |_attachment_count|
file = OpenProject::Files.create_uploaded_file(name: Faker::Lorem.words(8).join(' '))
attachment = Attachment.new(
container: work_package,
author: user,
file: file
)
attachment.save!

work_package.attachments << attachment
end
end

def add_custom_values(work_package)
project.work_package_custom_fields.each do |custom_field|
work_package.type.custom_fields << custom_field if !work_package.type.custom_fields.include?(custom_field)
work_package.custom_values << CustomValue.new(custom_field: custom_field,
value: Faker::Lorem.words(8).join(' '))
end

work_package.type.save!
work_package.save!
end

def make_changes(work_package)
20.times do
print '.'
work_package.reload

work_package.status = statuses.sample if rand(99).even?
work_package.subject = Faker::Lorem.words(8).join(' ') if rand(99).even?
work_package.description = Faker::Lorem.paragraph(5, true, 3) if rand(99).even?
work_package.type = types.sample if rand(99).even?

work_package.time_entries.each do |t|
t.spent_on = Date.today + rand(100) if rand(99).even?
t.activity = time_entry_activities.sample if rand(99).even?
t.hours = rand(10) if rand(99).even?
end

work_package.reload

attachments = work_package.attachments.select { |_a| rand(999) < 10 }
work_package.attachments = work_package.attachments - attachments

work_package.reload

work_package.custom_values.each do |cv|
cv.value = Faker::Code.isbn if rand(99).even?
end

work_package.save!
end
end

# Using the attribute values passed in or random onesbb
def new_work_package(subject: nil, status: nil, type: nil, start_date: nil, due_date: nil)
WorkPackage.create!(
project: project,
author: user,
subject: subject || Faker::Lorem.words(8).join(' '),
status: status || statuses.sample,
type: type || types.sample,
start_date: start_date || s = Date.today - (25 - rand(50)).days,
due_date: due_date || s + (1 + rand(120)).days
)
end

def calculate_start_date(days_ahead)
monday = Date.today.monday
days_ahead > 0 ? monday + days_ahead : monday
Expand Down
13 changes: 4 additions & 9 deletions app/seeders/demo_data_seeder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,16 @@ def self.seed!
project = DemoData::ProjectSeeder.seed!

DemoData::CustomFieldSeeder.seed!(project)
DemoData::BoardSeeder.seed!(project)
DemoData::WikiSeeder.new(project).seed! random: false
DemoData::WorkPackageSeeder.new(project).seed! random: false
DemoData::NewsSeeder.seed!(project)
DemoData::WikiSeeder.seed!(project)
DemoData::WorkPackageSeeder.new(project).seed!

puts "\n\n"
puts ' ###############################'
puts ' # Demo data seeding....done #'
puts ' ###############################'
puts " # %02d %-22s #" % [1, 'project created.']
puts " # %02d %-22s #" % [WorkPackage.where(project_id: project.id).count, 'issues created.']
puts " # %02d %-22s #" % [Message.joins(:board).where(boards: { project_id: project.id }).count, 'messages created.']
puts " # %02d %-22s #" % [News.where(project_id: project.id).count, 'news created.']
puts " # %02d %-22s #" % [WikiContent.joins(page: [:wiki]).where('wikis.project_id = ?', project.id).count, 'wiki contents created.']
puts " # %02d %-22s #" % [TimeEntry.where(project_id: project.id).count, 'time entries created.']
puts " # %02d %-22s #" % [Changeset.joins(:repository).where(repositories: { project_id: project.id }).count, 'changesets created.']
puts " # %02d %-22s #" % [WikiContent.joins(page: [:wiki]).where('wikis.project_id = ?', project.id).count, 'wiki created.']
puts " ###############################\n\n"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See doc/COPYRIGHT.rdoc for more details.
module DemoData
module RandomData
class BoardSeeder
def self.seed!(project)
user = User.admin.first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See doc/COPYRIGHT.rdoc for more details.
module DemoData
module RandomData
class NewsSeeder

def self.seed!(project)
user = User.admin.first

## create some news

puts ''
print ' ↳ Creating news'

Expand All @@ -45,7 +42,6 @@ def self.seed!(project)
description: Faker::Lorem.paragraph(5, true, 3)

## create some journal entries

rand(5).times do
news.reload

Expand All @@ -57,6 +53,5 @@ def self.seed!(project)
end
end
end

end
end
62 changes: 62 additions & 0 deletions app/seeders/random_data/wiki_seeder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#-- encoding: UTF-8
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See doc/COPYRIGHT.rdoc for more details.
module RandomData
class WikiSeeder
def self.seed!(project)
user = User.admin.first

puts ''
print ' ↳ Creating wikis'

rand(5).times do
print '.'
wiki_page = WikiPage.create(
wiki: project.wiki,
title: Faker::Lorem.words(5).join(' ')
)

## create some wiki contents
rand(5).times do
print '.'
wiki_content = WikiContent.create(
page: wiki_page,
author: user,
text: Faker::Lorem.paragraph(5, true, 3)
)

## create some journal entries
rand(5).times do
wiki_content.reload
wiki_content.text = Faker::Lorem.paragraph(5, true, 3) if rand(99).even?
wiki_content.save!
end
end
end
end
end
end
Loading

0 comments on commit 9fca805

Please sign in to comment.