From 6feba1e50cbf762ca9bee9ec1ddcf45ac76acec9 Mon Sep 17 00:00:00 2001 From: bohung Date: Wed, 3 Mar 2021 20:24:10 +0800 Subject: [PATCH] Fix bugs. Add copy page. --- .../personal_plugin_fields_controller.rb | 46 +++++++++++++++---- app/models/personal_plugin_field.rb | 12 +++++ .../personal_plugin_fields/_form.html.erb | 15 +++++- .../_personal_plugin_fields.html.erb | 4 +- .../_render_fields_check_table.html.erb | 4 ++ .../personal_plugin_fields/copy.html.erb | 5 ++ .../personal_plugin_fields/index.html.erb | 35 +++++++++++++- config/locales/en.yml | 6 ++- config/locales/zh_tw.yml | 6 ++- config/routes.rb | 6 ++- .../admin/plugin_templates/_form.html.erb | 2 +- 11 files changed, 123 insertions(+), 18 deletions(-) create mode 100644 app/views/admin/personal_plugin_fields/copy.html.erb diff --git a/app/controllers/admin/personal_plugin_fields_controller.rb b/app/controllers/admin/personal_plugin_fields_controller.rb index 1f8c95f..42d7f31 100644 --- a/app/controllers/admin/personal_plugin_fields_controller.rb +++ b/app/controllers/admin/personal_plugin_fields_controller.rb @@ -11,7 +11,21 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController @member = MemberProfile.find_by(:uid=>params['uid']) rescue nil @personal_plugin_field = PersonalPluginField.new end - + def copy + @member = MemberProfile.find_by(:uid=>params['uid']) rescue nil + attributes = PersonalPluginField.find(params[:personal_plugin_field_id]).attributes rescue {} + attributes = attributes.except("_id") + copy_attributes = {} + attributes.each do |k,v| + if (PersonalPluginField.fields[k].options[:localize] rescue false) + copy_attributes["#{k}_translations"] = v + else + copy_attributes[k] = v + end + end + #render :html => attributes and return + @personal_plugin_field = PersonalPluginField.new(copy_attributes) + end def create personal_plugin_field = PersonalPluginField.create(personal_plugin_field_params) redirect_to params[:referer_url] @@ -75,7 +89,7 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController I18n.with_locale(locale) do author_name_translation = I18n.t("personal_plugins.author") if author_name_translation.blank? end - translate_hash["author_name_translation"] = author_name_translation + translate_hash["member_profile"] = author_name_translation col_name_translate_yaml = "" if translate_hash.present? col_name_translate_yaml = translate_hash.to_yaml.gsub("---\n", '') @@ -111,7 +125,7 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController " \r\n " elsif field_name.include?(".") "<%= #{plugin_template}.#{field_name} rescue \"\" %>" - elsif fields.include?(field_name) || plugin_template_related_members.include?(field_name) #file or link or member + elsif fields.include?(field_name) || plugin_template_related_members.include?(field_name) || field_name == "member_profile" #file or link or member "<%= #{plugin_template}.display_field(\"#{field_name}\").html_safe rescue \"\" %>" else "<%= #{plugin_template}.#{field_name} %>" @@ -184,6 +198,9 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController "#{@blank_text}}", "#{@blank_text}join_text = (text_only rescue false) ? \",\" : \"
\"", "#{@blank_text}value = value.join(join_text)", + "elsif field == \"member_profile\" && value.present?", + "#{@blank_text}path = OrbitHelper.url_to_plugin_show(value.to_param, 'member') rescue '#'", + "#{@blank_text}value = \"\#{value.name}\"", "end", "strftime_hash = #{datetime_fields}", "if strftime_hash.keys.include?(field)", @@ -274,12 +291,16 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController replace_dirs(dirs,false) #end copy_templates("#{cp_template_dir_path}modules/#{@personal_plugin_field.module_name}/") - render :html => @logs#@match_pattern#@logs.join("
").html_safe#@match_pattern + #render :html => @logs#@match_pattern#@logs.join("
").html_safe#@match_pattern + @personal_plugin_field.update(:log_text=>"") + render :json => {:success=>true,:url=>"/admin/#{plugin_template.pluralize}/",:name=>@personal_plugin_field.title} rescue => e @match_pattern = {"parse_again_start"=>"","parse_again_end"=>"","col_name_translate_yaml"=>""} replace_files(files) replace_dirs(dirs,false) - render :html => e.backtrace.join("
") + error = e.backtrace.join("
") + @personal_plugin_field.update(:log_text=>error) + render :json => {:success=>false,:error=>error} end end def copy_templates(source) @@ -405,7 +426,6 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController end sub_dirs = Dir.glob("#{dir}/*/") files = Dir.glob("#{dir}/*").select { |fn| File.file?(fn) } - @logs << files.to_s if current_index replace_files(files,current_index) if sub_dirs.present? sub_dirs.each do |sub_dir| @@ -450,7 +470,6 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController if is_array file_text = replace_text_with_pattern(file_text,false,i,nil,true) elsif current_index - @logs << sub_file file_text = replace_text_with_pattern(file_text,false,current_index,nil,true) else file_text = replace_text_with_pattern(file_text) @@ -479,9 +498,6 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController new_text = gsub_text_by_key_value(new_text,k,vv) end elsif vv.class == Array - if i - @logs << i - end if is_file if v.count == 0 new_text = "" @@ -524,6 +540,16 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController text = text.gsub(k.classify + "s"){|ff| v.classify.pluralize.gsub("\n","\n#{@blank_texts[i]}")} text = text.gsub(k.classify){|ff| v.classify.gsub("\n","\n#{@blank_texts[i]}")} end + def get_all_gem_plugins + extention_files = ["downloaded_extensions.rb","built_in_extensions.rb"] + gem_plugins = extention_files.map{|f| (File.read(f).scan(/\n\s*gem\s*["'](.*)["']\s*,/).flatten rescue [])}.flatten + end + def check_plugin_exist + gem_plugins = get_all_gem_plugins + can_install = !gem_plugins.include?(params[:plugin_name]) + can_install = (PersonalPluginField.find(params[:id]).module_name == params[:plugin_name] rescue false) if !can_install + render :json => {:can_install => can_install } + end private def personal_plugin_field_params personal_plugin_field_params = params.require(:personal_plugin_field).permit! rescue {} diff --git a/app/models/personal_plugin_field.rb b/app/models/personal_plugin_field.rb index 640ffcf..5e72984 100644 --- a/app/models/personal_plugin_field.rb +++ b/app/models/personal_plugin_field.rb @@ -12,4 +12,16 @@ class PersonalPluginField field :related_modal_fields, :type => Array, :default => [] #ex: [[],[]] field :backend_fields, :type => Hash, :default => {} field :frontend_fields, :type => Hash, :default => {} + field :log_text, :type => String, :default => "" + before_save :check_plugin_exist + def get_all_gem_plugins + extention_files = ["downloaded_extensions.rb","built_in_extensions.rb"] + gem_plugins = extention_files.map{|f| (File.read(f).scan(/\n\s*gem\s*["'](.*)["']\s*,/).flatten rescue [])}.flatten + end + def check_plugin_exist + gem_plugins = get_all_gem_plugins + can_install = !gem_plugins.include?(self.module_name) + can_install = (self.class.where(:module_name=>self.module_name,:id.ne=>self.id).count == 0 rescue false) if !can_install + return can_install + end end diff --git a/app/views/admin/personal_plugin_fields/_form.html.erb b/app/views/admin/personal_plugin_fields/_form.html.erb index 42aa1aa..9632ed0 100644 --- a/app/views/admin/personal_plugin_fields/_form.html.erb +++ b/app/views/admin/personal_plugin_fields/_form.html.erb @@ -228,13 +228,26 @@ } }) } + $.ajaxSetup({async: false}); $(".main-forms").submit(function(){ if($("#primary_modal_plane .slug_title:checked").length == 0){ alert("<%=thead_field('please_choose_one_slug_title')%>"); }else if($("#primary_modal_plane .slug_title:checked").length > 1){ alert("<%=thead_field('slug_title_can_only_choose_one')%>"); }else{ - return true; + var can_install; + $.post("<%=check_plugin_exist_admin_personal_plugin_field_path%>",{plugin_name: $("#personal_plugin_field_module_name").val(), id: "<%=f.object.id %>"}).done(function(data){ + console.log(data); + if(data["can_install"]){ + can_install = true; + }else{ + $("#personal_plugin_field_module_name").css('border', '2px solid red'); + alert("<%=thead_field("please_change_module_name")%>"); + window.location.href = "#" + $(".main-forms")[0].id; + can_install = false; + } + }) + return can_install; } $('tr >:last-child').css( 'border', '2px solid red'); return false; diff --git a/app/views/admin/personal_plugin_fields/_personal_plugin_fields.html.erb b/app/views/admin/personal_plugin_fields/_personal_plugin_fields.html.erb index 680f2c2..438a95f 100644 --- a/app/views/admin/personal_plugin_fields/_personal_plugin_fields.html.erb +++ b/app/views/admin/personal_plugin_fields/_personal_plugin_fields.html.erb @@ -1,9 +1,11 @@ <% @personal_plugin_fields.each do |personal_plugin_field| %> <%= personal_plugin_field.title %> + <%= personal_plugin_field.module_name %> <%= link_to t(:edit) ,edit_admin_personal_plugin_field_path(personal_plugin_field.id),:class=> "btn btn-primary" %> <%= link_to thead_field('fields_display_setting') ,admin_personal_plugin_field_fields_setting_path(personal_plugin_field.id),:class=> "btn btn-primary" %> - <%= link_to thead_field('generate_plugin') ,admin_personal_plugin_field_generate_plugin_path(personal_plugin_field.id),:class=> "btn btn-primary" %> + <%= link_to thead_field(:copy) ,admin_personal_plugin_field_copy_path(personal_plugin_field.id),:class=> "btn btn-primary" %> + <%= t(:delete_) %> <% end %> \ No newline at end of file diff --git a/app/views/admin/personal_plugin_fields/_render_fields_check_table.html.erb b/app/views/admin/personal_plugin_fields/_render_fields_check_table.html.erb index a46afcd..ab5cfd3 100644 --- a/app/views/admin/personal_plugin_fields/_render_fields_check_table.html.erb +++ b/app/views/admin/personal_plugin_fields/_render_fields_check_table.html.erb @@ -18,6 +18,9 @@ <%="#{related_modal_name}-#{field_value[:translation_name][I18n.locale] rescue ""}-#{field_value[:field_name]}"%> <% end %> <% end %> + <% author_name_translation = @personal_plugin_field.author_name_translations[locale] rescue "" + author_name_translation = I18n.t("personal_plugins.author") if author_name_translation.blank? %> + <%= author_name_translation %> <% end %> @@ -40,6 +43,7 @@ <% end %> <% end %> + <%= check_box_tag "#{f.object_name}[]", "member_profile" , (object.send(root_name)[page_name].include?("member_profile") rescue false),:id=>nil %> <% end %> <% end %> <% end %> diff --git a/app/views/admin/personal_plugin_fields/copy.html.erb b/app/views/admin/personal_plugin_fields/copy.html.erb new file mode 100644 index 0000000..32ac43c --- /dev/null +++ b/app/views/admin/personal_plugin_fields/copy.html.erb @@ -0,0 +1,5 @@ +<%= form_for @personal_plugin_field, url: admin_personal_plugin_fields_path, html: {class: "form-horizontal main-forms previewable"} do |f| %> +
+ <%= render partial: 'form', locals: {f: f} %> +
+<% end %> \ No newline at end of file diff --git a/app/views/admin/personal_plugin_fields/index.html.erb b/app/views/admin/personal_plugin_fields/index.html.erb index 519b9eb..a89895d 100644 --- a/app/views/admin/personal_plugin_fields/index.html.erb +++ b/app/views/admin/personal_plugin_fields/index.html.erb @@ -2,6 +2,7 @@ <%= thead_field("personal_plugin_name") %> + <%= thead_field("module_name") %> <%= thead_field("action") %> @@ -9,7 +10,14 @@ <%= render 'personal_plugin_fields' %> - +
" style="display: none;"> +
+
+ +
<%=thead_field("generating_plugin")%>
+
+
+
<%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_admin_personal_plugin_field_path, :class => 'btn btn-primary' %> @@ -17,4 +25,27 @@ -
\ No newline at end of file +
+ \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 3295ffe..721bfea 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -40,4 +40,8 @@ en: generate_plugin: Generate plugin member: Member link: Link - analysis: Analysis Page \ No newline at end of file + analysis: Analysis Page + 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 diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index e7ae731..8fecb49 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -40,4 +40,8 @@ zh_tw: generate_plugin: 生成個人外掛 member: 會員 link: 連結 - analysis: 分析頁面 \ No newline at end of file + analysis: 分析頁面 + goto: "前往 " + generating_plugin: 正在生成個人外掛中... + please_change_module_name: "請更改模組名稱(此名稱已存在)" + copy: 複製 \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 626e17b..8b2c537 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,7 +7,11 @@ Rails.application.routes.draw do post 'update_fields_setting' , to: 'personal_plugin_fields#update_fields_setting' patch 'update_fields_setting' , to: 'personal_plugin_fields#update_fields_setting' get 'generate_plugin' , to: 'personal_plugin_fields#generate_plugin' + get 'copy' , to: 'personal_plugin_fields#copy' end - end + resource :personal_plugin_field do + post 'check_plugin_exist' ,to: 'personal_plugin_fields#check_plugin_exist' + end + end end end diff --git a/template_generator/app/views/admin/plugin_templates/_form.html.erb b/template_generator/app/views/admin/plugin_templates/_form.html.erb index 40fb9a7..d543775 100644 --- a/template_generator/app/views/admin/plugin_templates/_form.html.erb +++ b/template_generator/app/views/admin/plugin_templates/_form.html.erb @@ -156,7 +156,7 @@ parse_again_end <% else %>
- +
<% members = !@plugin_template.member_profile.nil? ? @plugin_template.member_profile.to_a : [] %> <%= render partial: 'admin/member_selects/email_selection_box', locals: {field: 'plugin_template[member_profile_id]', email_members: members,index:'0',select_name:'member_profile_id'} %>