From bd0d975014c90eaa04ad8bef19e5d4478cf94d1b Mon Sep 17 00:00:00 2001 From: "Matthew K. Fu JuYuan" Date: Wed, 24 Oct 2012 14:46:25 +0800 Subject: [PATCH] for user new interface, member role is working. --- app/assets/stylesheets/member.css | 23 ++++ app/assets/stylesheets/site-map.css | 3 +- .../admin/users_new_interface_controller.rb | 31 ++--- app/helpers/attribute_fields_helper.rb | 8 +- app/models/user/attribute_field.rb | 27 ++++- app/models/user/attribute_value.rb | 5 +- app/models/user/role.rb | 7 ++ app/models/user/status.rb | 2 +- app/models/user/user.rb | 92 ++++++++++----- .../admin/users_new_interface/_form.html.erb | 109 +++++++++++++----- .../admin/users_new_interface/_role.html.erb | 13 ++- .../users_new_interface/_show_roles.html.erb | 10 ++ .../_show_sub_role.html.erb | 7 ++ .../admin/users_new_interface/show.html.erb | 3 + lib/tasks/build_new_member.rake | 62 ++++++++++ 15 files changed, 312 insertions(+), 90 deletions(-) create mode 100644 app/views/admin/users_new_interface/_show_roles.html.erb create mode 100644 app/views/admin/users_new_interface/_show_sub_role.html.erb diff --git a/app/assets/stylesheets/member.css b/app/assets/stylesheets/member.css index 7ef219be..436a9b6a 100644 --- a/app/assets/stylesheets/member.css +++ b/app/assets/stylesheets/member.css @@ -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; } \ No newline at end of file diff --git a/app/assets/stylesheets/site-map.css b/app/assets/stylesheets/site-map.css index 711b10e2..d7d3cda6 100644 --- a/app/assets/stylesheets/site-map.css +++ b/app/assets/stylesheets/site-map.css @@ -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; diff --git a/app/controllers/admin/users_new_interface_controller.rb b/app/controllers/admin/users_new_interface_controller.rb index 7a68cac9..0609fef0 100644 --- a/app/controllers/admin/users_new_interface_controller.rb +++ b/app/controllers/admin/users_new_interface_controller.rb @@ -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 diff --git a/app/helpers/attribute_fields_helper.rb b/app/helpers/attribute_fields_helper.rb index f750a1d8..5e0cf290 100644 --- a/app/helpers/attribute_fields_helper.rb +++ b/app/helpers/attribute_fields_helper.rb @@ -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 diff --git a/app/models/user/attribute_field.rb b/app/models/user/attribute_field.rb index 4407cb6d..0d9999b3 100644 --- a/app/models/user/attribute_field.rb +++ b/app/models/user/attribute_field.rb @@ -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 diff --git a/app/models/user/attribute_value.rb b/app/models/user/attribute_value.rb index 2835a0bd..a1eb5466 100644 --- a/app/models/user/attribute_value.rb +++ b/app/models/user/attribute_value.rb @@ -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 diff --git a/app/models/user/role.rb b/app/models/user/role.rb index 438e2dab..cd3d6775 100644 --- a/app/models/user/role.rb +++ b/app/models/user/role.rb @@ -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 \ No newline at end of file diff --git a/app/models/user/status.rb b/app/models/user/status.rb index f9f19420..d9715d89 100644 --- a/app/models/user/status.rb +++ b/app/models/user/status.rb @@ -5,7 +5,7 @@ class Status has_and_belongs_to_many :users - + field :key belongs_to :role # has_and_belongs_to_many :sub_roles diff --git a/app/models/user/user.rb b/app/models/user/user.rb index 14ebbc69..3ae7bf5d 100644 --- a/app/models/user/user.rb +++ b/app/models/user/user.rb @@ -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 \ No newline at end of file diff --git a/app/views/admin/users_new_interface/_form.html.erb b/app/views/admin/users_new_interface/_form.html.erb index 6ac69e8a..31e17932 100644 --- a/app/views/admin/users_new_interface/_form.html.erb +++ b/app/views/admin/users_new_interface/_form.html.erb @@ -16,39 +16,92 @@ <%= render :partial=>"infos"%> -<%#= render :partial=>"roles"%> +<%= render :partial=>"roles"%> <% content_for :page_specific_javascript do -%> - <% end -%> \ No newline at end of file diff --git a/app/views/admin/users_new_interface/_role.html.erb b/app/views/admin/users_new_interface/_role.html.erb index 6c2ba769..405bfad8 100644 --- a/app/views/admin/users_new_interface/_role.html.erb +++ b/app/views/admin/users_new_interface/_role.html.erb @@ -1,16 +1,19 @@ -
+

<%= role.title %>

<% 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)%> - <%= sub_role.title %> -
> + + <%= sub_role.title %> + +
<% 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 %>
diff --git a/app/views/admin/users_new_interface/_show_roles.html.erb b/app/views/admin/users_new_interface/_show_roles.html.erb new file mode 100644 index 00000000..fa4775df --- /dev/null +++ b/app/views/admin/users_new_interface/_show_roles.html.erb @@ -0,0 +1,10 @@ +
+
+
+
+ + + <%= render :partial => 'show_sub_role',:collection=> @user.get_sub_roles_by_role(show_roles)%> + +
+
diff --git a/app/views/admin/users_new_interface/_show_sub_role.html.erb b/app/views/admin/users_new_interface/_show_sub_role.html.erb new file mode 100644 index 00000000..12c5d897 --- /dev/null +++ b/app/views/admin/users_new_interface/_show_sub_role.html.erb @@ -0,0 +1,7 @@ +<%= show_sub_role.title %> +<% show_sub_role.attribute_fields.each do |item|%> + + <%= item.title %> + <%= show_attribute_value(@user.get_attribute_value(item).get_value_by_locale(I18n.locale)) %> + +<% end %> \ No newline at end of file diff --git a/app/views/admin/users_new_interface/show.html.erb b/app/views/admin/users_new_interface/show.html.erb index b92197dd..b064986b 100644 --- a/app/views/admin/users_new_interface/show.html.erb +++ b/app/views/admin/users_new_interface/show.html.erb @@ -51,6 +51,9 @@
<%= 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} %> diff --git a/lib/tasks/build_new_member.rake b/lib/tasks/build_new_member.rake index 4e4a6bcc..5810f4fe 100644 --- a/lib/tasks/build_new_member.rake +++ b/lib/tasks/build_new_member.rake @@ -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