From dce4cac9bcfe0aa31ee758a5e90a47960e615433 Mon Sep 17 00:00:00 2001 From: bohung Date: Mon, 8 Mar 2021 19:19:10 +0800 Subject: [PATCH] Fix bug. Add periodic time field choice. --- .../personal_plugin_fields_controller.rb | 83 +++++++++++++++---- .../admin/personal_plugin_fields_helper.rb | 1 + app/models/personal_plugin_field.rb | 21 ++++- .../personal_plugin_fields/_form.html.erb | 4 +- .../_render_table.html.erb | 5 ++ config/locales/en.yml | 3 +- config/locales/zh_tw.yml | 3 +- .../helpers/admin/plugin_templates_helper.rb | 34 ++++++-- .../app/models/plugin_template.rb | 3 + .../app/models/plugin_template_related.rb | 3 + .../download_excel.xlsx.axlsx | 2 +- 11 files changed, 133 insertions(+), 29 deletions(-) diff --git a/app/controllers/admin/personal_plugin_fields_controller.rb b/app/controllers/admin/personal_plugin_fields_controller.rb index bd55948..72e4399 100644 --- a/app/controllers/admin/personal_plugin_fields_controller.rb +++ b/app/controllers/admin/personal_plugin_fields_controller.rb @@ -129,7 +129,7 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController "accepts_nested_attributes_for :#{field_name.pluralize}, :allow_destroy => true" }.join("\r\n") plugin_template = @personal_plugin_field.primary_modal_name - backend_index_fields = @personal_plugin_field.backend_fields["index"] rescue [] + backend_index_fields = @personal_plugin_field.backend_fields["index"].to_a rescue [] backend_index_fields_contents = backend_index_fields.map do |field_name| if field_name == slug_title "\r\n <%= link_to #{plugin_template}.#{field_name}, page_for_#{plugin_template}(#{plugin_template}), target: \"blank\" %>\r\n" + @@ -147,7 +147,7 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController "<%= #{plugin_template}.#{field_name} %>" end end - backend_profile_fields = @personal_plugin_field.backend_fields["profile"] rescue [] + backend_profile_fields = @personal_plugin_field.backend_fields["profile"].to_a rescue [] backend_profile_fields_contents = backend_profile_fields.map do |field_name| if field_name == slug_title "\r\n <%= link_to #{plugin_template}.#{field_name}, page_for_#{plugin_template}(#{plugin_template}), target: \"blank\" %>\r\n" + @@ -165,9 +165,9 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController "<%= #{plugin_template}.#{field_name} %>" end end - col_name_to_show = @personal_plugin_field.frontend_fields["member_show"] rescue [] - col_name_to_show_in_show_page = @personal_plugin_field.frontend_fields["show"] rescue [] - col_name_to_show_in_index_page = @personal_plugin_field.frontend_fields["index"] rescue [] + col_name_to_show = @personal_plugin_field.frontend_fields["member_show"].to_a rescue [] + col_name_to_show_in_show_page = @personal_plugin_field.frontend_fields["show"].to_a rescue [] + col_name_to_show_in_index_page = @personal_plugin_field.frontend_fields["index"].to_a rescue [] extra_translate_title = col_name_to_show_in_index_page.map{|field_name| #在個人外掛前台index頁面的欄位翻譯名稱hash ["th-#{field_name}","I18n.t(\"#{@personal_plugin_field.module_name}.#{field_name}\")"] } @@ -250,11 +250,32 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController related_modal_name = @personal_plugin_field.related_modal_name[i] field_names.map{|field_name| "<%= #{related_modal_name}.#{field_name} %>"} } - member_methods_define = primary_modal_fields.select{|f| f[:field_type] == "member"}.map{|field_value| + member_methods_define = primary_modal_fields.select{|f| (f[:field_type] == "member" rescue false)}.map{|field_value| ["def #{field_value[:field_name].pluralize}", " MemberProfile.find(self.#{field_value[:field_name].singularize}_ids) rescue []", "end"] }.flatten + date_time_strftime = {"date"=>"%Y/%m/%d","date_time"=>"%Y/%m/%d %H:%M","year_month"=>"%Y/%m"} + periodic_methods_define = primary_modal_fields.select{|f| (f[:periodic_time] == "1" rescue false)}.map{|field_value| + strftime_string = "" + if date_time_strftime.keys.include?(field_value[:field_type]) + strftime_string = ".strftime(\"#{date_time_strftime[field_value[:field_type]]}\")" + end + ["def #{field_value[:field_name]}", + " \"\#{self.#{field_value[:field_name]}_start#{strftime_string}} ~ \#{self.#{field_value[:field_name]}_end#{strftime_string}}\"", + "end"] + }.flatten + related_periodic_methods_define = @personal_plugin_field.related_modal_fields.map{|field_values| + field_values.select{|f| (f[:periodic_time] == "1" rescue false)}.map{|field_value| + strftime_string = "" + if date_time_strftime.keys.include? field_value[:field_type] + strftime_string = ".strftime(\"#{date_time_strftime[field_value[:field_type]]}\")" + end + ["def #{field_value[:field_name]}", + " \"\#{self.#{field_value[:field_name]}_start#{strftime_string}} ~ \#{self.#{field_value[:field_name]}_end#{strftime_string}}\"", + "end"] + }.flatten + } analysis_field_name = @personal_plugin_field.backend_fields[:analysis][0] rescue "" analysis_field_name = "year" if analysis_field_name.blank? analysis_field_input_fields = "" @@ -314,7 +335,9 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController "before_save_codes" => before_save_codes, "time_fields_text" => time_fields.to_s, "iterate_step_text" => iterate_step_text, - "plugin_template_related_members" => plugin_template_related_members.to_s + "plugin_template_related_members" => plugin_template_related_members.to_s, + "periodic_methods_define" => periodic_methods_define, + "related_periodic_methods_define" => related_periodic_methods_define } max_length = @match_pattern.keys.map{|k| k.length}.max @match_pattern = @match_pattern.sort_by{|k,v| (v.class != Array) ? (max_length - k.length) : -k.length} @@ -355,26 +378,35 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController field_type = field_value[:field_type] next if field_name.blank? next if field_type == "file" || field_type == "link" - if (field_value[:localize] == "1" rescue false) + localize = (field_value[:localize] == "1" rescue false) + periodic = (field_value[:periodic_time] == "1" rescue false) + input_field = generate_input_field(field_type,field_name,localize,extra_field_name,plugin_template,periodic) + if localize locale_fields << field_name - input_field = generate_input_field(field_type,field_name,true,extra_field_name,plugin_template) locale_fields_input_fields << input_field else none_locale_fields << field_name - input_field = generate_input_field(field_type,field_name,false,extra_field_name,plugin_template) none_locale_fields_input_fields << input_field end @index += 1 end return locale_fields , none_locale_fields , locale_fields_input_fields,none_locale_fields_input_fields end - def generate_input_field(field_type,field_name,localize = false,extra_field_name = nil, plugin_template = nil) + def generate_input_field(field_type,field_name,localize = false,extra_field_name = nil, plugin_template = nil,periodic = false) personal_plugin_template = @personal_plugin_field.module_name plugin_template = @personal_plugin_field.primary_modal_name if plugin_template.nil? input_field = "" field_value_text = "@#{plugin_template}.#{field_name}" field_name_text = "\"#{field_name}\"" field_name_text = "locale" if localize + org_field_name = field_name + if periodic + if localize + field_name = "periodic_time_field" + else + field_name_text = "\"periodic_time_field\"" + end + end placeholder = "#{personal_plugin_template}.#{field_name}" placeholder = "#{personal_plugin_template}.#{extra_field_name}.#{field_name}" if extra_field_name if localize @@ -401,6 +433,15 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController if localize input_field.prepend("<%= f.fields_for :#{field_name}_translations do |f| %>\r\n ").concat("\r\n<% end %>") end + if periodic + input_fields = [] + input_fields[0] = input_field.gsub("periodic_time_field","#{org_field_name}_start") + input_fields[1] = input_field.gsub("periodic_time_field","#{org_field_name}_end") + input_field = ["start","end"].map.with_index{|f,i| + "<%=t(\"#{personal_plugin_template}.extend_translate.#{f}_#{field_type}\")%>" + + input_fields[i] + }.join("") + end input_field end def datetime_picker_text(plugin_template,field_name_text,format = "yyyy/MM/dd hh:mm",timepicker = false) @@ -420,7 +461,7 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController default = "DateTime.now.year" when "date" type = "Date" - default = "Date.parse(DateTime.now.to_s)" + default = "Date.today" when "time" type = "String" default = "\"\"" @@ -436,11 +477,23 @@ class Admin::PersonalPluginFieldsController < OrbitMemberController field_name = "#{field_name.singularize}_ids" end no_localize_types = ["date","time","date_time","year_month"] - field_text = "field :#{field_name}, :type => #{type}, :default => #{default}" - field_text += ", :localize => true" if field_value[:localize] == "1" && !no_localize_types.include?(field_value[:field_type]) - field_text += ", as: :slug_title" if field_value[:slug_title] == "1" + field_text = "" + if field_value[:periodic_time] == "1" && no_localize_types.include?(field_value[:field_type]) + field_text = [] + field_text[0] = "field :#{field_name}_start, :type => #{type}, :default => #{default}" + field_text[0] += ", :localize => true" if field_value[:localize] == "1" && !no_localize_types.include?(field_value[:field_type]) + field_text[0] += ", as: :slug_title" if field_value[:slug_title] == "1" + field_text[1] = "field :#{field_name}_end, :type => #{type}, :default => #{default}" + field_text[1] += ", :localize => true" if field_value[:localize] == "1" && !no_localize_types.include?(field_value[:field_type]) + field_text[1] += ", as: :slug_title" if field_value[:slug_title] == "1" + else + field_text = "field :#{field_name}, :type => #{type}, :default => #{default}" + field_text += ", :localize => true" if field_value[:localize] == "1" && !no_localize_types.include?(field_value[:field_type]) + field_text += ", as: :slug_title" if field_value[:slug_title] == "1" + end field_text end + fields_text = fields_text.flatten fields_text.select{|t| t.present?}.join("\r\n") end def replace_dirs(dirs,is_array = true) diff --git a/app/helpers/admin/personal_plugin_fields_helper.rb b/app/helpers/admin/personal_plugin_fields_helper.rb index 8db3643..73999f7 100644 --- a/app/helpers/admin/personal_plugin_fields_helper.rb +++ b/app/helpers/admin/personal_plugin_fields_helper.rb @@ -1,4 +1,5 @@ module Admin::PersonalPluginFieldsHelper + include OrbitBackendHelper def thead_field(field) return I18n.t("personal_plugin_generator.#{field}") end diff --git a/app/models/personal_plugin_field.rb b/app/models/personal_plugin_field.rb index eb0549c..a4a6728 100644 --- a/app/models/personal_plugin_field.rb +++ b/app/models/personal_plugin_field.rb @@ -15,7 +15,7 @@ class PersonalPluginField field :log_text, :type => String, :default => "" field :fields_order, :type => Hash, :default => {} field :copy_id - before_save :change_extensions,:check_modal_name + before_save :change_extensions,:check_modal_name,:auto_add_display_fields after_destroy do delete_plugin("downloaded_extensions.rb",self.module_name) restart_server @@ -80,6 +80,25 @@ class PersonalPluginField return false end end + def auto_add_display_fields + change_primary_fields = self.primary_modal_fields.map{|f| f["field_name"]} - self.primary_modal_fields_was.map{|f| f["field_name"]} + change_related_fields = [] + self.related_modal_fields.each_with_index do |field_values,i| + old_field_values = self.related_modal_fields_was[i].to_a rescue [] + related_modal_name = self.related_modal_name[i].to_s + change_related_fields = change_related_fields + (field_values.map{|f| f["field_name"]} - old_field_values.map{|f| f["field_name"]}).select{|f| f.present?}.map{|f| "#{related_modal_name}.#{f}"} + end + change_fields = change_primary_fields + change_related_fields + change_fields = change_fields.select{|f| f.present?} + if change_fields.count > 0 + ["index","profile"].each do |f| + self.backend_fields[f] = self.backend_fields[f].to_a + change_fields + end + ["index","show","member_show"].each do |f| + self.frontend_fields[f] = self.frontend_fields[f].to_a + change_fields + end + end + end def self.get_modal_names name_routes = Rails.application.routes.named_routes.map{|k,v| k}.select{|s| s.to_s[0..4] == "admin"}.map{|s| s.to_s.split('admin_').last} modal_names = name_routes.map{|n| n.classify}.select{|n| (n.constantize rescue nil)} diff --git a/app/views/admin/personal_plugin_fields/_form.html.erb b/app/views/admin/personal_plugin_fields/_form.html.erb index 56a7051..aaed93a 100644 --- a/app/views/admin/personal_plugin_fields/_form.html.erb +++ b/app/views/admin/personal_plugin_fields/_form.html.erb @@ -172,9 +172,9 @@ }) <% related_modal_fields_html = "

#{thead_field("related_modal_name")} :

" - related_modal_fields_html += render(:partial => 'render_table',locals: {:f => f,:root_name => "related_modal_fields][new_index",:field_values => [nil],:field_types => field_types1}) + related_modal_fields_html += render(:partial => 'render_table',locals: {:f => f,:root_name => "related_modal_fields][new_index",:field_values => [nil],:field_types => field_types1,:@include_blank=>false}) related_modal_fields_html += "
" - primary_modal_field_html = render(:partial => 'render_table',locals: {:f => f,:root_name => "primary_modal_fields",:field_values => [nil],:field_types => field_types}) + primary_modal_field_html = render(:partial => 'render_table',locals: {:f => f,:root_name => "primary_modal_fields",:field_values => [nil],:field_types => field_types,:@include_blank=>false}) %> var related_modal_fields_html = "<%= escape_javascript(related_modal_fields_html.html_safe) %>"; var primary_modal_field_html = $("<%= escape_javascript(primary_modal_field_html.html_safe) %>").find("tbody tr")[0].outerHTML; diff --git a/app/views/admin/personal_plugin_fields/_render_table.html.erb b/app/views/admin/personal_plugin_fields/_render_table.html.erb index 04c6857..7acddbe 100644 --- a/app/views/admin/personal_plugin_fields/_render_table.html.erb +++ b/app/views/admin/personal_plugin_fields/_render_table.html.erb @@ -6,6 +6,7 @@ <%= thead_field("field_type") %> <%= thead_field("localize") %> <%= thead_field("slug_title") %> + <%= thead_field("periodic_time") %> <%= f.fields_for root_name do |f| %> @@ -32,6 +33,10 @@ <%= hidden_field_tag "#{f.object_name}[#{i}][slug_title]", "0",:id=>nil %> <%= check_box_tag "#{f.object_name}[#{i}][slug_title]", "1" , (field_value["slug_title"] == "1" rescue false),:id=>nil,:class=>"slug_title" %> + + <%= hidden_field_tag "#{f.object_name}[#{i}][periodic_time]", "0",:id=>nil %> + <%= check_box_tag "#{f.object_name}[#{i}][periodic_time]", "1" , (field_value["periodic_time"] == "1" rescue false),:id=>nil,:class=>"periodic_time" %> + <% end %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 8c00dd2..02eea35 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -46,4 +46,5 @@ en: goto: "Go to " generating_plugin: Generating plugin please_change_module_name: "Please change module name(This name already exist)." - copy: Copy \ No newline at end of file + copy: Copy + periodic_time: Periodic time \ No newline at end of file diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 04df5f9..a4d0797 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -46,4 +46,5 @@ zh_tw: goto: "前往 " generating_plugin: 正在生成個人外掛中... please_change_module_name: "請更改模組名稱(此名稱已存在)" - copy: 複製 \ No newline at end of file + copy: 複製 + periodic_time: 週期性時間 \ No newline at end of file diff --git a/template_generator/app/helpers/admin/plugin_templates_helper.rb b/template_generator/app/helpers/admin/plugin_templates_helper.rb index 4c3d361..9cf82c4 100644 --- a/template_generator/app/helpers/admin/plugin_templates_helper.rb +++ b/template_generator/app/helpers/admin/plugin_templates_helper.rb @@ -27,12 +27,24 @@ module Admin::PluginTemplatesHelper d = {} d["name"] = role.title mps = role.member_profile_ids - d["data"] = PluginTemplate.where(:analysis_field_name.gte => analysis_field_name_start, :analysis_field_name.lte => analysis_field_name_end, :member_profile_id.in => mps) rescue [] + d["data"] = filter_data(PluginTemplate, analysis_field_name_start, analysis_field_name_end, mps) data << d end return data end - + def filter_data(data,analysis_field_name_start,analysis_field_name_end,mps = nil) + result = [] + if @periodic + all_ids = data.all.pluck(:id) rescue [] + out_of_range_ids1 = data.where(:analysis_field_name_start.gt => analysis_field_name_end).pluck(:id) rescue [] + out_of_range_ids2 = data.where(:analysis_field_name_end.lt => analysis_field_name_start).pluck(:id) rescue [] + result = data.where(:id.in=>(all_ids - out_of_range_ids1 - out_of_range_ids2)) rescue [] + else + result = data.where(:analysis_field_name.gte => analysis_field_name_start, :analysis_field_name.lte => analysis_field_name_end) rescue [] + end + result = result.where(:member_profile_id.in => mps) rescue [] unless mps.nil? + return result + end def get_chart_data(analysis_field_name_start,analysis_field_name_end,role,type,timezone) analysis_field_name_start = parse_date_time_field("analysis_field_name",analysis_field_name_start,timezone) analysis_field_name_end = parse_date_time_field("analysis_field_name",analysis_field_name_end,timezone) @@ -71,7 +83,8 @@ parse_again_end current_analysis_field_name = analysis_field_name current_analysis_field_name = analysis_field_name.strftime("%H:%M") if time_fields.include?("analysis_field_name") next_analysis_field_name = next_analysis_field_name.strftime("%H:%M") if time_fields.include?("analysis_field_name") - t = jl.plugin_templates.where(:analysis_field_name.gte => current_analysis_field_name, :analysis_field_name.lt => next_analysis_field_name, :member_profile_id.in => mps).count rescue 0 + t = filter_data(jl.plugin_templates, current_analysis_field_name, next_analysis_field_name, mps) + if current_analysis_field_name.class == DateTime current_analysis_field_name = display_date_time(current_analysis_field_name,timezone,iterate_step) end @@ -87,13 +100,13 @@ parse_again_end next_analysis_field_name = next_analysis_field_name.strftime("%H:%M") if time_fields.include?("analysis_field_name") case type when "default" - t = PluginTemplate.where(:analysis_field_name.gte => current_analysis_field_name, :analysis_field_name.lt => next_analysis_field_name, :member_profile_id.in => mps).count rescue 0 + t = filter_data(PluginTemplate, current_analysis_field_name, next_analysis_field_name, mps).count rescue 0 parse_again_start when "plugin_template_related" - t = PluginTemplate.where(:analysis_field_name.gte => current_analysis_field_name, :analysis_field_name.lt => next_analysis_field_name, :member_profile_id.in => mps, :plugin_template_related_id => nil).count rescue 0 + t = filter_data(PluginTemplate, current_analysis_field_name, next_analysis_field_name, mps).where(:plugin_template_related_id => nil).count rescue 0 parse_again_end else - t = PluginTemplate.where(:analysis_field_name.gte => current_analysis_field_name, :analysis_field_name.lt => next_analysis_field_name, :member_profile_id.in => mps).count rescue 0 + t = filter_data(PluginTemplate, current_analysis_field_name, next_analysis_field_name, mps).count rescue 0 end current_analysis_field_name = current_analysis_field_name.new_offset(timezone) if current_analysis_field_name.class == DateTime if current_analysis_field_name.class == DateTime @@ -106,11 +119,16 @@ parse_again_end end def parse_date_time_field(field,value,timezone="+08:00") time_fields = time_fields_text + type = PluginTemplate.fields[field].type rescue nil + if type.nil? + @periodic = true + type = PluginTemplate.fields[field + "_start"].type + end if time_fields.include?(field) parse_time(value,timezone) - elsif PluginTemplate.fields[field].type == Integer + elsif type == Integer value.to_i - elsif PluginTemplate.fields[field].type == Date + elsif type == Date Date.parse(value) else DateTime.parse(value+timezone).utc diff --git a/template_generator/app/models/plugin_template.rb b/template_generator/app/models/plugin_template.rb index e554d44..6acd6c8 100644 --- a/template_generator/app/models/plugin_template.rb +++ b/template_generator/app/models/plugin_template.rb @@ -21,6 +21,9 @@ parse_again_end parse_again_start member_methods_define +parse_again_end +parse_again_start + periodic_methods_define parse_again_end before_save do before_save_codes diff --git a/template_generator/app/models/plugin_template_related.rb b/template_generator/app/models/plugin_template_related.rb index 5489453..c058814 100644 --- a/template_generator/app/models/plugin_template_related.rb +++ b/template_generator/app/models/plugin_template_related.rb @@ -5,4 +5,7 @@ class PluginTemplateRelated col_related_fields has_many :plugin_templates +parse_again_start + related_periodic_methods_define +parse_again_end end \ No newline at end of file diff --git a/template_generator/app/views/admin/plugin_templates/download_excel.xlsx.axlsx b/template_generator/app/views/admin/plugin_templates/download_excel.xlsx.axlsx index 4bea5ad..f67de5d 100644 --- a/template_generator/app/views/admin/plugin_templates/download_excel.xlsx.axlsx +++ b/template_generator/app/views/admin/plugin_templates/download_excel.xlsx.axlsx @@ -7,7 +7,7 @@ wb = xlsx_package.workbook heading = sheet.styles.add_style(:b => true, :locked => true) - row = [t("personal_plugin_template.author_name_translation")] + row = [t("personal_plugin_template.member_profile")] parse_again_start @site_in_use_locales.each do |locale| row << t("personal_plugin_template.locale_fields") + " - " + t(locale.to_s)