From 72fec3b314616e8bfc27193880c358715e7ab681 Mon Sep 17 00:00:00 2001 From: "konstantin.grabar" Date: Mon, 27 Feb 2012 13:03:28 +0400 Subject: [PATCH] [refs #195] Add owner choose on project create --- app/controllers/projects_controller.rb | 12 ++++++++++-- app/models/group.rb | 7 +++++++ app/views/projects/_form.html.haml | 10 ++++++++++ config/locales/en.yml | 3 +++ config/locales/ru.yml | 3 +++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 91ad5ff98..f3d2d7e76 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -42,8 +42,8 @@ class ProjectsController < ApplicationController def create @project = Project.new params[:project] - @project.owner = get_owner -# puts @project.owner.inspect + #@project.owner = get_owner + @project.owner = choose_owner if @project.save flash[:notice] = t('flash.project.saved') @@ -116,4 +116,12 @@ class ProjectsController < ApplicationController def find_project @project = Project.find params[:id] end + + def choose_owner + if params[:who_owns] == 'group' + Group.find(params[:owner_id]) + else + current_user + end + end end diff --git a/app/models/group.rb b/app/models/group.rb index d74161e25..6242822bb 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -17,6 +17,9 @@ class Group < ActiveRecord::Base validates :uname, :presence => true, :uniqueness => {:case_sensitive => false}, :format => { :with => /^[a-z0-9_]+$/ } validate { errors.add(:uname, :taken) if User.where('uname LIKE ?', uname).present? } + scope :by_owner, lambda { |owner| where(:owner_id => owner.id) } + scope :by_admin, lambda { |admin| joins(:relations).where(:'relations.role' => 'admin', :'relations.target_id' => admin.id, :'relations.target_type' => 'User') } + attr_readonly :uname, :own_projects_count delegate :ssh_key, :email, :to => :owner @@ -26,6 +29,10 @@ class Group < ActiveRecord::Base include Modules::Models::PersonalRepository # include Modules::Models::Owner + def self.can_own_project(user) + (by_owner(user) | by_admin(user)).collect { |el| [el.name, el.id] } + end + protected def add_owner_to_members Relation.create_with_role(self.owner, self, 'admin') diff --git a/app/views/projects/_form.html.haml b/app/views/projects/_form.html.haml index 5689ae253..5c9cda5f1 100644 --- a/app/views/projects/_form.html.haml +++ b/app/views/projects/_form.html.haml @@ -10,6 +10,16 @@ .rightlist = f.text_area :description, :class => 'text_field', :cols => 80 .both +- if ['new', 'create'].include? controller.action_name + .leftlist + = f.label :owner_id, t("activerecord.attributes.project.owner"), :class => :label + .rightlist + = label_tag t("activerecord.attributes.project.who_owns.me") + = radio_button_tag :who_owns, 'me' + = label_tag t("activerecord.attributes.project.who_owns.group") + = radio_button_tag :who_owns, 'group' + = select_tag :owner_id, options_for_select( Group.can_own_project(current_user) ) + .both -#- if [:new, :create].include? act -# .leftlist -# = f.label :category_id, t("activerecord.attributes.project.category_id"), :class => :label diff --git a/config/locales/en.yml b/config/locales/en.yml index 5775a2aae..48c6a80ac 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -834,6 +834,9 @@ en: has_issues: Tracker on has_wiki: Wiki on srpm: Import code from src.rpm + who_owns: + me: I + group: Group rpm: name: Name diff --git a/config/locales/ru.yml b/config/locales/ru.yml index fa13aadaa..7cad2ffdb 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -696,6 +696,9 @@ ru: has_issues: Включить трэкер has_wiki: Включить Wiki srpm: Импортировать код из src.rpm + who_owns: + me: Я + group: Группа rpm: name: Название