OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/app/seeders/demo_data/project_seeder.rb

237 lines
6.7 KiB

#-- 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 DemoData
class ProjectSeeder < Seeder
# Careful: The seeding recreates the seeded project before it runs, so any changes
# on the seeded project will be lost.
def seed_data!
6 years ago
["demo-project", "scrum-project"].each do |key|
puts " ↳ Creating #{key} project..."
6 years ago
puts ' -Creating/Resetting project'
project = reset_project key
6 years ago
puts ' -Setting members.'
set_members(project)
6 years ago
puts ' -Creating news.'
seed_news(project, key)
6 years ago
puts ' -Assigning types.'
set_types(project, key)
6 years ago
puts ' -Creating versions.'
seed_versions(project, key)
6 years ago
puts ' -Creating queries.'
seed_queries(project, key)
project_data_seeders(project, key).each do |seeder|
puts " -#{seeder.class.name.demodulize}"
seeder.seed!
end
end
end
def applicable?
Project.count.zero?
end
6 years ago
def project_data_seeders(project, key)
seeders = [
6 years ago
DemoData::WikiSeeder,
DemoData::CustomFieldSeeder,
6 years ago
DemoData::WorkPackageSeeder
]
6 years ago
seeders.map { |seeder| seeder.new project, key }
end
6 years ago
def reset_project(key)
delete_project(key)
create_project(key)
end
6 years ago
def create_project(key)
Project.create! project_data(key)
end
6 years ago
def delete_project(key)
if delete_me = find_project(key)
delete_me.destroy
end
end
def set_members(project)
role = Role.find_by(name: I18n.t(:default_role_project_admin))
user = User.admin.first
Member.create!(
project: project,
user: user,
roles: [role]
)
end
6 years ago
def set_types(project, key)
project.types.clear
Array(I18n.t("seeders.demo_data.projects.#{key}.types")).each do |type_name|
type = Type.find_by(name: I18n.t(type_name))
project.types << type
end
end
def seed_news(project, key)
Array(I18n.t("seeders.demo_data.projects.#{key}")[:news]).each do |news|
News.create! project: project, title: news[:title], description: news[:content]
end
end
def seed_queries(project, key)
Array(I18n.t("seeders.demo_data.projects.#{key}")[:queries]).each do |config|
attr = {
project: project,
name: config[:name],
user: User.admin.first,
is_public: true,
show_hierarchies: config[:hierarchy] == true,
timeline_visible: config[:timeline] == true
}
filters = {}
if status = String(config[:status])
filters[:status_id] = { operator: "o" } if status == "open"
end
if version = config[:version].presence
filters[:fixed_version_id] = { operator: "=", values: [Version.find_by(name: version).id] }
end
if sort_by = config[:sort_by]
attr[:sort_criteria] = [[sort_by, "asc"]]
end
if group_by = config[:group_by]
attr[:group_by] = group_by
end
types = Array(config[:type]).map do |name|
Type.find_by(name: I18n.t(name))
end
if !types.empty?
filters[:type_id] = { operator: "=", values: types.map(&:id).map(&:to_s) }
end
columns = Array(config[:columns])
attr[:column_names] = columns.map(&:to_s) unless columns.empty?
attr[:filters] = [filters] unless filters.empty?
query = Query.create! attr
MenuItems::QueryMenuItem.create! navigatable_id: query.id, name: SecureRandom.uuid, title: query.name
end
end
6 years ago
def seed_versions(project, key)
version_data = I18n.t("seeders.demo_data.projects.#{key}.versions")
return if version_data.is_a?(String) && version_data.start_with?("translation missing")
version_data.each do |attributes|
6 years ago
version = Version.create!(
name: attributes[:name],
status: attributes[:status],
sharing: attributes[:sharing]
)
6 years ago
project.versions << version
if attributes[:wiki]
version.wiki_page_title = attributes[:wiki][:title]
page = WikiPage.create! wiki: version.project.wiki, title: version.wiki_page_title
content = WikiContent.create! page: page, author: User.admin.first, text: attributes[:wiki][:content]
version.save!
end
end
end
def seed_board(project)
Board.create!(
project: project,
name: I18n.t('seeders.demo_data.board.name'),
description: I18n.t('seeders.demo_data.board.description')
)
end
module Data
module_function
6 years ago
def project_data(key)
{
6 years ago
name: project_name(key),
identifier: project_identifier(key),
description: project_description(key),
enabled_module_names: project_modules(key),
types: project_types
}
end
6 years ago
def project_name(key)
I18n.t("seeders.demo_data.projects.#{key}.name")
end
6 years ago
def project_identifier(key)
I18n.t("seeders.demo_data.projects.#{key}.identifier")
end
6 years ago
def project_description(key)
I18n.t("seeders.demo_data.projects.#{key}.description")
end
def project_types
Type.all
end
6 years ago
def project_modules(key)
I18n.t("seeders.demo_data.projects.#{key}.modules")
end
6 years ago
def find_project(key)
Project.find_by(identifier: project_identifier(key))
end
end
include Data
end
end