for user new interface, member role is working.

This commit is contained in:
Matthew K. Fu JuYuan 2012-10-24 14:46:25 +08:00
parent 13ae2602cd
commit 1b876d4305
15 changed files with 312 additions and 90 deletions

View File

@ -315,4 +315,27 @@
}
.user-role .nav-pills {
margin-left: 10px;
}
.plural .input-append input, .plural .input-append select, .plural .input-append .uneditable-input {
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}
.plural .input-append .removeInput {
display: none;
}
.plural.editMore .input-append input, .plural.editMore .input-append select, .plural.editMore .input-append .uneditable-input {
-webkit-border-radius: 3px 0 0 3px;
-moz-border-radius: 3px 0 0 3px;
border-radius: 3px 0 0 3px;
}
.plural.editMore .input-append input, .plural.editMore .input-append select, .plural.editMore .input-append .uneditable-input {
-webkit-border-radius: 3px 0 0 3px;
-moz-border-radius: 3px 0 0 3px;
border-radius: 3px 0 0 3px;
}
.plural.editMore .input-append .add-on, .plural.editMore .input-append .btn {
-webkit-border-radius: 0 3px 3px 0;
-moz-border-radius: 0 3px 3px 0;
border-radius: 0 3px 3px 0;
}

View File

@ -69,8 +69,9 @@
}
.site-map .disabled h4 {
color: #CACACA;
border-radius: 4px;
}
.site-map .disabled h4 .onoff {
.site-map .disabled h4 .onoff ,.onoff.disabled {
box-shadow: 0px 1px 0px rgba(255, 255, 255, 0.5), 0 0 5px rgba(0,0,0,.2) inset;
background-color: #9C9C9C;
color: #CACACA;

View File

@ -43,18 +43,6 @@ class Admin::UsersNewInterfaceController < ApplicationController
@profile_data.push({:name => att_val.attribute_field.title,:value =>att_val.get_value_by_locale(I18n.locale)}) if att_val.attribute_field.attribute.key=="profile" rescue false
}
attribute_values.each{|att_val|
@teacher_data.push({:name => att_val.attribute_field.title,:value => att_val.get_value_by_locale(I18n.locale) }) if att_val.attribute_field.role.key=="teacher"rescue false
}
attribute_values.each{|att_val|
@student_data.push({:name => att_val.attribute_field.title,:value => att_val.get_value_by_locale(I18n.locale) }) if att_val.attribute_field.role.key=="student"rescue false
}
attribute_values.each{|att_val|
@staff_data.push({:name => att_val.attribute_field.title,:value => att_val.get_value_by_locale(I18n.locale) }) if att_val.attribute_field.role.key=="staff_data"rescue false
}
if(!params[:show_plugin_profile].nil?)
@right_partial = OrbitApp::Plugin::Registration.find_by_key(params[:show_plugin_profile]).profile_partial_path rescue 'plugin_summary'
@ -81,20 +69,21 @@ class Admin::UsersNewInterfaceController < ApplicationController
def new
@user = User.new
@disable_all_field = true
# @disable_all_field = true
@form_index = 0
get_info_and_roles
end
def create
binding.pry
puts params.to_yaml
attribute_values_key = params[:user].has_key?('new_attribute_values') ? 'new_attribute_values' : 'attribute_values'
attribute_values = params[:user].delete(attribute_values_key)
# attribute_values_key = params[:user].has_key?('new_attribute_values') ? 'new_attribute_values' : 'attribute_values'
# attribute_values = params[:user].delete(attribute_values_key)
@user = User.new(params["user"])
attribute_values.to_hash.each{|key,value|
@user.attribute_values.build(value)
}
@user.rebuild_sub_roles_from_attribute_values!(attribute_values)
# attribute_values.to_hash.each{|key,value|
# @user.attribute_values.build(value)
# }
# @user.rebuild_sub_roles_from_attribute_values!(attribute_values)
if @user.save
flash[:notice] = t('admin.create_success_user')
redirect_to :action => :index
@ -111,7 +100,7 @@ class Admin::UsersNewInterfaceController < ApplicationController
end
def edit
@disable_all_field = false
# @disable_all_field = false
@user = User.find(params[:id])
@form_index = 0
get_info_and_roles
@ -122,7 +111,7 @@ class Admin::UsersNewInterfaceController < ApplicationController
@user.update_attributes(params[:user])
attribute_values_key = params[:user].has_key?('new_attribute_values') ? 'new_attribute_values' : 'attribute_values'
attribute_values = params[:user].delete(attribute_values_key)
@user.rebuild_sub_roles_from_attribute_values!(attribute_values)
# @user.rebuild_sub_roles_from_attribute_values!(attribute_values)
@user.save
redirect_to :action => :show
# # Update changes to the avatar

View File

@ -44,6 +44,7 @@ module AttributeFieldsHelper
end
def render_checkbox
@prefiled_value ||=[]
markup_value = eval(self.markup_value) rescue {}
control_group_wrapper do
markup_value.collect do |key,value|
@ -61,8 +62,9 @@ module AttributeFieldsHelper
end
def render_radio_button
@prefiled_value ||=[]
markup_value = eval(self.markup_value) rescue {}
control_group_wrapper do
control_group_wrapper do
markup_value.collect do |key,value|
label_tag(key,radio_button_tag(get_field_name_base+"[value][#{key}]", value[I18n.locale.to_s], (@prefiled_value.include?(key) ? true : false), {})+value[I18n.locale.to_s],@markup_options.merge(:class=>"control-label"))
end.join rescue ""
@ -70,7 +72,7 @@ module AttributeFieldsHelper
end
def render_select
markup_value = eval(self.markup_value) rescue {}
markup_value = (self.markup_value.is_a?(Hash) ? self.markup_value : eval(self.markup_value) )rescue {}
control_group_wrapper{select_tag( get_field_name_base+"[value]",options_for_select(markup_value.collect{|p| [p[1][I18n.locale.to_s],p[0]]},@prefiled_value),@markup_options)} rescue ""
end
@ -94,6 +96,7 @@ module AttributeFieldsHelper
if(add_more and value.is_a?(Hash))
values = value
values.each_with_index.collect do |value,index|
text_field_tag(get_field_name_base + (key.nil? ? '' : "[#{key}][#{index}]"), value.last,@markup_options)
end.join.html_safe
else
@ -166,6 +169,7 @@ protected
def get_field_name_base
# "user[#{self.attribute._type.downcase.pluralize}][#{self.attribute._id.to_s}][attribute_values][#{attribute_value.id}]"
# binding.pry if @attribute_value[:key] == 'status'
if @new_attribute
"user[new_attribute_values][#{@index}]"
else

View File

@ -22,10 +22,35 @@ class AttributeField
belongs_to :attribute
# belongs_to :role
has_many :attribute_values
has_many :attribute_values,:autosave => true, :dependent => :destroy
# validates_uniqueness_of :key
def self_defined_markup_options?
(self.attribute.role.method(self[:key].pluralize.to_sym) && self.attribute.role.method(self[:key].pluralize+"_for_"+markup)) rescue false
end
def markup_value=(var)
if !self_defined_markup_options?
self[:markup_value] = (eval(var) rescue {})
end
end
def markup_value
if self_defined_markup_options?
#Class need to have corresponding field and value agent
# Ex: For "status" the class must have field called "statuses" for the relation and "statuses_for_select" for the select function
method = self.attribute.role.method(self[:key].pluralize+"_for_"+markup)
return (method.call rescue {})
elsif self[:markup_value].nil? || (self[:markup_value].empty?)
return {}
else
return self[:markup_value]
end
end
def markup_options=(var)
self[:markup_options] = (eval(var) rescue {})
end

View File

@ -14,6 +14,7 @@ class AttributeValue
# NO_MULTI_TAG = ["select","date","radio_button","checkbox","date_durnation"]
def check_key
binding.pry if attribute_field.nil?
self.key = attribute_field.key
end
@ -30,7 +31,7 @@ class AttributeValue
when "text_field"
self.attribute_field.locale ? self[locale] : self[:value]
when "select"
markup_values = eval(self.attribute_field.markup_value)
markup_values = self.attribute_field.self_defined_markup_options? ? self.attribute_field.markup_value : eval(self.attribute_field.markup_value)
markup_values[self[:value]][locale.to_s] rescue 'NoData'
when "text_area"
self.attribute_field.locale ? self[locale] : self[:value]
@ -62,7 +63,7 @@ class AttributeValue
if self.attribute_field.markup == "select"
self[:value]
else
self[:value].keys
self[:value].keys rescue self[:value]
end
end
end

View File

@ -27,4 +27,11 @@ class Role
self.first(:conditions => {:key => key})
end
def statuses_for_select
data = self.statuses.map do |t|
[t.id.to_s,t.title_translations]
end
Hash[data]
end
end

View File

@ -5,7 +5,7 @@ class Status
has_and_belongs_to_many :users
field :key
belongs_to :role
# has_and_belongs_to_many :sub_roles

View File

@ -34,27 +34,60 @@ class User
accepts_nested_attributes_for :attribute_values, :allow_destroy => true
before_create :initialize_desktop
before_save :check_status_record
before_save :rebuild_status_record
before_save :save_roles
scope :remote_account, where(:nccu_id.ne => nil)
validates_uniqueness_of :email,:message=> I18n.t("devise.registrations.email_not_unique")
# def new_attribute_values=(vars)
# binding.pry
# end
# def new_attribute_values(vars)
# binding.pry
# end
def rebuild_sub_roles_from_attribute_values!(attribute_values)
# attribute_fields = AttributeField.find attribute_values.collect{|t| t[1][:attribute_field_id]}
# self.sub_roles = attribute_fields.collect{|t| t.attribute if t.attribute.is_a? SubRole}.compact.uniq
## # class << self
## def initialize
## self.define_method("asd"){ p "hi"}
## sub_roles.each do |sr|
## sr_ids = sr.attribute_fields.collect{|t| t.id.to_s}
## self.define_method("attribute_values_for_"+sr.key.to_s) {
## AttributeValue.where(user_id: id ).in(attribute_field_id: sr_ids)
## }
## end
## yield self
## end
# end
# def initialize
# binding.pry
# # yield self
# end
def get_attribute_value(attribute_field)
attribute_values.where(attribute_field_id: attribute_field.id).first
end
def get_sub_roles_by_role(role)
sub_roles.where(role_id: role.id)
end
def disable_sub_role=(var)
var[:id].each do |id,val|
# binding.pry if id == '5052c5b22b5c49ab02000004'
if (val=="true")
self.sub_roles = self.sub_roles.reject{|t| t.id.to_s==id}
elsif(val=="false")
self.sub_roles << SubRole.find(id)
end
end
end
def set_sub_role(sub_role_id,status_id)
self.sub_roles << SubRole.find(sub_role_id)
self.status_record.store(sub_role_id,status_id)
self.status_record.merge!(Hash[sub_role_id,status_id])
end
# def get_status(*params)
@ -80,11 +113,18 @@ class User
def self.find_by_status_and_sub_role_key(sub_role_key,status_key) #Query for users by using specific sub_role and status key
sr = ::SubRole.first({conditions: { key: sub_role_key }})
status = ::Status.first({conditions:{role_id: sr.role.id,key: status_key}})
find_by_status(sr.id,status.id)
find_by_subrole_and_status(sr.id,status.id)
end
def self.find_by_status(sub_role_id,status_id) #Query for users by using specific sub_role and status key buy within ID
User.where("status_record.#{sub_role_id}" => status_id)
def self.find_by_subrole_and_status(sub_role_id,status_id) #Query for users by using specific sub_role and status key by within ID
User.where("status_record.#{sub_role_id}" => status_id,:sub_role_ids.in => [ sub_role_id ])
end
def self.find_by_status(status_id)
status = ::Status.find status_id
::SubRole.where({role_id: status.role.id.to_s,disabled: false}).collect{|sr|
find_by_subrole_and_status(sr.id.to_s,status_id)
}.uniq!
end
def create_dept_cache
@ -143,23 +183,17 @@ class User
end
protected
def save_roles
self.roles = self.sub_roles.collect{|t| t.role}.uniq
def save_roles
self.roles = self.sub_roles.collect{|t| t.role}.uniq
end
def rebuild_status_record
self.status_record = {}
self.attribute_values.reject{|t| t.key!='status'}.each do |status|
# binding.pry
set_sub_role(status.attribute_field.attribute.id.to_s,status[:value]) rescue nil
end
end
def check_status_record
roles = sub_roles.collect{|t| t.role}.uniq #get all role from sub_roles
sub_roles_ary = sub_roles.collect{|t| t.id.to_s}
self.status_record = status_record.keep_if{|sub_role_id, status_id|
includeing = sub_roles_ary.include?(sub_role_id)
valide = false
if includeing
sub_role = SubRole.find sub_role_id
valide = sub_role.role.statuses.include? (Status.find status_id)
end
(includeing and valide)
} rescue {}
end
end
end

View File

@ -16,39 +16,92 @@
</div>
<%= render :partial=>"infos"%>
<%#= render :partial=>"roles"%>
<%= render :partial=>"roles"%>
<% content_for :page_specific_javascript do -%>
<script type="text/javascript">
$(document).ready(function(){
// $("a.submit_btn").click(function(){
// value = $("a.submit_btn").parents("form").find("input[disabled='disabled']")
// jQuery.each(value, function() {
// index = $(this).attr("for");
// console.log(index);
// $("input[for='"+ index +"']").remove();
// });
// value.remove();
// $(this).parents("form")[0].submit();
<script>
// $('.site-map').masonry({
// itemSelector: '.map-block',
// columnWidth: 450,
// isAnimated: true
// });
$(document).ready(function(){
function subrole_disable_field( triggled_object, switch_value_str){
var switch_field = triggled_object.attr("for");
$("input.subrole_disable_field[for="+switch_field+"]").val(switch_value_str)
}
$(".onoff").click(function(){
var dom = $(this).attr("for");
if($(this).hasClass("disabled")){
$(".controls-block[for="+dom+"] input").removeAttr("disabled");
$(".controls-block[for="+dom+"]").slideDown();
$(this).removeClass("disabled");
$(this).text("OFF");
}else{
$(this).addClass("disabled");
$(".controls-block[for="+dom+"]").slideUp();
$(".controls-block[for="+dom+"] input").attr("disabled","disabled");
$(this).text("ON");
}
return false;
function checkSwitch() {
$(".groups").each(function() {
$(this).find('.form-horizontal > .hide').length < $(this).find('.form-horizontal > div').length ? $(this).removeClass("disabled") : $(this).addClass("disabled")
});
}
function checkMultipleInput() {
$(".multipleInput").each(function() {
$(this).find('.controls').length==1 ? $(this).addClass("plural") : $(this).removeClass("plural")
});
}
function removeInput(){
$(".removeInput").click(function (){
$(this).parents(".controls").remove();
checkMultipleInput();
return false;
});
}
checkSwitch();
checkMultipleInput();
removeInput();
$(".help-block a").click(function (){
var $CloneTarget = $(this).parents(".controls").prev(".multipleInput").find('.controls:last');
$(this).parents(".controls").prev(".multipleInput").append($CloneTarget.clone());
$(this).parents(".controls").prev(".multipleInput").find('.controls:last input').val("");
removeInput();
checkMultipleInput();
return false;
})
$(".status select").each(function (i) {
$(this).change(function () {
$(".status option:selected").eq(i).each(function () {
if($(this).attr("value")=="alumna") {
$(this).parents(".status").nextAll(".graduated").removeClass("hide");
$(this).parents(".status").nextAll(".graduated").find("select").removeAttr("disabled");
}else{
$(this).parents(".status").nextAll(".graduated").addClass("hide");
$(this).parents(".status").nextAll(".graduated").find("select").attr({disabled:''});
}
});
})
})
$('.onoff').click(function () {
if($(this).parents("h4").length==1) {
$(this).parents(".map-block").toggleClass("disabled");
$(this).parents(".map-block").find(".form-horizontal").toggleClass("hide");
if($(this).parents(".map-block").hasClass("disabled")){
$(this).text("OFF");
// console.log("LV1");
// console.log($(this));
}else{
$(this).text("ON");
// console.log("LV1");
// console.log($(this));
}
}
if($(this).parents("legend").length==1) {
$(this).toggleClass("disabled");
$(this).parents("legend").next("div").toggleClass("hide");
if($(this).parents("legend").next("div").hasClass("hide")){
$(this).text("OFF");
console.log("LV2");
subrole_disable_field($(this),"true");
}else{
$(this).text("ON");
console.log("LV2");
subrole_disable_field($(this),"false");
}
checkSwitch();
}
return false;
});
})
</script>
<% end -%>

View File

@ -1,16 +1,19 @@
<div class="map-block back">
<div class="groups map-block back">
<h4><span><%= role.title %></span></h4>
<div class="form-horizontal">
<% role.sub_roles.each do |sub_role| %>
<% sub_role_disable = @disable_all_field ? true : !check_user_role_enable(sub_role.attribute_fields) %>
<% sub_role_disable = @user.sub_roles.include?(sub_role) ? false : true %>
<%= hidden_field_tag("user[disable_sub_role][id][#{sub_role.id}]",sub_role_disable,:for=>@form_index,:class=>"subrole_disable_field",:value=>sub_role_disable)%>
<%#= hidden_field_tag("[user][new_attribute_values][sub_role][disable][#{sub_role.id}]",sub_role_disable)%>
<legend><button for=<%=@form_index%> class="onoff pull-right <%= sub_role_disable ? 'disabled' : '' %> "><%= sub_role_disable ? 'ON' : 'OFF' %></button><%= sub_role.title %></legend>
<div class="controls-block <%= sub_role_disable ? 'hide' : '' %>" for=<%=@form_index%> >
<legend>
<button for=<%=@form_index%> class="onoff pull-right <%= sub_role_disable ? 'disabled' : '' %> ">ON</button><%= sub_role.title %>
</legend>
<div class="controls-block <%= sub_role_disable ? 'hide' : '' %>" >
<% sub_role.attribute_fields.each do |af|%>
<%= af.block_helper(@user,@form_index,sub_role_disable)%>
<%= af.block_helper(@user,@form_index)%>
<% @form_index = @form_index +1 %>
<% end %>
</div>

View File

@ -0,0 +1,10 @@
<div class="roles <%= show_roles.key %>">
<div class="label-line">
<hr />
</div>
<table class="table">
<tbody>
<%= render :partial => 'show_sub_role',:collection=> @user.get_sub_roles_by_role(show_roles)%>
</tbody>
</table>
</div>

View File

@ -0,0 +1,7 @@
<tr><td class="span1"><%= show_sub_role.title %></td></tr>
<% show_sub_role.attribute_fields.each do |item|%>
<tr>
<td class="span1"><%= item.title %></td>
<td><%= show_attribute_value(@user.get_attribute_value(item).get_value_by_locale(I18n.locale)) %></td>
</tr>
<% end %>

View File

@ -51,6 +51,9 @@
<div class="viewport">
<div class="overview">
<%= render :partial=> "user_role",:locals=>{:role_class=>"basic",:i18n=>"admin.new_admin.users.profile",:items=>@profile_data} %>
<%= render :partial=> "show_roles",:collection=>@user.roles %>
<%# @user.sub_roles.each{|sr| render :partial=> "user_role",:locals=>{:role_class=>sr.key.to_s,:i18n=>"admin.new_admin.users.roles.#{sr.key.to_s}",:items=>sr} }%>
<%#= render :partial=> "user_role",:locals=>{:role_class=>"teacher",:i18n=>"admin.new_admin.users.roles.teacher",:items=>@teacher_data} %>
<%#= render :partial=> "user_role",:locals=>{:role_class=>"student",:i18n=>"admin.new_admin.users.roles.student",:items=>@student_data}%>
<%#= render :partial=> "user_role",:locals=>{:role_class=>"staff",:i18n=>"admin.new_admin.users.roles.staff",:items=>@staff_data} %>

View File

@ -268,4 +268,66 @@ namespace :build_new_member do
# p "==Finished building status for Students"
# end
task :build_sub_role_for_staff_1023 => :environment do
role = Role.first(conditions: {key: 'staff'})
staff_sub_roles ={
:secretary => {"en"=>"Secretary", "zh_tw"=>"秘書"},
:assistant => {"en"=>"Assistant", "zh_tw"=>"行政助理"},
:research_assistant => {"en"=>"Research Assistant", "zh_tw"=>"研究助理"},
:ta => {"en"=>"TA", "zh_tw"=>"助教"},
:technical_specialist => {"en"=>"Technical Specialist", "zh_tw"=>"技士"}
}
staff_sub_roles.each do |key,item|
role.sub_roles.build(:key=>key,:title_translations=> item)
end
role.save
end
task :build_staff_attribute_field_1023 => :environment do
sub_role_attribute_fields_template = [
{key: 'status', markup: "select", locale: true, list_options: nil, built_in: true, disabled: false, locale_title_translations: {"zh_tw"=>"狀態", "en"=>"Status"}, neutral_title: nil },
{key: 'responsibilities', markup: "text_field", locale: true, list_options: nil, built_in: true, disabled: false, locale_title_translations: {"zh_tw"=>"負責業務", "en"=>"Responsibilities"}, neutral_title: nil }
]
role = Role.first({conditions:{key: 'staff'}})
role.sub_roles.each{|sb|
sb.attribute_fields.destroy_all
sub_role_attribute_fields_template.each{|att_f|
sb.attribute_fields.build att_f
}
sb.save
}
role.save
end
task :build_status_1023 => :environment do
student_status ={
:studying => {"en"=>"Studying", "zh_tw"=>"在學"},
:drop_out => {"en"=>"Drop-out", "zh_tw"=>"休學"},
:alumi => {"en"=>"Alumi", "zh_tw"=>"校友"},
:suspended => {"en"=>"Suspended", "zh_tw"=>"未完成"}
}
role = Role.first(conditions: {key: 'student'})
student_status.each do |key,item|
role.statuses.build(:key=>key.to_s,:title_translations=>item)
end
role.save
teacher_status ={
:fulltime => {"en"=>"Full Time", "zh_tw"=>"全職"},
:adjunct => {"en"=>"Adjunct", "zh_tw"=>"兼職"},
:co_hiring => {"en"=>"Co Hiring", "zh_tw"=>"合聘"},
:distinguished => {"en"=>"Distinguished", "zh_tw"=>"特聘"}
}
role = Role.first(conditions: {key: 'teacher'})
teacher_status.each do |key,item|
role.statuses.build(:key=>key.to_s,:title_translations=>item)
end
role.save
end
task :clean_up_attribute_value_for_deleted_fields => :environment do
AttributeValue.all.reject{|t| !AttributeField.find(t.attribute_field_id).nil? rescue false }.each{|t| t.delete}
end
end