From dafe28ffb5ac81cc8196dd37578ea30935d34545 Mon Sep 17 00:00:00 2001 From: "Matthew K. Fu JuYuan" Date: Thu, 8 Nov 2012 18:59:19 +0800 Subject: [PATCH] add_more ok --- .../admin/users_new_interface_controller.rb | 4 +- app/helpers/attribute_fields_helper.rb | 103 +++++++++------ app/helpers/attribute_values_helper.rb | 68 ++++++++++ app/models/user/attribute_field.rb | 66 ++++++++-- app/models/user/attribute_value.rb | 105 ++++++++------- .../admin/infos/_attribute_field.html.erb | 15 ++- app/views/admin/infos/_form.html.erb | 2 +- .../admin/users_new_interface/_form.html.erb | 122 ++++++------------ .../_show_sub_role.html.erb | 2 +- config/locales/zh_tw.yml | 8 +- config/mongoid.yml | 1 + spec/models/attribute_value_spec.rb | 94 +++++++++----- 12 files changed, 373 insertions(+), 217 deletions(-) create mode 100644 app/helpers/attribute_values_helper.rb diff --git a/app/controllers/admin/users_new_interface_controller.rb b/app/controllers/admin/users_new_interface_controller.rb index 323271d2..0d3699b8 100644 --- a/app/controllers/admin/users_new_interface_controller.rb +++ b/app/controllers/admin/users_new_interface_controller.rb @@ -35,7 +35,6 @@ class Admin::UsersNewInterfaceController < ApplicationController attribute_values = @user.attribute_values attribute_values.each{|att_val| - binding.pry if att_val.id.to_s == '5052dab52b5c49ae9d000006' @profile_data.push({:name => att_val.attribute_field.title,:value =>att_val.get_value_by_locale(I18n.locale.to_s)}) if att_val.attribute_field.attribute.key=="profile" rescue false } @@ -79,7 +78,7 @@ class Admin::UsersNewInterfaceController < ApplicationController # @user.attribute_values.build(value) # } # @user.rebuild_sub_roles_from_attribute_values!(attribute_values) - binding.pry + # binding.pry if @user.save flash[:notice] = t('admin.create_success_user') redirect_to :action => :index @@ -104,6 +103,7 @@ class Admin::UsersNewInterfaceController < ApplicationController def update @user = User.find(params[:id]) + @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) diff --git a/app/helpers/attribute_fields_helper.rb b/app/helpers/attribute_fields_helper.rb index 323d26a3..a1d68b56 100644 --- a/app/helpers/attribute_fields_helper.rb +++ b/app/helpers/attribute_fields_helper.rb @@ -1,6 +1,4 @@ #encoding: utf-8 -# require ActionView::Helpers::FormTagHelper - module AttributeFieldsHelper include ActionView::Helpers::FormTagHelper include ActionView::Helpers::FormOptionsHelper @@ -49,8 +47,8 @@ module AttributeFieldsHelper end def render_checkbox - @prefiled_value ||=[] + # @prefiled_value = @prefiled_value.keys unless @prefiled_value ==[] # begin # markup_value = eval(self.markup_value) # rescue @@ -92,8 +90,7 @@ module AttributeFieldsHelper end def render_select - - prompt = @panel_setting[:prompt][I18n.locale] rescue nil + prompt = @panel_setting["initial"][I18n.locale.to_s] rescue nil @markup_options.merge!(:prompt => prompt) unless prompt.nil? # markup_value = (self.markup_value.is_a?(Hash) ? self.markup_value : eval(self.markup_value) )rescue {} # check self[:option_list].collect{|p| [p[1][I18n.locale.to_s],p[0]]} @@ -102,38 +99,31 @@ module AttributeFieldsHelper def render_text_area control_group_wrapper do |key,value| - if(add_more and value.is_a?(Hash)) - values = value - values.each_with_index.collect do |value,index| - place_holder= @panel_setting["placeholder"][key] - text_area_tag(get_field_name_base + (key.nil? ? '' : "[#{key}][#{index}]"), value.last,@markup_options.merge(:placeholder=>place_holder)) - end.join.html_safe - else + # if(add_more and value.is_a?(Hash)) + # values = value + # values.each_with_index.collect do |value,index| + # place_holder= @panel_setting["placeholder"][key] + # text_area_tag(get_field_name_base + (key.nil? ? '' : "[#{key}][#{index}]"), value.last,@markup_options.merge(:placeholder=>place_holder)) + # end.join.html_safe + # else value = can_muti_lang_input ? @prefiled_value[key] : @prefiled_value key = can_muti_lang_input ? "[#{key}]" : "" place_holder= @panel_setting["placeholder"][I18n.locale.to_s] rescue '' text_area_tag(get_field_name_base + key, value,@markup_options.merge(:placeholder=>place_holder)) - end + # end end end def render_text_field - control_group_wrapper do |key,value| + control_group_wrapper do |key,value,add_more_counter| if(add_more) - values = value - result = "" - unless values.nil? - result = values.each_with_index.collect do |value,index| - place_holder= @panel_setting["placeholder"][key] - text_field_tag(get_field_name_base + (key.nil? ? '' : "[#{key}][#{index}]"), value.last,@markup_options.merge(:placeholder=>place_holder)) - end.join.html_safe - end - result + place_holder= @panel_setting["placeholder"][key] + text_field_tag(get_field_name_base + (key.nil? ? '' : "[][#{key}]"), value,@markup_options.merge(:placeholder=>place_holder)) + link_to( (content_tag :i,'',:class=>"icon-trash"),'#',:class=> "btn removeInput") else - value = (can_muti_lang_input ? @prefiled_value[key] : @prefiled_value) rescue nil - key_field = can_muti_lang_input ? "[#{key}]" : "" - place_holder= @panel_setting["placeholder"][key] rescue '' - text_field_tag(get_field_name_base + key_field, value,@markup_options.merge(:placeholder=>place_holder)) + value = (can_muti_lang_input ? @prefiled_value[key] : @prefiled_value) rescue nil + key_field = can_muti_lang_input ? "[#{key}]" : "" + place_holder= @panel_setting["placeholder"][key] rescue '' + text_field_tag(get_field_name_base + key_field, value,@markup_options.merge(:placeholder=>place_holder)) end end end @@ -147,25 +137,40 @@ protected result.html_safe end + def controls_wrapper(&block) - result = "
" + loop_time = self.add_more ? @attribute_value.add_more_counter : 1 + result = '' + result << "
" if self.add_more + result << "
" + loop_time.times do |loop_counter| if can_muti_lang_input result << "
" - result << "
" + result << "
" VALID_LOCALES.collect do |key| + value = @prefiled_value[key.to_s] rescue nil div_class = ["tab-pane" ,"fade"].join(" ") div_class << (key == I18n.locale.to_s ? " active in" : '') - result << content_tag(:div,yield(key,value),:class=>div_class,:id=>"tab"+id.to_s+"_#{key}") - end - + if self.add_more + div_class << " input-append #{add_more_tab(:input_field,loop_counter,key)}" + # binding.pry + add_more_field_value = @prefiled_value[loop_counter][key] rescue '' + result << content_tag(:div,yield(key,add_more_field_value,loop_counter),:class=>div_class)#,:id=>"tab"+id.to_s+"_#{key}_#{loop_counter}" + else + result << content_tag(:div,yield(key,value),:class=>div_class,:id=>"tab"+id.to_s+"_#{key}") + end + end # of VALID_LOCALES.collect for tabed input result << "
" + result << "" + link_entry = self.add_more ? "#{add_more_tab(:tab_btn,loop_counter,key)}" : "#tab"+id.to_s+"_#{key}" + result << content_tag(:li,link_to(I18n.t("langs."+key),link_entry,:data=>{:toggle=>"tab"}),:class=>(key == I18n.locale.to_s ? "active" : nil)) + end # of VALID_LOCALES.collect for tabs + result << "" + result << "
" # @prefiled_value.collect do |key,value| # result << yield(key,value) @@ -173,14 +178,19 @@ protected else result << yield end - if can_add_more + if can_add_more and (loop_counter == loop_time-1) + temp_field_name = get_basic_field_name_base + '[temp]' result << '' - result << ' Add' + result << ''+I18n.t("admin.infos.add")+' ' + result << hidden_field_tag("#{temp_field_name}[count]",loop_time,:class=>"list_count") + result << hidden_field_tag("#{temp_field_name}[count]",get_basic_field_name_base,:class=>"field_name") result << '' end + end # of loop_time result << "
" + result << "
" if self.add_more result.html_safe - end + end # of def controls_wrapper(&block) def control_group_wrapper(&block) div_class = can_muti_lang_input ? "control-group language-swich" : "control-group" @@ -200,6 +210,23 @@ protected end end + def add_more_tab(mode,counter,key) + case mode + when :input_field + get_pairing_tab_class(:suffix=>['','tab'+counter.to_s,key].join('-')) + when :tab_btn + ".#{get_pairing_tab_class(:suffix=>['','tab'+counter.to_s,key].join('-'))}" + end + end + + def get_pairing_tab_class(opts) + prefix = opts[:prefix] + suffix = opts[:suffix] + str = get_basic_field_name_base.gsub("[","-").gsub("]",'') + str = prefix.nil? ? str : prefix+ str + suffix.nil? ? str : str + suffix + end + def get_basic_field_name_base if @new_attribute "user[new_attribute_values][#{@index}]" diff --git a/app/helpers/attribute_values_helper.rb b/app/helpers/attribute_values_helper.rb new file mode 100644 index 00000000..f2ea0832 --- /dev/null +++ b/app/helpers/attribute_values_helper.rb @@ -0,0 +1,68 @@ +module AttributeValuesHelper + def show_west_calender + date = get_date + case self.attribute_field["typeC"]["format"] + when 'format1' # Y/M/D h:m + date.strftime("%Y/%m/%d %H:%M") + when 'format2' # Y/M/D + date.strftime("%Y/%m/%d") + when 'format3' # Y/M + date.strftime("%Y/%m") + when 'format4' # Y + date.strftime("%Y") + end # of case west cal format + end + + def show_minguo_calendar + get_minguo + date = get_date + year_str = "" + unless date.year == 1912 + m_year = (date.year - 1912).abs.to_s + I18n.t("admin.infos.date.minguo_calendar.year") + year_str = minguo_format_year(m_year) + end + get_minguo_year + minguo_m_y_d_time + end + + def get_minguo_year + date = get_date + m_year = (date.year - 1911).abs + year_end = I18n.t("admin.infos.date.minguo_calendar.year") + case + when date.year <1912 + I18n.t("admin.infos.date.minguo_calendar.before") + (m_year+1).to_s + year_end + when date.year ==1912 + I18n.t("admin.infos.date.minguo_calendar.first_year") + when date.year >1912 + I18n.t("admin.infos.date.minguo_calendar.after")+ (m_year).to_s + year_end + end # of case tw_claendar year + end + + def minguo_m_y_d_time + date = get_date + case self.attribute_field["typeC"]["format"] + when 'format1' # Y/M/D h:m + date.strftime(" %m/%d %H:%M") + when 'format2' # Y/M/D + date.strftime(" %m/%d") + when 'format3' # Y/M + date.strftime(" %m#{I18n.t("admin.infos.date.minguo_calendar.month")}") + when 'format4' # Y + '' + end # of case + end + + def get_date_by_format + case I18n.locale + when :zh_tw + case + when self.attribute_field["typeC"]["claendar"] == "west_claendar" + show_west_calender + when self.attribute_field["typeC"]["claendar"] == "tw_claendar" + show_minguo_calendar + end #case self.attribute_field["typeC"]["claendar"] + when :en + show_west_calender + end + end +end \ No newline at end of file diff --git a/app/models/user/attribute_field.rb b/app/models/user/attribute_field.rb index a94a38a2..3cef5f1d 100644 --- a/app/models/user/attribute_field.rb +++ b/app/models/user/attribute_field.rb @@ -13,10 +13,10 @@ class AttributeField field :built_in, :type => Boolean, :default => false field :disabled, :type => Boolean, :default => false field :to_delete,:type=> Boolean,:default => false - field :typeA,:type=> Hash,:default=>{} + field :typeA,:type=> Hash,:default=>{:cross_lang=>false} field :typeB,:type=> Hash,:default=>{} - field :typeC,:type=> Hash,:default=>{:claendar=>"west_claendar"} - field :typeD,:type=> Hash,:default=>{} + field :typeC,:type=> Hash,:default=>{:claendar=>"west_claendar",:format=>"format3"} + field :typeD,:type=> Hash,:default=>{:cross_lang=>false} field :typeE,:type=> Hash,:default=>{} @@ -30,6 +30,11 @@ class AttributeField has_many :attribute_values,:autosave => true, :dependent => :destroy before_save :check_option_list # validates_uniqueness_of :key + + def markup_value + get_data["option_list"] + end + def add_more (get_data["add_more"] == "true" ? true : false) rescue false end @@ -47,11 +52,11 @@ class AttributeField (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=(var) + # if !self_defined_markup_options? + # self[:markup_value] = (eval(var) rescue {}) + # end + # end def option_list if self_defined_markup_options? @@ -82,7 +87,7 @@ class AttributeField end def role - self.attribute.role + self.attribute.role end def panel @@ -138,7 +143,48 @@ class AttributeField # def select_list_options # self.list_options.to_a.join(', ') # end - + + def add_more_convert(opt) + case opt + when :to_add_more + self.attribute_values.each do |av| + VALID_LOCALES.each do |loc| + splited_str = av[loc].split(",") rescue [] + av["val"] = [] if av["val"].nil? + splited_str.each_with_index{|value,index| av["val"][index] = av["val"][index].nil? ? {loc=>value} : av["val"][index].merge(loc=>value) } + # av[loc] = Hash[splited_str.each_with_index.map{|t,index| [index.to_s,t]}] rescue {"0"=>av[loc].to_s} + end + av.save + end #of self.attribute_values.each + when :to_no_add_more + self.attribute_values.each do |av| + VALID_LOCALES.each do |loc| + if av["val"].kind_of? Array + av[loc] = av["val"].collect{|t| t[loc]}.join(",") + else + av[loc] = av["val"] + end + # av[loc] = av["val"].invert.keys.join(",") rescue av["val"] + # av.save(:validate => false) + end + av.unset("val") + av.save + end #of self.attribute_values.each + end + end + + def typeA=(var) + if self["typeA"]["add_more"] != var["add_more"] + case var["add_more"] + when "true" #from no-add_more to add_more + add_more_convert(:to_add_more) + else #from add_more to no-add_more + add_more_convert(:to_no_add_more) + end # of case + end # of if + self["typeA"] = var + end + def is_built_in? self.built_in end diff --git a/app/models/user/attribute_value.rb b/app/models/user/attribute_value.rb index d6500fbf..7b5d4caf 100644 --- a/app/models/user/attribute_value.rb +++ b/app/models/user/attribute_value.rb @@ -1,9 +1,8 @@ class AttributeValue - include Mongoid::Document include Mongoid::Timestamps include Mongoid::MultiParameterAttributes - + include AttributeValuesHelper field :key belongs_to :attribute_field @@ -14,48 +13,52 @@ class AttributeValue # NO_MULTI_TAG = ["select","date","radio_button","checkbox","date_durnation"] - def data_proc - # binding.pry - # binding.pry if self.attribute_field.markup == 'radio_button' - # if self.attribute_field - case self.attribute_field.markup - when 'text_field','text_area' - # binding.pry - self[:temp_data].each{|key,val| - self[key] = val - } unless self[:temp_data].nil? - when 'select','date','radio_button' - self["val"] = self[:temp_data] - when 'checkbox' - self["val"] = self[:temp_data].keys rescue {} - end #end of case - else # if not locale - case self.attribute_field.markup - when 'text_field','text_area' - self["val"] = self[:temp_data] - when 'select','date','radio_button' - self["val"] = self[:temp_data] - when 'checkbox' - self["val"] = self[:temp_data].keys rescue {} - end #end of case - # end #of if self.attribute_field - self.unset('temp_data') + def add_more_counter + index_max = self["val"].count rescue 0 + index_max == 0 ? 1 : index_max end - def value - - result="" - if self.attribute_field.locale && (self.attribute_field.markup == "text_field" || self.attribute_field.markup == "text_area") - result= Hash[VALID_LOCALES.collect{|lang| [lang,self[lang.to_sym]]}] - else - result = self["val"] - end - result + def data_proc + unless self[:temp_data].nil? + case self.attribute_field.markup + when 'text_field','text_area' + if self.attribute_field.add_more + self["val"] = self["temp_data"] + else # if not add_more + self[:temp_data].each{|key,val| + self[key] = val + } if(!self.attribute_field.get_data[:cross_lang]) + self["val"] = self[:temp_data] if(self.attribute_field.get_data[:cross_lang]) + end # of self.attribute_field.add_more + + when 'select','date','radio_button' + self["val"] = self[:temp_data] + when 'checkbox' + self["val"] = self[:temp_data].keys + end #end of case self.attribute_field.markup + end # of self[:temp_data].nil? + self.unset('temp_data') + self.unset('temp') + end #of data_proc + + def value(index = nil) + result = case self.attribute_field.markup + when 'text_field','text_area' + if self.attribute_field.add_more and (self.attribute_field.markup == "text_field") + index.nil? ? self["val"] : self["val"][index] + # self.attribute_field.get_data[:cross_lang] ? Hash[VALID_LOCALES.collect{|lang| [lang,self[lang.to_sym]]}] : self["val"] #if !self.attribute_field.get_data[:cross_lang] + else + self.attribute_field.get_data[:cross_lang] ? self["val"] : Hash[VALID_LOCALES.collect{|lang| [lang,self[lang.to_sym]]}] + end + when 'select','date','radio_button' + self["val"] + when 'checkbox' + self["val"] + end #end of case self.attribute_field.markup end def value=(value) #save everything to temp_data waiting for futher process - # binding self[:temp_data] = value end @@ -70,27 +73,32 @@ class AttributeValue self[(field[0].to_s.delete "=")] = field[1] end end - - def get_value_by_locale(locale) + + def get_value_by_locale(locale,add_more_index=nil) case self.attribute_field.markup when "text_field" - self.attribute_field.locale ? self[locale.to_s] : self.value + case self.attribute_field.add_more + when true + add_more_index.nil? ? self.value.collect{|t| t[locale]}.join(",") : self.value(add_more_index)[locale] + when false + self.attribute_field.locale ? self[locale.to_s] : self.value + end when "select" - markup_values = self.attribute_field.self_defined_markup_options? ? self.attribute_field.markup_value : eval(self.attribute_field.markup_value) + markup_values = self.attribute_field.self_defined_markup_options? ? self.attribute_field.markup_value : self.attribute_field.markup_value markup_values[self.value][locale.to_s] rescue 'NoData' when "text_area" self.attribute_field.locale ? self[locale.to_s] : self.value when "date" - Date.new(self[:val]["(1i)"].to_i,self[:val]["(2i)"].to_i,self[:val]["(3i)"].to_i) rescue nil + get_date_by_format when "addr" self.value when "radio_button" - markup_values = eval(self.attribute_field.markup_value) - markup_values[:value][locale.to_s] + markup_values = self.attribute_field.markup_value + markup_values[self.value][locale.to_s] when "checkbox" markup_values = self.attribute_field.markup_value - self[:value].keys.collect{|key| markup_values[key][locale.to_s]}.join(",") + self.value.collect{|key| markup_values[key][locale.to_s]}.join(",") when "date_durnation" self.value else @@ -113,5 +121,10 @@ class AttributeValue # end # end # end + protected + + def get_date + Date.new(self[:val]["(1i)"].to_i,self[:val]["(2i)"].to_i,self[:val]["(3i)"].to_i) rescue nil + end end diff --git a/app/views/admin/infos/_attribute_field.html.erb b/app/views/admin/infos/_attribute_field.html.erb index 95d60268..f8c7a701 100644 --- a/app/views/admin/infos/_attribute_field.html.erb +++ b/app/views/admin/infos/_attribute_field.html.erb @@ -33,8 +33,8 @@