Commit 95965d3f5417b62ec61fcdd3d7f21fe1f6260993

Authored by Dmitriy Zaporozhets
1 parent 81697850

Added UsersGroup validations. Added ability to add UsersGroup to group via UI

app/controllers/groups_controller.rb
... ... @@ -63,18 +63,19 @@ class GroupsController < ApplicationController
63 63  
64 64 def people
65 65 @project = group.projects.find(params[:project_id]) if params[:project_id]
66   - @users = @project ? @project.users : group.users
67   - @users.sort_by!(&:name)
  66 +
  67 + @users_groups = group.users_groups
68 68  
69 69 if @project
70 70 @team_member = @project.users_projects.new
71 71 else
72   - @team_member = UsersProject.new
  72 + @team_member = UsersGroup.new
73 73 end
74 74 end
75 75  
76 76 def team_members
77   - @group.add_users_to_project_teams(params[:user_ids].split(','), params[:project_access])
  77 + @group.add_users(params[:user_ids].split(','), params[:group_access])
  78 +
78 79 redirect_to people_group_path(@group), notice: 'Users were successfully added.'
79 80 end
80 81  
... ...
app/models/group.rb
... ... @@ -16,6 +16,12 @@ class Group < Namespace
16 16 has_many :users_groups, dependent: :destroy
17 17 has_many :users, through: :users_groups
18 18  
  19 + def add_users(user_ids, group_access)
  20 + user_ids.compact.each do |user_id|
  21 + self.users_groups.create(user_id: user_id, group_access: group_access)
  22 + end
  23 + end
  24 +
19 25 def add_users_to_project_teams(user_ids, project_access)
20 26 UsersProject.add_users_into_projects(
21 27 projects.map(&:id),
... ...
app/models/users_group.rb
... ... @@ -5,6 +5,16 @@ class UsersGroup < ActiveRecord::Base
5 5 MASTER = 40
6 6 OWNER = 50
7 7  
  8 + def self.group_access_roles
  9 + {
  10 + "Guest" => GUEST,
  11 + "Reporter" => REPORTER,
  12 + "Developer" => DEVELOPER,
  13 + "Master" => MASTER,
  14 + "Owner" => OWNER
  15 + }
  16 + end
  17 +
8 18 attr_accessible :group_access, :group_id, :user_id
9 19  
10 20 belongs_to :user
... ... @@ -18,4 +28,12 @@ class UsersGroup < ActiveRecord::Base
18 28  
19 29 scope :with_group, ->(group) { where(group_id: group.id) }
20 30 scope :with_user, ->(user) { where(user_id: user.id) }
  31 +
  32 + validates :group_access, inclusion: { in: UsersGroup.group_access_roles.values }, presence: true
  33 + validates :user_id, presence: true
  34 + validates :group_id, presence: true
  35 +
  36 + def human_group_access
  37 + UsersGroup.group_access_roles.index(self.group_access)
  38 + end
21 39 end
... ...
app/views/groups/_new_group_member.html.haml
... ... @@ -9,10 +9,10 @@
9 9  
10 10 %h6 2. Set access level for them
11 11 .clearfix
12   - = f.label :project_access, "Project Access"
13   - .input= select_tag :project_access, options_for_select(Project.access_options, @team_member.project_access), class: "project-access-select chosen"
  12 + = f.label :group_access, "Group Access"
  13 + .input= select_tag :group_access, options_for_select(UsersGroup.group_access_roles, @team_member.group_access), class: "project-access-select chosen"
14 14  
15 15 .form-actions
16 16 = hidden_field_tag :redirect_to, people_group_path(@group)
17   - = f.submit 'Add', class: "btn btn-save"
  17 + = f.submit 'Add users into group', class: "btn btn-create"
18 18  
... ...
app/views/groups/people.html.haml
... ... @@ -8,13 +8,18 @@
8 8 %h5.title
9 9 Team
10 10 %small
11   - (#{@users.size})
  11 + (#{@users_groups.count})
12 12 %ul.well-list
13   - - @users.each do |user|
  13 + - @users_groups.each do |users_group|
  14 + - user = users_group.user
14 15 %li
15 16 = image_tag gravatar_icon(user.email, 16), class: "avatar s16"
16 17 %strong= user.name
17   - %span.cgray= user.email
18   - - if @group.owner == user
19   - %span.btn.btn-small.disabled.pull-right Group Owner
  18 + %span.cgray= user.username
  19 +
  20 + %span.pull-right
  21 + - if @group.owners.include?(user)
  22 + %span.label.label-info Group Owner
  23 + - else
  24 + = users_group.human_group_access
20 25  
... ...
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 => 20130614132337) do
  14 +ActiveRecord::Schema.define(:version => 20130617095603) do
15 15  
16 16 create_table "deploy_keys_projects", :force => true do |t|
17 17 t.integer "deploy_key_id", :null => false
... ... @@ -53,8 +53,8 @@ ActiveRecord::Schema.define(:version => 20130614132337) do
53 53 t.integer "assignee_id"
54 54 t.integer "author_id"
55 55 t.integer "project_id"
56   - t.datetime "created_at"
57   - t.datetime "updated_at"
  56 + t.datetime "created_at", :null => false
  57 + t.datetime "updated_at", :null => false
58 58 t.integer "position", :default => 0
59 59 t.string "branch_name"
60 60 t.text "description"
... ... @@ -71,8 +71,8 @@ ActiveRecord::Schema.define(:version => 20130614132337) do
71 71  
72 72 create_table "keys", :force => true do |t|
73 73 t.integer "user_id"
74   - t.datetime "created_at"
75   - t.datetime "updated_at"
  74 + t.datetime "created_at", :null => false
  75 + t.datetime "updated_at", :null => false
76 76 t.text "key"
77 77 t.string "title"
78 78 t.string "identifier"
... ... @@ -89,8 +89,8 @@ ActiveRecord::Schema.define(:version => 20130614132337) do
89 89 t.integer "author_id"
90 90 t.integer "assignee_id"
91 91 t.string "title"
92   - t.datetime "created_at"
93   - t.datetime "updated_at"
  92 + t.datetime "created_at", :null => false
  93 + t.datetime "updated_at", :null => false
94 94 t.text "st_commits", :limit => 2147483647
95 95 t.text "st_diffs", :limit => 2147483647
96 96 t.integer "milestone_id"
... ... @@ -139,8 +139,8 @@ ActiveRecord::Schema.define(:version => 20130614132337) do
139 139 t.text "note"
140 140 t.string "noteable_type"
141 141 t.integer "author_id"
142   - t.datetime "created_at"
143   - t.datetime "updated_at"
  142 + t.datetime "created_at", :null => false
  143 + t.datetime "updated_at", :null => false
144 144 t.integer "project_id"
145 145 t.string "attachment"
146 146 t.string "line_code"
... ... @@ -158,8 +158,8 @@ ActiveRecord::Schema.define(:version => 20130614132337) do
158 158 t.string "name"
159 159 t.string "path"
160 160 t.text "description"
161   - t.datetime "created_at"
162   - t.datetime "updated_at"
  161 + t.datetime "created_at", :null => false
  162 + t.datetime "updated_at", :null => false
163 163 t.integer "creator_id"
164 164 t.string "default_branch"
165 165 t.boolean "issues_enabled", :default => true, :null => false
... ... @@ -206,8 +206,8 @@ ActiveRecord::Schema.define(:version => 20130614132337) do
206 206 t.text "content"
207 207 t.integer "author_id", :null => false
208 208 t.integer "project_id"
209   - t.datetime "created_at"
210   - t.datetime "updated_at"
  209 + t.datetime "created_at", :null => false
  210 + t.datetime "updated_at", :null => false
211 211 t.string "file_name"
212 212 t.datetime "expires_at"
213 213 t.boolean "private", :default => true, :null => false
... ... @@ -228,6 +228,9 @@ ActiveRecord::Schema.define(:version => 20130614132337) do
228 228 t.datetime "created_at"
229 229 end
230 230  
  231 + add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
  232 + add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context"
  233 +
231 234 create_table "tags", :force => true do |t|
232 235 t.string "name"
233 236 end
... ... @@ -259,52 +262,61 @@ ActiveRecord::Schema.define(:version => 20130614132337) do
259 262 end
260 263  
261 264 create_table "users", :force => true do |t|
262   - t.string "email", :default => "", :null => false
263   - t.string "encrypted_password", :limit => 128, :default => "", :null => false
  265 + t.string "email", :default => "", :null => false
  266 + t.string "encrypted_password", :default => "", :null => false
264 267 t.string "reset_password_token"
265 268 t.datetime "reset_password_sent_at"
266 269 t.datetime "remember_created_at"
267   - t.integer "sign_in_count", :default => 0
  270 + t.integer "sign_in_count", :default => 0
268 271 t.datetime "current_sign_in_at"
269 272 t.datetime "last_sign_in_at"
270 273 t.string "current_sign_in_ip"
271 274 t.string "last_sign_in_ip"
272   - t.datetime "created_at"
273   - t.datetime "updated_at"
  275 + t.datetime "created_at", :null => false
  276 + t.datetime "updated_at", :null => false
274 277 t.string "name"
275   - t.boolean "admin", :default => false, :null => false
276   - t.integer "projects_limit", :default => 10
277   - t.string "skype", :default => "", :null => false
278   - t.string "linkedin", :default => "", :null => false
279   - t.string "twitter", :default => "", :null => false
  278 + t.boolean "admin", :default => false, :null => false
  279 + t.integer "projects_limit", :default => 10
  280 + t.string "skype", :default => "", :null => false
  281 + t.string "linkedin", :default => "", :null => false
  282 + t.string "twitter", :default => "", :null => false
280 283 t.string "authentication_token"
281   - t.integer "theme_id", :default => 1, :null => false
  284 + t.integer "theme_id", :default => 1, :null => false
282 285 t.string "bio"
283   - t.integer "failed_attempts", :default => 0
  286 + t.integer "failed_attempts", :default => 0
284 287 t.datetime "locked_at"
285 288 t.string "extern_uid"
286 289 t.string "provider"
287 290 t.string "username"
288   - t.boolean "can_create_group", :default => true, :null => false
289   - t.boolean "can_create_team", :default => true, :null => false
  291 + t.boolean "can_create_group", :default => true, :null => false
  292 + t.boolean "can_create_team", :default => true, :null => false
290 293 t.string "state"
291   - t.integer "color_scheme_id", :default => 1, :null => false
292   - t.integer "notification_level", :default => 1, :null => false
  294 + t.integer "color_scheme_id", :default => 1, :null => false
  295 + t.integer "notification_level", :default => 1, :null => false
293 296 t.datetime "password_expires_at"
294 297 t.integer "created_by_id"
295 298 end
296 299  
297 300 add_index "users", ["admin"], :name => "index_users_on_admin"
298 301 add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  302 + add_index "users", ["extern_uid", "provider"], :name => "index_users_on_extern_uid_and_provider", :unique => true
299 303 add_index "users", ["name"], :name => "index_users_on_name"
300 304 add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
301 305 add_index "users", ["username"], :name => "index_users_on_username"
302 306  
  307 + create_table "users_groups", :force => true do |t|
  308 + t.integer "access_level", :null => false
  309 + t.integer "group_id", :null => false
  310 + t.integer "user_id", :null => false
  311 + t.datetime "created_at", :null => false
  312 + t.datetime "updated_at", :null => false
  313 + end
  314 +
303 315 create_table "users_projects", :force => true do |t|
304 316 t.integer "user_id", :null => false
305 317 t.integer "project_id", :null => false
306   - t.datetime "created_at"
307   - t.datetime "updated_at"
  318 + t.datetime "created_at", :null => false
  319 + t.datetime "updated_at", :null => false
308 320 t.integer "project_access", :default => 0, :null => false
309 321 t.integer "notification_level", :default => 3, :null => false
310 322 end
... ... @@ -316,8 +328,8 @@ ActiveRecord::Schema.define(:version => 20130614132337) do
316 328 create_table "web_hooks", :force => true do |t|
317 329 t.string "url"
318 330 t.integer "project_id"
319   - t.datetime "created_at"
320   - t.datetime "updated_at"
  331 + t.datetime "created_at", :null => false
  332 + t.datetime "updated_at", :null => false
321 333 t.string "type", :default => "ProjectHook"
322 334 t.integer "service_id"
323 335 end
... ...