From eccb4e0a6c6a1bc531e3e8498a57a9e5e5ceaa01 Mon Sep 17 00:00:00 2001 From: Alexey Nayden Date: Thu, 10 Mar 2011 16:38:50 +0300 Subject: [PATCH] Project/platform management --- app/controllers/platforms_controller.rb | 4 +++- app/controllers/projects_controller.rb | 23 +++++++++++++++++++++++ app/models/platform.rb | 10 +++------- app/models/project.rb | 18 +++++++++--------- app/models/project/has_repository.rb | 4 ++-- app/views/platforms/index.html.haml | 2 +- app/views/platforms/new.html.haml | 11 +++++++++++ app/views/platforms/show.html.haml | 11 +++++++++++ app/views/projects/new.html.haml | 8 ++++++++ config/locales/ru.yml | 16 ++++++++++++++++ 10 files changed, 87 insertions(+), 20 deletions(-) create mode 100644 app/views/platforms/new.html.haml create mode 100644 app/views/platforms/show.html.haml create mode 100644 app/views/projects/new.html.haml diff --git a/app/controllers/platforms_controller.rb b/app/controllers/platforms_controller.rb index 7a6c127fa..abf094934 100644 --- a/app/controllers/platforms_controller.rb +++ b/app/controllers/platforms_controller.rb @@ -6,10 +6,12 @@ class PlatformsController < ApplicationController end def show - @platform = Platform.find params[:id] + @platform = Platform.find params[:id], :include => :projects + @projects = @platform.projects end def new + @platforms = Platform.all @platform = Platform.new end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 60b4a9e37..4c83600af 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -1,2 +1,25 @@ class ProjectsController < ApplicationController + before_filter :authenticate_user! + before_filter :find_platform + + def new + @project = @platform.projects.new + end + + def create + @project = @platform.projects.new params[:project] + if @project.save + flash[:notice] = t('flash.project.saved') + redirect_to @platform + else + flash[:error] = t('flash.project.save_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 90bd5d1bf..685a9a1a6 100644 --- a/app/models/platform.rb +++ b/app/models/platform.rb @@ -3,18 +3,14 @@ class Platform < ActiveRecord::Base has_one :parent, :class_name => 'Platform', :foreign_key => 'parent_platform_id' validate :name, :presence => true, :uniqueness => true - validate :unixname, :presence => true, :uniqueness => true - validate :validate_unixname + validate :unixname, :uniqueness => true, :presence => true, :format => { :with => /^[a-zA-Z0-9\-.]+$/ }, :allow_nil => false, :allow_blank => false before_validation :generate_unixname protected def generate_unixname - #TODO: Implement unixname generation - end - - def validate_unixname - #TODO: Implement unixname validation + self.unixname = name.gsub(/[^a-zA-Z0-9\-.]/, '-') + #TODO: Fix non-unique unixname end end diff --git a/app/models/project.rb b/app/models/project.rb index 43a42232f..1d2118c90 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,22 +1,22 @@ class Project < ActiveRecord::Base belongs_to :platform - validate :name, :uniqueness => true, :presence => true - validate :unixname, :uniqueness => true, :presence => true - validate :validate_unixname + 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 + # 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, project.unixname, project.unixname + '.git') + end + protected def generate_unixname - #TODO: Implement unixname generation + self.unixname = name.gsub(/[^a-zA-Z0-9\-.]/, '-') + #TODO: Fix non-unique unixname end - - def validate_unixname - #TODO: Implement unixname validation - end - end diff --git a/app/models/project/has_repository.rb b/app/models/project/has_repository.rb index a109297a2..064740e69 100644 --- a/app/models/project/has_repository.rb +++ b/app/models/project/has_repository.rb @@ -9,6 +9,6 @@ module Project::HasRepository protected def git_repo_path - @git_repo_path ||= "xxx" + @git_repo_path ||= File.join(APP_CONFIG['root_path'], platform.unixname, project.unixname, project.unixname + '.git') end -end \ No newline at end of file +end diff --git a/app/views/platforms/index.html.haml b/app/views/platforms/index.html.haml index e3d186060..84dce1cee 100644 --- a/app/views/platforms/index.html.haml +++ b/app/views/platforms/index.html.haml @@ -1,7 +1,7 @@ %h1= t('layout.platforms.list') - @platforms.each do |platform| = div_for platform do - = platform.name + = link_to platform.name, platform = link_to t('layout.platforms.new'), new_platform_path .div = link_to t('layout.user_list'), users_path diff --git a/app/views/platforms/new.html.haml b/app/views/platforms/new.html.haml new file mode 100644 index 000000000..46e4a60d9 --- /dev/null +++ b/app/views/platforms/new.html.haml @@ -0,0 +1,11 @@ +%h1= t('layout.platforms.new_header') += form_for @platform do |f| + %p + = f.label :name + = f.text_field :name + %p + = f.label :parent_platform_id + = f.select :parent_platform_id, @platforms.map { |p| [p.name, p.id] }, :include_blank => true + %p + = f.submit t('layout.create') + = link_to t('layout.cancel'), platforms_path diff --git a/app/views/platforms/show.html.haml b/app/views/platforms/show.html.haml new file mode 100644 index 000000000..c5325a14e --- /dev/null +++ b/app/views/platforms/show.html.haml @@ -0,0 +1,11 @@ +%h1 + = t('layout.platforms.show') + = @platform.name + +%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.products') diff --git a/app/views/projects/new.html.haml b/app/views/projects/new.html.haml new file mode 100644 index 000000000..706d28291 --- /dev/null +++ b/app/views/projects/new.html.haml @@ -0,0 +1,8 @@ +%h1= t('layout.projects.new') += form_for [@platform, @project] do |f| + %p + = f.label :name + = f.text_field :name + %p + = f.submit t('layout.create') + = link_to t('layout.cancel'), platform_path(@platform) diff --git a/config/locales/ru.yml b/config/locales/ru.yml index 9154e0cd3..17ef24b5b 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -4,10 +4,26 @@ ru: logged_in_as: Вы вошли как logout: Выйти user_list: Список пользователей + cancel: Отмена + create: Создать + save: Сохранить platforms: list: Платформы new: Создать новую платформу + new_header: Новая платформа + show: Платформа + projects: Проекты + products: Продукты + projects: + new: Новый проект + + flash: + project: + saved: Проект успешно сохранен + save_error: Не удалось сохранить проект attributes: password: Пароль remember_me: Запомнить + name: Название + parent_platform_id: Родительская платформа