Trying to add repositories to my hierarchy

This commit is contained in:
Alexey Nayden 2011-03-11 19:08:41 +03:00
parent 3a3930b1b1
commit 80304ef7f2
12 changed files with 152 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

31
app/models/repository.rb Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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|

View File

@ -0,0 +1,5 @@
require 'spec_helper'
describe Repository do
pending "add some examples to (or delete) #{__FILE__}"
end