[issue #64] Added own_projects counters to User & Group

This commit is contained in:
George Vinogradov 2012-01-27 02:33:18 +04:00
parent b04a9e0176
commit 1498e937f6
6 changed files with 34 additions and 4 deletions

View File

@ -16,7 +16,7 @@ class Group < 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 User.where('uname LIKE ?', uname).present? } validate { errors.add(:uname, :taken) if User.where('uname LIKE ?', uname).present? }
attr_readonly :uname attr_readonly :uname, :own_projects_count
delegate :ssh_key, :to => :owner delegate :ssh_key, :to => :owner

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 belongs_to :owner, :polymorphic => 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

@ -28,7 +28,7 @@ class User < ActiveRecord::Base
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
attr_readonly :uname attr_readonly :uname, :own_projects_count
attr_accessor :login attr_accessor :login
def admin? def admin?

View File

@ -0,0 +1,14 @@
class AddProjectsCountToUsers < ActiveRecord::Migration
def self.up
add_column :users, :own_projects_count, :integer, :default => 0, :null => false
User.reset_column_information
User.all.each do |user|
User.reset_counters(user.id, :own_projects)
end
end
def self.down
remove_column :users, :own_projects_count
end
end

View File

@ -0,0 +1,14 @@
class AddProjectsCountToGroups < ActiveRecord::Migration
def self.up
add_column :groups, :own_projects_count, :integer, :default => 0, :null => false
Group.reset_column_information
Group.all.each do |group|
Group.reset_counters(group.id, :own_projects)
end
end
def self.down
remove_column :groups, :projects_count
end
end

View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20111228182425) do ActiveRecord::Schema.define(:version => 20120126214447) do
create_table "arches", :force => true do |t| create_table "arches", :force => true do |t|
t.string "name", :null => false t.string "name", :null => false
@ -150,6 +150,7 @@ ActiveRecord::Schema.define(:version => 20111228182425) do
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.string "uname" t.string "uname"
t.integer "own_projects_count", :default => 0, :null => false
end end
create_table "issues", :force => true do |t| create_table "issues", :force => true do |t|
@ -294,6 +295,7 @@ ActiveRecord::Schema.define(:version => 20111228182425) do
t.text "ssh_key" t.text "ssh_key"
t.string "uname" t.string "uname"
t.string "role" t.string "role"
t.integer "own_projects_count", :default => 0, :null => false
end end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true add_index "users", ["email"], :name => "index_users_on_email", :unique => true