Commit 67f0c62d0737ceeeb1babc953fbab41438e8d4f2

Authored by Valera Sizov
1 parent 98401026

Issue #83 - Project limit

app/assets/stylesheets/projects.css.scss
... ... @@ -521,3 +521,7 @@ tbody tr:nth-child(2n) td, tbody tr.even td {
521 521 width:400px;
522 522 }
523 523 }
  524 +
  525 +#user_projects_limit{
  526 + width: 60px;
  527 +}
... ...
app/controllers/admin/users_controller.rb
... ... @@ -21,7 +21,7 @@ class Admin::UsersController < ApplicationController
21 21 end
22 22  
23 23 def new
24   - @admin_user = User.new
  24 + @admin_user = User.new(:projects_limit => 10)
25 25  
26 26 respond_to do |format|
27 27 format.html # new.html.erb
... ...
app/models/project.rb
... ... @@ -25,6 +25,8 @@ class Project < ActiveRecord::Base
25 25 :uniqueness => true,
26 26 :length => { :within => 3..12 }
27 27  
  28 + validate :check_limit
  29 +
28 30 before_save :format_code
29 31 after_destroy :destroy_gitosis_project
30 32 after_save :update_gitosis_project
... ... @@ -126,6 +128,12 @@ class Project < ActiveRecord::Base
126 128 path ? (tree / path) : tree
127 129 end
128 130  
  131 + def check_limit
  132 + unless owner.can_create_project?
  133 + errors[:base] << ("You can to have #{owner.projects_limit} your own projects")
  134 + end
  135 + end
  136 +
129 137 def valid_repo?
130 138 repo
131 139 rescue
... ...
app/models/user.rb
... ... @@ -5,7 +5,7 @@ class User &lt; ActiveRecord::Base
5 5 :recoverable, :rememberable, :trackable, :validatable
6 6  
7 7 # Setup accessible (or protected) attributes for your model
8   - attr_accessible :email, :password, :password_confirmation, :remember_me, :name
  8 + attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :projects_limit
9 9  
10 10 has_many :users_projects, :dependent => :destroy
11 11 has_many :projects, :through => :users_projects
... ... @@ -29,6 +29,10 @@ class User &lt; ActiveRecord::Base
29 29 def is_admin?
30 30 admin
31 31 end
  32 +
  33 + def can_create_project?
  34 + projects_limit >= my_own_projects.count
  35 + end
32 36 end
33 37 # == Schema Information
34 38 #
... ... @@ -49,6 +53,6 @@ end
49 53 # updated_at :datetime
50 54 # name :string(255)
51 55 # admin :boolean default(FALSE), not null
52   -# allowed_create_repo :boolean default(TRUE), not null
  56 +# projects_limit :integer
53 57 #
54 58  
... ...
app/views/admin/users/_form.html.haml
... ... @@ -30,8 +30,8 @@
30 30 = f.check_box :admin
31 31 = f.label :admin
32 32 .field.prepend-top
33   - = f.check_box :allowed_create_repo, :disabled => true
34   - = f.label :allowed_create_repo
  33 + = f.text_field :projects_limit, :class => "small_input"
  34 + = f.label :projects_limit
35 35 .clear
36 36 %br
37 37 .actions
... ...
app/views/admin/users/show.html.haml
... ... @@ -10,6 +10,9 @@
10 10 %p
11 11 %b Admin:
12 12 = @admin_user.admin
  13 + %p
  14 + %b Projects limit:
  15 + = @admin_user.projects_limit
13 16  
14 17 .clear
15 18 = link_to 'Edit', edit_admin_user_path(@admin_user)
... ...
app/views/projects/index.html.haml
1   -= link_to 'New Project', new_project_path, :class => "lbutton vm"
  1 +- if current_user.can_create_project?
  2 + = link_to 'New Project', new_project_path, :class => "lbutton vm"
2 3  
3 4 %table.round-borders#projects-list
4 5 %tr
... ...
db/fixtures/development/001_admin.rb
... ... @@ -6,5 +6,6 @@ admin = User.create(
6 6 :password_confirmation => "5iveL!fe"
7 7 )
8 8  
  9 +admin.projects_limit = 10000
9 10 admin.admin = true
10 11 admin.save!
... ...
db/fixtures/production/001_admin.rb
... ... @@ -5,5 +5,6 @@ admin = User.create(
5 5 :password_confirmation => "5iveL!fe"
6 6 )
7 7  
  8 +admin.projects_limit = 10000
8 9 admin.admin = true
9 10 admin.save!
... ...
db/migrate/20111009110913_add_projects_limit_to_user.rb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +class AddProjectsLimitToUser < ActiveRecord::Migration
  2 + def change
  3 + add_column :users, :projects_limit, :integer, :default => 10
  4 + end
  5 +end
... ...
db/migrate/20111009111204_remove_allow_create_repo_from_user.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +class RemoveAllowCreateRepoFromUser < ActiveRecord::Migration
  2 + def up
  3 + remove_column :users, :allowed_create_repo
  4 + end
  5 +
  6 + def down
  7 + add_column :users, :allowed_create_repo, :boolean, :default => true, :null => false
  8 + end
  9 +end
... ...
db/schema.rb
... ... @@ -11,7 +11,7 @@
11 11 #
12 12 # It's strongly recommended to check this file into your version control system.
13 13  
14   -ActiveRecord::Schema.define(:version => 20111009101738) do
  14 +ActiveRecord::Schema.define(:version => 20111009111204) do
15 15  
16 16 create_table "issues", :force => true do |t|
17 17 t.string "title"
... ... @@ -70,7 +70,7 @@ ActiveRecord::Schema.define(:version =&gt; 20111009101738) do
70 70 t.datetime "updated_at"
71 71 t.string "name"
72 72 t.boolean "admin", :default => false, :null => false
73   - t.boolean "allowed_create_repo", :default => true, :null => false
  73 + t.integer "projects_limit"
74 74 end
75 75  
76 76 add_index "users", ["email"], :name => "index_users_on_email", :unique => true
... ...
spec/models/user_spec.rb
... ... @@ -38,6 +38,6 @@ end
38 38 # updated_at :datetime
39 39 # name :string(255)
40 40 # admin :boolean default(FALSE), not null
41   -# allowed_create_repo :boolean default(TRUE), not null
  41 +# projects_limit :integer
42 42 #
43 43  
... ...