diff --git a/app/controllers/platforms_controller.rb b/app/controllers/platforms_controller.rb index abf094934..fb435bb35 100644 --- a/app/controllers/platforms_controller.rb +++ b/app/controllers/platforms_controller.rb @@ -6,8 +6,8 @@ class PlatformsController < ApplicationController end def show - @platform = Platform.find params[:id], :include => :projects - @projects = @platform.projects + @platform = Platform.find params[:id], :include => :repositories + @repositories = @platform.repositories end def new diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 425c804db..61bf48d56 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -1,20 +1,21 @@ class ProjectsController < ApplicationController before_filter :authenticate_user! before_filter :find_platform + before_filter :find_repository before_filter :find_project, :only => [:show] def new - @project = @platform.projects.new + @project = @repository.projects.new end def show end def create - @project = @platform.projects.new params[:project] + @project = @repository.projects.new params[:project] if @project.save flash[:notice] = t('flash.project.saved') - redirect_to @platform + redirect_to [@platform, @repository] else flash[:error] = t('flash.project.save_error') render :action => :new @@ -27,7 +28,11 @@ class ProjectsController < ApplicationController @platform = Platform.find params[:platform_id] end + def find_repository + @repository = @platform.repositories.find(params[:repository_id]) + end + def find_project - @project = @platform.projects.find params[:id] + @project = @repository.projects.find params[:id] end end diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb new file mode 100644 index 000000000..9f540f1dd --- /dev/null +++ b/app/controllers/repositories_controller.rb @@ -0,0 +1,53 @@ +class RepositoriesController < ApplicationController + before_filter :authenticate_user! + before_filter :find_platform + + def index + @repositories = @platform.repositories + end + + def show + @repository = @platform.repositories.find params[:id], :include => :projects + @projects = @repository.projects + end + + def new + @repository = @platform.repositories.new + end + + def edit + @repository = @platform.repositories.find params[:id] + end + + def destroy + Repository.destroy params[:id] + end + + def update + @repository = @platform.repositories.find params[:id] + if @repository.update_attributes params[:repository] + flash[:notice] = '' + redirect_to @repository + else + flash[:error] = '' + render :action => :edit + end + end + + def create + @repository = @platform.repositories.new(params[:repository]) + if @repository.save + flash[:notice] = '' + redirect_to @platform, @repository + else + flash[:error] = '' + render :action => :new + end + end + + protected + + def find_platform + @platform = Platform.find params[:platform_id] + end +end diff --git a/app/models/platform.rb b/app/models/platform.rb index b0f5c467a..88e26eaa9 100644 --- a/app/models/platform.rb +++ b/app/models/platform.rb @@ -1,6 +1,6 @@ class Platform < ActiveRecord::Base - has_many :projects, :dependent => :destroy has_one :parent, :class_name => 'Platform', :foreign_key => 'parent_platform_id' + has_many :repositories, :dependent => :destroy validate :name, :presence => true, :uniqueness => true validate :unixname, :uniqueness => true, :presence => true, :format => { :with => /^[a-zA-Z0-9\-.]+$/ }, :allow_nil => false, :allow_blank => false diff --git a/app/models/project.rb b/app/models/project.rb index ad3c660b1..8a509fabb 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,5 +1,5 @@ class Project < ActiveRecord::Base - belongs_to :platform + belongs_to :repository 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 @@ -10,7 +10,7 @@ class Project < ActiveRecord::Base # 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') + @git_repo_path ||= File.join(path, unixname + ".git") end def path @@ -20,7 +20,7 @@ class Project < ActiveRecord::Base protected def build_path(dir) - File.join(APP_CONFIG['root_path'], platform.unixname, dir) + File.join(repository.path, dir) end #TODO: Spec me diff --git a/app/models/repository.rb b/app/models/repository.rb new file mode 100644 index 000000000..79ee09901 --- /dev/null +++ b/app/models/repository.rb @@ -0,0 +1,31 @@ +class Repository < ActiveRecord::Base + belongs_to :platform + has_many :projects, :dependent => :destroy + + validate :name, :presence => true, :uniqueness => true + validate :unixname, :uniqueness => true, :presence => true, :format => { :with => /^[a-zA-Z0-9\-.]+$/ } + + before_create :create_directory + + def path + build_path(unixname) + end + + protected + + def build_path(dir) + File.join(platform.path, 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/show.html.haml b/app/views/platforms/show.html.haml index dfdfb8aa7..fa70e4649 100644 --- a/app/views/platforms/show.html.haml +++ b/app/views/platforms/show.html.haml @@ -7,10 +7,11 @@ = @platform.path -%h2= t('layout.platforms.projects') -- @projects.each do |project| - = div_for project do - = link_to project.name, [@platform, project] -= link_to t('layout.projects.new'), new_platform_project_path(@platform) +%h2= t('layout.platforms.repositories') +- @repositories.each do |repository| + = div_for repository do + = link_to repository.name, [@platform, repository] += link_to t('layout.repositories.new'), new_platform_repository_path(@platform) %h2= t('layout.platforms.products') +TBD diff --git a/app/views/projects/new.html.haml b/app/views/projects/new.html.haml index 039108f4e..931aa3450 100644 --- a/app/views/projects/new.html.haml +++ b/app/views/projects/new.html.haml @@ -1,5 +1,5 @@ %h1= t('layout.projects.new') -= form_for [@platform, @project] do |f| += form_for [@platform, @repository, @project] do |f| %p = f.label :name = f.text_field :name @@ -8,4 +8,4 @@ = f.text_field :unixname %p = f.submit t('layout.create') - = link_to t('layout.cancel'), platform_path(@platform) + = link_to t('layout.cancel'), platform_repository_path(@platform, @repository) diff --git a/app/views/projects/show.html.haml b/app/views/projects/show.html.haml index b15b586d5..43a24d1cf 100644 --- a/app/views/projects/show.html.haml +++ b/app/views/projects/show.html.haml @@ -7,6 +7,6 @@ = t('layout.projects.git_repo_location') = @project.git_repo_path -= link_to t('layout.projects.back_to_the_list'), platform_path(@platform) += link_to t('layout.projects.back_to_the_list'), platform_repository_path(@repository, @platform) %br/ -= link_to "git-repo", platform_project_repo_path(@platform, @project) += link_to "git-repo", platform_repository_project_repo_path(@platform, @repository, @project) diff --git a/app/views/repositories/new.html.haml b/app/views/repositories/new.html.haml new file mode 100644 index 000000000..606f88eaa --- /dev/null +++ b/app/views/repositories/new.html.haml @@ -0,0 +1,12 @@ +%h1= t('layout.repositories.new_header') += form_for [@platform, @repository] do |f| + %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_repositories_path(@platform) + diff --git a/app/views/repositories/show.html.haml b/app/views/repositories/show.html.haml new file mode 100644 index 000000000..1d032c6fd --- /dev/null +++ b/app/views/repositories/show.html.haml @@ -0,0 +1,14 @@ +%h1 + = t('layout.repositories.show') + = @repository.name += link_to t('layout.repositories.back_to_the_list'), platform_path(@platform) +.location + = t('layout.repositories.location') + = @repository.path + + +%h2= t('layout.repositories.projects') +- @projects.each do |project| + = div_for project do + = link_to project.name, [@platform, @repository, project] += link_to t('layout.projects.new'), new_platform_repository_project_path(@platform, @repository) diff --git a/config/routes.rb b/config/routes.rb index 4bab0e2fe..c18cfc28f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,8 +2,10 @@ Rosa::Application.routes.draw do devise_for :users resources :platforms do - resources :projects do - resource :repo, :controller => "git/repositories", :only => [:show] + resources :repositories do + resources :projects do + resource :repo, :controller => "git/repositories", :only => [:show] + end end end diff --git a/db/migrate/20110311154011_create_repositories.rb b/db/migrate/20110311154011_create_repositories.rb new file mode 100644 index 000000000..1396f8c30 --- /dev/null +++ b/db/migrate/20110311154011_create_repositories.rb @@ -0,0 +1,14 @@ +class CreateRepositories < ActiveRecord::Migration + def self.up + create_table :repositories do |t| + t.string :name, :null => false + t.integer :platform_id, :null => false + + t.timestamps + end + end + + def self.down + drop_table :repositories + end +end diff --git a/db/migrate/20110311154044_remove_platform_id_from_projects.rb b/db/migrate/20110311154044_remove_platform_id_from_projects.rb new file mode 100644 index 000000000..7ec739f02 --- /dev/null +++ b/db/migrate/20110311154044_remove_platform_id_from_projects.rb @@ -0,0 +1,9 @@ +class RemovePlatformIdFromProjects < ActiveRecord::Migration + def self.up + remove_column :projects, :platform_id + end + + def self.down + add_column :projects, :platform_id, :integer, :null => false + end +end diff --git a/db/migrate/20110311154136_add_repository_id_to_projects.rb b/db/migrate/20110311154136_add_repository_id_to_projects.rb new file mode 100644 index 000000000..c2099a9f5 --- /dev/null +++ b/db/migrate/20110311154136_add_repository_id_to_projects.rb @@ -0,0 +1,9 @@ +class AddRepositoryIdToProjects < ActiveRecord::Migration + def self.up + add_column :projects, :repository_id, :integer, :null => false + end + + def self.down + remove_column :projects, :repository_id + end +end diff --git a/db/migrate/20110311154929_add_unixname_to_repositories.rb b/db/migrate/20110311154929_add_unixname_to_repositories.rb new file mode 100644 index 000000000..b3907bc14 --- /dev/null +++ b/db/migrate/20110311154929_add_unixname_to_repositories.rb @@ -0,0 +1,9 @@ +class AddUnixnameToRepositories < ActiveRecord::Migration + def self.up + add_column :repositories, :unixname, :string, :null => false + end + + def self.down + remove_column :repositories, :unixname + end +end diff --git a/db/schema.rb b/db/schema.rb index d141342fe..b65fc4c4c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110309173421) do +ActiveRecord::Schema.define(:version => 20110311154929) do create_table "platforms", :force => true do |t| t.string "name" @@ -23,9 +23,17 @@ ActiveRecord::Schema.define(:version => 20110309173421) do create_table "projects", :force => true do |t| t.string "name" t.string "unixname" - t.integer "platform_id" t.datetime "created_at" t.datetime "updated_at" + t.integer "repository_id", :null => false + end + + create_table "repositories", :force => true do |t| + t.string "name", :null => false + t.integer "platform_id", :null => false + t.datetime "created_at" + t.datetime "updated_at" + t.string "unixname", :null => false end create_table "users", :force => true do |t| diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb new file mode 100644 index 000000000..217378744 --- /dev/null +++ b/spec/models/repository_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Repository do + pending "add some examples to (or delete) #{__FILE__}" +end