From 80304ef7f2828ca0d08b4afbb78a4666645bc3ae Mon Sep 17 00:00:00 2001 From: Alexey Nayden Date: Fri, 11 Mar 2011 19:08:41 +0300 Subject: [PATCH] Trying to add repositories to my hierarchy --- app/controllers/platforms_controller.rb | 4 +- app/controllers/projects_controller.rb | 7 ++- app/controllers/repositories_controller.rb | 53 +++++++++++++++++++ app/models/platform.rb | 2 +- app/models/project.rb | 6 +-- app/models/repository.rb | 31 +++++++++++ .../20110311154011_create_repositories.rb | 14 +++++ ...154044_remove_platform_id_from_projects.rb | 9 ++++ ...311154136_add_repository_id_to_projects.rb | 9 ++++ ...0311154929_add_unixname_to_repositories.rb | 9 ++++ db/schema.rb | 12 ++++- spec/models/repository_spec.rb | 5 ++ 12 files changed, 152 insertions(+), 9 deletions(-) create mode 100644 app/controllers/repositories_controller.rb create mode 100644 app/models/repository.rb create mode 100644 db/migrate/20110311154011_create_repositories.rb create mode 100644 db/migrate/20110311154044_remove_platform_id_from_projects.rb create mode 100644 db/migrate/20110311154136_add_repository_id_to_projects.rb create mode 100644 db/migrate/20110311154929_add_unixname_to_repositories.rb create mode 100644 spec/models/repository_spec.rb 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..6a1643514 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -1,6 +1,7 @@ class ProjectsController < ApplicationController before_filter :authenticate_user! before_filter :find_platform + before_filter :find_repository before_filter :find_project, :only => [:show] def new @@ -27,7 +28,11 @@ class ProjectsController < ApplicationController @platform = Platform.find params[:platform_id] end + def find_repository + @repository = @platform.find parmas[: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..eabb00889 --- /dev/null +++ b/app/controllers/repositories_controller.rb @@ -0,0 +1,53 @@ +class RepositoriesController < ApplicationController::Base + 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[:id] + if @repository.save + flash[:notice] = '' + redirect_to @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..c0f9e9d65 --- /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\-.]+$/ }, :allow_nil => false, :allow_blank => false + + 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/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