[refs #232] Add new design and logic refactor for group members action

This commit is contained in:
konstantin.grabar 2012-03-01 19:41:40 +04:00
parent fc3d4c0b6d
commit eb689b7f18
4 changed files with 35 additions and 86 deletions

View File

@ -29,50 +29,53 @@ class MembersController < ApplicationController
end end
def update def update
all_user_ids = [] params['user'].keys.each { |user_id|
Relation::ROLES.each { |r| role = params['user'][user_id]
all_user_ids = all_user_ids | params[r.to_sym].keys if params[r.to_sym]
}
# Remove relations if relation = parent.objects.find_by_object_id_and_object_type(user_id, 'User')
users_for_removing = parent.members.select do |u| relation.update_attribute(:role, role)
!all_user_ids.map{|k| k.to_i}.include? u.id and parent.owner != u else
end relation = parent.objects.build(:object_id => user_id, :object_type => 'User', :role => role)
users_for_removing.each do |u| relation.save!
Relation.by_object(u).by_target(parent).each {|r| r.destroy} end
end } if params['user']
# Create relations
Relation::ROLES.each { |r|
#users_for_creating = users_for_creating params[:user].keys.map{|p| p.to_i} - @project.collaborators.map(&:id)
params[r.to_sym].keys.each { |u|
if relation = parent.objects.find_by_object_id_and_object_type(u, 'User')
relation.update_attribute(:role, r)
else
relation = parent.objects.build(:object_id => u, :object_type => 'User', :role => r)
puts relation.inspect
puts r
relation.save!
end
} if params[r.to_sym]
}
if parent.save if parent.save
flash[:notice] = t("flash.members.successfully_changed") flash[:notice] = t("flash.members.successfully_changed")
else else
flash[:error] = t("flash.members.error_in_changing") flash[:error] = t("flash.members.error_in_changing")
end end
redirect_to parent_path
redirect_to edit_group_members_path(parent)
end end
def remove def remove
if params[:id]
u = User.find(params[:id])
Relation.by_object(u).by_target(parent)[0].destroy
redirect_to groups_path
else
all_user_ids = []
params['user_remove'].keys.each { |user_id|
all_user_ids << user_id if params['user_remove'][user_id] == ["1"]
} if params['user_remove']
all_user_ids.each do |user_id|
u = User.find(user_id)
Relation.by_object(u).by_target(parent).each {|r| r.destroy}
end
redirect_to edit_group_members_path(parent)
end
end end
def add def add
if params['user_id'] and !params['user_id'].empty? if params['user_id'] and !params['user_id'].empty?
@user = User.find_by_uname(params['user_id']) @user = User.find_by_uname(params['user_id'])
unless parent.objects.exists? :object_id => @user.id, :object_type => 'User' unless parent.objects.exists? :object_id => @user.id, :object_type => 'User'
relation = parent.objects.build(:object_id => @user.id, :object_type => 'User', :role => 'reader') relation = parent.objects.build(:object_id => @user.id, :object_type => 'User', :role => params[:role])
if relation.save if relation.save
flash[:notice] = t("flash.members.successfully_added") flash[:notice] = t("flash.members.successfully_added")
else else

View File

@ -11,7 +11,7 @@
%td= link_to group.name, group_path(group) %td= link_to group.name, group_path(group)
%td.td2= group.description %td.td2= group.description
%td.td5 %td.td5
= link_to image_tag('x.png'), '#' unless group.owner_id == current_user.id = link_to image_tag('x.png'), remove_group_member_path(group, current_user), :method => :delete unless group.owner_id == current_user.id
-#.block -#.block
-# .secondary-navigation -# .secondary-navigation

View File

@ -11,9 +11,9 @@
= t("layout.collaborators.roles") = t("layout.collaborators.roles")
%tbody %tbody
- @users.each do |user| - @users.each do |user|
%tr#admin-table-members-row1 %tr
%td %td
%span#niceCheckbox1.niceCheck-main{:onclick => "changeCheck(this)", :style => "background-position: 0px 0px; "} %span#niceCheckbox1.niceCheck-main
= check_box_tag "user_remove[#{user.id}][]" = check_box_tag "user_remove[#{user.id}][]"
%td %td
.img .img
@ -23,9 +23,6 @@
%td %td
.radio .radio
= radio_button_tag "user[#{user.id}]", role, ((parent.objects.exists? :object_id => user.id, :object_type => 'User', :role => role) ? :checked : nil), :class => 'niceRadio' = radio_button_tag "user[#{user.id}]", role, ((parent.objects.exists? :object_id => user.id, :object_type => 'User', :role => role) ? :checked : nil), :class => 'niceRadio'
-# f.radio_button "group[#{role}][#{group.id}]", '1', ((@project.relations.exists? :object_id => group.id, :object_type => 'Group', :role => role) ? :checked : nil)
-#%span.niceRadio.radioChecked
-# %input#myradio1{:checked => "checked", :name => "myradio", :tabindex => "1", :type => "radio", :value => "on"}/
.forradio= t("layout.collaborators.role_names.#{ role }") .forradio= t("layout.collaborators.role_names.#{ role }")
= link_to_function t("layout.delete"), "deleteAdminMember();", :class => 'button' = link_to_function t("layout.delete"), "deleteAdminMember();", :class => 'button'
.both .both
@ -38,11 +35,6 @@
.lineForm .lineForm
= select_tag 'role', options_for_collaborators_roles_select = select_tag 'role', options_for_collaborators_roles_select
.both .both
-#.admin-search
-# = label_tag "group_uname", t("layout.collaborators.input_groupname")
-# = autocomplete_field_tag 'group_id', params[:group_id], autocomplete_group_uname_groups_path, :id_element => '#group_id_field'
=# hidden_field_tag 'member_id', nil, :id => 'member_id_field'
=# hidden_field_tag 'group_id', nil, :id => 'group_id_field'
%br %br
= submit_tag t("layout.add"), :class => 'button' = submit_tag t("layout.add"), :class => 'button'
@ -51,50 +43,3 @@
= link_to_function t("layout.save"), "saveAdminMember();", :class => 'button' = link_to_function t("layout.save"), "saveAdminMember();", :class => 'button'
- content_for :sidebar, render('groups/sidebar') - content_for :sidebar, render('groups/sidebar')
-#.block
-# .secondary-navigation
-# %ul.wat-cf
-# %li.first= link_to t("layout.members.back_to_group"), parent_path
-# %li.active= link_to t("layout.members.edit"), edit_group_members_path(@group)
-# .content
-# .inner
-# = form_tag group_members_path(parent) do
-# %h2.title= t("layout.users.list_header")
-# %table.table
-# %tr
-# %th.first ID
-# %th= t("activerecord.attributes.user.name")
-# %th= t("activerecord.attributes.user.roles")
-# %th= t("activerecord.attributes.user.uname")
-# - #TODO: Replace this Chelyabinsk add/remove collaborators method by more human method
-# - @users.each do |user|
-# %tr{:class => cycle("odd", "even")}
-# %td
-# = user.id
-# %td
-# = link_to user.name, user_path(user)
-# %td
-# - Relation::ROLES.each do |role|
-# = check_box_tag "#{ role }[#{user.id}]", '1', ((parent.objects.exists? :object_id => user.id, :object_type => 'User', :role => role) ? :checked : nil), {:class => "user_role_chbx"}
-# = label_tag "#{ role }[#{user.id}]", t("layout.members.roles.#{ role }")
-# %td
-# = user.uname
-# .group.navform.wat-cf
-# %button.button{:type => "submit"}
-# = image_tag("choose.png", :alt => t("layout.save"))
-# = t("layout.save")
-# %span.text_button_padding= t("layout.or")
-# = link_to t("layout.cancel"), group_path(parent), :class => "text_button_padding link_button"
-#
-# = form_tag add_group_members_path(parent) do
-# .group
-# %h2.title= t("layout.members.add_member")
-# = label_tag "", t("layout.members.input_username")
-# = autocomplete_field_tag 'user_id', params[:user_id], autocomplete_user_uname_users_path
-# %br
-# .group.navform.wat-cf
-# %button.button{:type => "submit"}
-# = image_tag("choose.png", :alt => t("layout.add"))
-# = t("layout.add")
-#

View File

@ -168,6 +168,7 @@ Rosa::Application.routes.draw do
end end
member do member do
post :update post :update
delete :remove
end end
end end
end end