diff --git a/app/models/platform.rb b/app/models/platform.rb index 13c7feabf..b0f5c467a 100644 --- a/app/models/platform.rb +++ b/app/models/platform.rb @@ -5,16 +5,25 @@ class Platform < ActiveRecord::Base validate :name, :presence => true, :uniqueness => true validate :unixname, :uniqueness => true, :presence => true, :format => { :with => /^[a-zA-Z0-9\-.]+$/ }, :allow_nil => false, :allow_blank => false - before_validation :generate_unixname + before_create :create_directory def path - File.join(APP_CONFIG['root_path'], unixname) + build_path(unixname) end protected - def generate_unixname - self.unixname = name.gsub(/[^a-zA-Z0-9\-.]/, '-') - #TODO: Fix non-unique unixname + def build_path(dir) + File.join(APP_CONFIG['root_path'], dir) + end + + def create_directory + exists = File.exists?(path) && File.directory?(path) + raise "Directory #{path} already exists" if exists + if new_record? + FileUtils.mkdir_p(path) + elsif unixname_changed? + FileUtils.mv(build_path(unixname_was), buildpath(unixname)) + end end end diff --git a/app/models/project.rb b/app/models/project.rb index d81693b92..ad3c660b1 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -4,23 +4,33 @@ class Project < ActiveRecord::Base validate :name, :uniqueness => true, :presence => true, :allow_nil => false, :allow_blank => false validate :unixname, :uniqueness => true, :presence => true, :format => { :with => /^[a-zA-Z0-9\-.]+$/ }, :allow_nil => false, :allow_blank => false - before_validation :generate_unixname - include Project::HasRepository + before_create :create_directory + # Redefining a method from Project::HasRepository module to reflect current situation def git_repo_path @git_repo_path ||= File.join(APP_CONFIG['root_path'], platform.unixname, unixname, unixname + '.git') end def path - File.join(APP_CONFIG['root_path'], platform.unixname, unixname) + build_path(unixname) end protected - def generate_unixname - self.unixname = name.gsub(/[^a-zA-Z0-9\-.]/, '-') - #TODO: Fix non-unique unixname + def build_path(dir) + File.join(APP_CONFIG['root_path'], platform.unixname, dir) + end + + #TODO: Spec me + def create_directory + exists = File.exists?(path) && File.directory?(path) + raise "Directory #{path} already exists" if exists + if new_record? + FileUtils.mkdir_p(path) + elsif unixname_changed? + FileUtils.mv(build_path(unixname_was), buildpath(unixname)) + end end end diff --git a/app/views/platforms/new.html.haml b/app/views/platforms/new.html.haml index 46e4a60d9..36a7d0283 100644 --- a/app/views/platforms/new.html.haml +++ b/app/views/platforms/new.html.haml @@ -3,6 +3,9 @@ %p = f.label :name = f.text_field :name + %p + = f.label :unixname + = f.text_field :unixname %p = f.label :parent_platform_id = f.select :parent_platform_id, @platforms.map { |p| [p.name, p.id] }, :include_blank => true diff --git a/app/views/projects/new.html.haml b/app/views/projects/new.html.haml index 706d28291..039108f4e 100644 --- a/app/views/projects/new.html.haml +++ b/app/views/projects/new.html.haml @@ -3,6 +3,9 @@ %p = f.label :name = f.text_field :name + %p + = f.label :unixname + = f.text_field :unixname %p = f.submit t('layout.create') = link_to t('layout.cancel'), platform_path(@platform) diff --git a/db/seeds.rb b/db/seeds.rb index 7a96639d9..c9f4bd74f 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -17,7 +17,7 @@ TEST_USERS.each do |tuser| end -TEST_PLATFORMS = %w(cooker Mandriva2010.10 Mandriva2011.4) +TEST_PLATFORMS = %w(cooker Mandriva2010-10 Mandriva2011.4) TEST_PROJECTS = %w(gcc glibc mysql-dev ruby ruby1.9 mc mesa avrdude vim gvim openssh-server openssh nethack binutils build-essentials rpm rpmtools ffmpeg mkvtoolnix libogg mpg123 openbox openoffice.org) @@ -25,6 +25,7 @@ TEST_PLATFORMS.each do |platform| p = Platform.find_or_create_by_name(platform) TEST_PROJECTS.each do |project| pr = Project.find_or_initialize_by_platform_id_and_name(p.id, project) + pr.unixname = pr.name puts "#{project} added to #{platform}" if pr.new_record? pr.save! end