]> source.dussan.org Git - redmine.git/commitdiff
Moved redmine:load_default_data code to a module so that it can be called from the...
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 4 Jan 2008 18:27:05 +0000 (18:27 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 4 Jan 2008 18:27:05 +0000 (18:27 +0000)
git-svn-id: http://redmine.rubyforge.org/svn/trunk@1037 e93f8b46-1217-0410-a6f0-8f06a7374b81

lib/redmine/default_data/loader.rb [new file with mode: 0644]
lib/tasks/load_default_data.rake

diff --git a/lib/redmine/default_data/loader.rb b/lib/redmine/default_data/loader.rb
new file mode 100644 (file)
index 0000000..1052191
--- /dev/null
@@ -0,0 +1,171 @@
+# redMine - project management software
+# Copyright (C) 2006-2007  Jean-Philippe Lang
+#
+# 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.
+
+module Redmine
+  module DefaultData
+    class DataAlreadyLoaded < Exception; end
+
+    module Loader
+      include GLoc
+    
+      class << self
+        # Returns true if no data is already loaded in the database
+        # otherwise false
+        def no_data?
+          !Role.find(:first, :conditions => {:builtin => 0}) &&
+            !Tracker.find(:first) &&
+            !IssueStatus.find(:first) &&
+            !Enumeration.find(:first)
+        end
+        
+        # Loads the default data
+        # Raises a RecordNotSaved exception if something goes wrong
+        def load(lang=nil)
+          raise DataAlreadyLoaded.new("Some configuration data is already loaded.") unless no_data?
+          set_language_if_valid(lang)
+          
+          # Roles
+          manager = Role.create! :name => l(:default_role_manager), 
+                                 :position => 1
+          manager.permissions = manager.setable_permissions.collect {|p| p.name}
+          manager.save!
+          
+          developper = Role.create! :name => l(:default_role_developper), 
+                                    :position => 2, 
+                                    :permissions => [:manage_versions, 
+                                                    :manage_categories,
+                                                    :add_issues,
+                                                    :edit_issues,
+                                                    :manage_issue_relations,
+                                                    :add_issue_notes,
+                                                    :change_issue_status,
+                                                    :save_queries,
+                                                    :view_gantt,
+                                                    :view_calendar,
+                                                    :log_time,
+                                                    :view_time_entries,
+                                                    :comment_news,
+                                                    :view_documents,
+                                                    :view_wiki_pages,
+                                                    :edit_wiki_pages,
+                                                    :delete_wiki_pages,
+                                                    :add_messages,
+                                                    :view_files,
+                                                    :manage_files,
+                                                    :browse_repository,
+                                                    :view_changesets]
+          
+          reporter = Role.create! :name => l(:default_role_reporter),
+                                  :position => 3,
+                                  :permissions => [:add_issues,
+                                                  :add_issue_notes,
+                                                  :change_issue_status,
+                                                  :save_queries,
+                                                  :view_gantt,
+                                                  :view_calendar,
+                                                  :log_time,
+                                                  :view_time_entries,
+                                                  :comment_news,
+                                                  :view_documents,
+                                                  :view_wiki_pages,
+                                                  :add_messages,
+                                                  :view_files,
+                                                  :browse_repository,
+                                                  :view_changesets]
+                      
+          Role.non_member.update_attribute :permissions, [:add_issues,
+                                                          :add_issue_notes,
+                                                          :change_issue_status,
+                                                          :save_queries,
+                                                          :view_gantt,
+                                                          :view_calendar,
+                                                          :view_time_entries,
+                                                          :comment_news,
+                                                          :view_documents,
+                                                          :view_wiki_pages,
+                                                          :add_messages,
+                                                          :view_files,
+                                                          :browse_repository,
+                                                          :view_changesets]
+        
+          Role.anonymous.update_attribute :permissions, [:view_gantt,
+                                                         :view_calendar,
+                                                         :view_time_entries,
+                                                         :view_documents,
+                                                         :view_wiki_pages,
+                                                         :view_files,
+                                                         :browse_repository,
+                                                         :view_changesets]
+                                                           
+          # Trackers
+          Tracker.create!(:name => l(:default_tracker_bug),     :is_in_chlog => true,  :is_in_roadmap => false, :position => 1)
+          Tracker.create!(:name => l(:default_tracker_feature), :is_in_chlog => true,  :is_in_roadmap => true,  :position => 2)
+          Tracker.create!(:name => l(:default_tracker_support), :is_in_chlog => false, :is_in_roadmap => false, :position => 3)
+          
+          # Issue statuses
+          new       = IssueStatus.create!(:name => l(:default_issue_status_new), :is_closed => false, :is_default => true, :position => 1)
+          assigned  = IssueStatus.create!(:name => l(:default_issue_status_assigned), :is_closed => false, :is_default => false, :position => 2)
+          resolved  = IssueStatus.create!(:name => l(:default_issue_status_resolved), :is_closed => false, :is_default => false, :position => 3)
+          feedback  = IssueStatus.create!(:name => l(:default_issue_status_feedback), :is_closed => false, :is_default => false, :position => 4)
+          closed    = IssueStatus.create!(:name => l(:default_issue_status_closed), :is_closed => true, :is_default => false, :position => 5)
+          rejected  = IssueStatus.create!(:name => l(:default_issue_status_rejected), :is_closed => true, :is_default => false, :position => 6)
+          
+          # Workflow
+          Tracker.find(:all).each { |t|
+            IssueStatus.find(:all).each { |os|
+              IssueStatus.find(:all).each { |ns|
+                Workflow.create!(:tracker_id => t.id, :role_id => manager.id, :old_status_id => os.id, :new_status_id => ns.id) unless os == ns
+              }        
+            }      
+          }
+          
+          Tracker.find(:all).each { |t|
+            [new, assigned, resolved, feedback].each { |os|
+              [assigned, resolved, feedback, closed].each { |ns|
+                Workflow.create!(:tracker_id => t.id, :role_id => developper.id, :old_status_id => os.id, :new_status_id => ns.id) unless os == ns
+              }        
+            }      
+          }
+          
+          Tracker.find(:all).each { |t|
+            [new, assigned, resolved, feedback].each { |os|
+              [closed].each { |ns|
+                Workflow.create!(:tracker_id => t.id, :role_id => reporter.id, :old_status_id => os.id, :new_status_id => ns.id) unless os == ns
+              }        
+            }
+            Workflow.create!(:tracker_id => t.id, :role_id => reporter.id, :old_status_id => resolved.id, :new_status_id => feedback.id)
+          }
+        
+          # Enumerations
+          Enumeration.create!(:opt => "DCAT", :name => l(:default_doc_category_user), :position => 1)
+          Enumeration.create!(:opt => "DCAT", :name => l(:default_doc_category_tech), :position => 2)
+        
+          Enumeration.create!(:opt => "IPRI", :name => l(:default_priority_low), :position => 1)
+          Enumeration.create!(:opt => "IPRI", :name => l(:default_priority_normal), :position => 2, :is_default => true)
+          Enumeration.create!(:opt => "IPRI", :name => l(:default_priority_high), :position => 3)
+          Enumeration.create!(:opt => "IPRI", :name => l(:default_priority_urgent), :position => 4)
+          Enumeration.create!(:opt => "IPRI", :name => l(:default_priority_immediate), :position => 5)
+        
+          Enumeration.create!(:opt => "ACTI", :name => l(:default_activity_design), :position => 1)
+          Enumeration.create!(:opt => "ACTI", :name => l(:default_activity_development), :position => 2)
+          
+          true
+        end
+      end
+    end
+  end
+end
index 8e89d4ecd3da683299e0312df5911d4c639d13aa..6ddd1fb9723e13088bf4611efa7f1c3b9829c9a0 100644 (file)
 desc 'Load Redmine default configuration data'\r
 \r
 namespace :redmine do\r
-task :load_default_data => :environment do\r
-  include GLoc\r
-  set_language_if_valid('en')\r
-  puts\r
-  \r
-  while true\r
-    print "Select language: "\r
-    print GLoc.valid_languages.sort {|x,y| x.to_s <=> y.to_s }.join(", ")\r
-    print " [#{GLoc.current_language}] "\r
-    lang = STDIN.gets.chomp!\r
-    break if lang.empty?\r
-    break if set_language_if_valid(lang)\r
-    puts "Unknown language!"\r
-  end\r
+  task :load_default_data => :environment do\r
+    include GLoc\r
+    set_language_if_valid('en')\r
+    puts\r
     \r
-  puts "===================================="\r
-  \r
-begin\r
-  # check that no data already exists\r
-  if Role.find(:first, :conditions => {:builtin => 0})\r
-    raise "Some roles are already defined."\r
-  end\r
-  if Tracker.find(:first)\r
-    raise "Some trackers are already defined."\r
-  end\r
-  if IssueStatus.find(:first)\r
-    raise "Some statuses are already defined."\r
-  end\r
-  if Enumeration.find(:first)\r
-    raise "Some enumerations are already defined."\r
-  end\r
+    while true\r
+      print "Select language: "\r
+      print GLoc.valid_languages.sort {|x,y| x.to_s <=> y.to_s }.join(", ")\r
+      print " [#{GLoc.current_language}] "\r
+      lang = STDIN.gets.chomp!\r
+      break if lang.empty?\r
+      break if set_language_if_valid(lang)\r
+      puts "Unknown language!"\r
+    end\r
     \r
-  puts "Loading default configuration data for language: #{current_language}"\r
\r
-  # roles\r
-  manager = Role.create :name => l(:default_role_manager), \r
-                        :position => 1\r
-  manager.permissions = manager.setable_permissions.collect {|p| p.name}\r
-  manager.save\r
-  \r
-  developper = Role.create :name => l(:default_role_developper), \r
-                           :position => 2, \r
-                           :permissions => [:manage_versions, \r
-                                            :manage_categories,\r
-                                            :add_issues,\r
-                                            :edit_issues,\r
-                                            :manage_issue_relations,\r
-                                            :add_issue_notes,\r
-                                            :change_issue_status,\r
-                                            :save_queries,\r
-                                            :view_gantt,\r
-                                            :view_calendar,\r
-                                            :log_time,\r
-                                            :view_time_entries,\r
-                                            :comment_news,\r
-                                            :view_documents,\r
-                                            :view_wiki_pages,\r
-                                            :edit_wiki_pages,\r
-                                            :delete_wiki_pages,\r
-                                            :add_messages,\r
-                                            :view_files,\r
-                                            :manage_files,\r
-                                            :browse_repository,\r
-                                            :view_changesets]\r
-  \r
-  reporter = Role.create :name => l(:default_role_reporter),\r
-                         :position => 3,\r
-                         :permissions => [:add_issues,\r
-                                          :add_issue_notes,\r
-                                          :change_issue_status,\r
-                                          :save_queries,\r
-                                          :view_gantt,\r
-                                          :view_calendar,\r
-                                          :log_time,\r
-                                          :view_time_entries,\r
-                                          :comment_news,\r
-                                          :view_documents,\r
-                                          :view_wiki_pages,\r
-                                          :add_messages,\r
-                                          :view_files,\r
-                                          :browse_repository,\r
-                                          :view_changesets]\r
-              \r
-  Role.non_member.update_attribute :permissions, [:add_issues,\r
-                                                  :add_issue_notes,\r
-                                                  :change_issue_status,\r
-                                                  :save_queries,\r
-                                                  :view_gantt,\r
-                                                  :view_calendar,\r
-                                                  :view_time_entries,\r
-                                                  :comment_news,\r
-                                                  :view_documents,\r
-                                                  :view_wiki_pages,\r
-                                                  :add_messages,\r
-                                                  :view_files,\r
-                                                  :browse_repository,\r
-                                                  :view_changesets]\r
-\r
-  Role.anonymous.update_attribute :permissions, [:view_gantt,\r
-                                                 :view_calendar,\r
-                                                 :view_time_entries,\r
-                                                 :view_documents,\r
-                                                 :view_wiki_pages,\r
-                                                 :view_files,\r
-                                                 :browse_repository,\r
-                                                 :view_changesets]\r
-                                                   \r
-  # trackers\r
-  Tracker.create(:name => l(:default_tracker_bug), :is_in_chlog => true, :is_in_roadmap => false, :position => 1)\r
-  Tracker.create(:name => l(:default_tracker_feature), :is_in_chlog => true, :is_in_roadmap => true, :position => 2)\r
-  Tracker.create(:name => l(:default_tracker_support), :is_in_chlog => false, :is_in_roadmap => false, :position => 3)\r
-  \r
-  # issue statuses\r
-  new       = IssueStatus.create(:name => l(:default_issue_status_new), :is_closed => false, :is_default => true, :position => 1)\r
-  assigned  = IssueStatus.create(:name => l(:default_issue_status_assigned), :is_closed => false, :is_default => false, :position => 2)\r
-  resolved  = IssueStatus.create(:name => l(:default_issue_status_resolved), :is_closed => false, :is_default => false, :position => 3)\r
-  feedback  = IssueStatus.create(:name => l(:default_issue_status_feedback), :is_closed => false, :is_default => false, :position => 4)\r
-  closed    = IssueStatus.create(:name => l(:default_issue_status_closed), :is_closed => true, :is_default => false, :position => 5)\r
-  rejected  = IssueStatus.create(:name => l(:default_issue_status_rejected), :is_closed => true, :is_default => false, :position => 6)\r
-  \r
-  # workflow\r
-  Tracker.find(:all).each { |t|\r
-    IssueStatus.find(:all).each { |os|\r
-      IssueStatus.find(:all).each { |ns|\r
-        Workflow.create(:tracker_id => t.id, :role_id => manager.id, :old_status_id => os.id, :new_status_id => ns.id) unless os == ns\r
-      }        \r
-    }      \r
-  }\r
-  \r
-  Tracker.find(:all).each { |t|\r
-    [new, assigned, resolved, feedback].each { |os|\r
-      [assigned, resolved, feedback, closed].each { |ns|\r
-        Workflow.create(:tracker_id => t.id, :role_id => developper.id, :old_status_id => os.id, :new_status_id => ns.id) unless os == ns\r
-      }        \r
-    }      \r
-  }\r
-  \r
-  Tracker.find(:all).each { |t|\r
-    [new, assigned, resolved, feedback].each { |os|\r
-      [closed].each { |ns|\r
-        Workflow.create(:tracker_id => t.id, :role_id => reporter.id, :old_status_id => os.id, :new_status_id => ns.id) unless os == ns\r
-      }        \r
-    }\r
-    Workflow.create(:tracker_id => t.id, :role_id => reporter.id, :old_status_id => resolved.id, :new_status_id => feedback.id)\r
-  }\r
-\r
-  # enumerations\r
-  Enumeration.create(:opt => "DCAT", :name => l(:default_doc_category_user), :position => 1)\r
-  Enumeration.create(:opt => "DCAT", :name => l(:default_doc_category_tech), :position => 2)\r
-\r
-  Enumeration.create(:opt => "IPRI", :name => l(:default_priority_low), :position => 1)\r
-  Enumeration.create(:opt => "IPRI", :name => l(:default_priority_normal), :position => 2, :is_default => true)\r
-  Enumeration.create(:opt => "IPRI", :name => l(:default_priority_high), :position => 3)\r
-  Enumeration.create(:opt => "IPRI", :name => l(:default_priority_urgent), :position => 4)\r
-  Enumeration.create(:opt => "IPRI", :name => l(:default_priority_immediate), :position => 5)\r
-\r
-  Enumeration.create(:opt => "ACTI", :name => l(:default_activity_design), :position => 1)\r
-  Enumeration.create(:opt => "ACTI", :name => l(:default_activity_development), :position => 2)\r
\r
-rescue => error\r
-  puts "Error: " + error\r
-  puts "Default configuration data can't be loaded."\r
-end\r
-end\r
+    puts "===================================="\r
+    \r
+    begin\r
+      Redmine::DefaultData::Loader.load(current_language)\r
+      puts "Default configuration data loaded."\r
+    rescue => error\r
+      puts "Error: " + error\r
+      puts "Default configuration data was not loaded."\r
+    end\r
+  end\r
 end\r