[issue #64] Added validation of own_projects count.

This commit is contained in:
George Vinogradov 2012-01-27 03:37:19 +04:00
parent 1498e937f6
commit ba84d5f216
4 changed files with 6 additions and 2 deletions

View File

@ -1,4 +1,5 @@
class Group < ActiveRecord::Base class Group < ActiveRecord::Base
MAX_OWN_PROJECTS = 32000
belongs_to :owner, :class_name => 'User' belongs_to :owner, :class_name => 'User'
has_many :own_projects, :as => :owner, :class_name => 'Project' has_many :own_projects, :as => :owner, :class_name => 'Project'
@ -15,6 +16,7 @@ class Group < ActiveRecord::Base
validates :name, :owner, :presence => true validates :name, :owner, :presence => true
validates :uname, :presence => true, :uniqueness => {:case_sensitive => false}, :format => { :with => /^[a-z0-9_]+$/ } 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? } validate { errors.add(:uname, :taken) if User.where('uname LIKE ?', uname).present? }
validate { errors.add(:own_projects_count, :less_than_or_equal_to, :count => MAX_OWN_PROJECTS) if own_projects.size >= MAX_OWN_PROJECTS }
attr_readonly :uname, :own_projects_count attr_readonly :uname, :own_projects_count

View File

@ -2,7 +2,7 @@ class Project < ActiveRecord::Base
VISIBILITIES = ['open', 'hidden'] VISIBILITIES = ['open', 'hidden']
belongs_to :category, :counter_cache => true belongs_to :category, :counter_cache => true
belongs_to :owner, :polymorphic => true, :counter_cache => :own_projects_count belongs_to :owner, :polymorphic => true, :validate => true, :counter_cache => :own_projects_count
has_many :issues, :dependent => :destroy has_many :issues, :dependent => :destroy
has_many :build_lists, :dependent => :destroy has_many :build_lists, :dependent => :destroy

View File

@ -1,4 +1,5 @@
class User < ActiveRecord::Base class User < ActiveRecord::Base
MAX_OWN_PROJECTS = 32000
ROLES = ['admin'] ROLES = ['admin']
devise :database_authenticatable, :registerable, :omniauthable, # :token_authenticatable, :encryptable, :timeoutable devise :database_authenticatable, :registerable, :omniauthable, # :token_authenticatable, :encryptable, :timeoutable
@ -25,6 +26,7 @@ class User < ActiveRecord::Base
validates :uname, :presence => true, :uniqueness => {:case_sensitive => false}, :format => { :with => /^[a-z0-9_]+$/ } validates :uname, :presence => true, :uniqueness => {:case_sensitive => false}, :format => { :with => /^[a-z0-9_]+$/ }
validate { errors.add(:uname, :taken) if Group.where('uname LIKE ?', uname).present? } validate { errors.add(:uname, :taken) if Group.where('uname LIKE ?', uname).present? }
validates :ssh_key, :uniqueness => true, :allow_blank => true validates :ssh_key, :uniqueness => true, :allow_blank => true
validate { errors.add(:own_projects_count, :less_than_or_equal_to, :count => MAX_OWN_PROJECTS) if own_projects.size >= MAX_OWN_PROJECTS }
validates :role, :inclusion => {:in => ROLES}, :allow_blank => true validates :role, :inclusion => {:in => ROLES}, :allow_blank => true
attr_accessible :email, :password, :password_confirmation, :remember_me, :login, :name, :ssh_key, :uname attr_accessible :email, :password, :password_confirmation, :remember_me, :login, :name, :ssh_key, :uname

View File

@ -1,6 +1,6 @@
class AddHasWikiToProjects < ActiveRecord::Migration class AddHasWikiToProjects < ActiveRecord::Migration
def self.up def self.up
add_column :projects, :has_wiki, :boolean add_column :projects, :has_wiki, :boolean, :default => false
end end
def self.down def self.down