personal_plugin_generator/app/models/personal_plugin_field.rb

117 lines
5.3 KiB
Ruby
Raw Normal View History

2021-02-27 04:19:24 +00:00
class PersonalPluginField
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include MemberHelper
2021-03-01 15:44:52 +00:00
field :author_name, :type => String, :default => "",:localize => true
2021-02-27 04:19:24 +00:00
field :title, :type => String, :default => "",:localize => true
field :module_name, :type => String, :default => ""
field :primary_modal_name, :type => String, :default => ""
field :related_modal_name, :type => Array, :default => []
field :primary_modal_fields, :type => Array, :default => []
field :related_modal_fields, :type => Array, :default => [] #ex: [[],[]]
field :backend_fields, :type => Hash, :default => {}
field :frontend_fields, :type => Hash, :default => {}
2021-03-03 12:24:10 +00:00
field :log_text, :type => String, :default => ""
2021-03-04 02:21:22 +00:00
field :fields_order, :type => Hash, :default => {}
field :copy_id
2021-03-08 14:56:57 +00:00
field :finished, :type => Boolean, :default => false
before_save :change_extensions,:check_modal_name,:auto_add_display_fields
after_destroy do
delete_plugin("downloaded_extensions.rb",self.module_name)
restart_server
end
before_create do
if self.copy_id
copy_item = self.class.find(copy_id) rescue nil
if !copy_item.nil?
self.backend_fields = copy_item.backend_fields
self.frontend_fields = copy_item.frontend_fields
self.fields_order = copy_item.fields_order
end
end
end
2021-03-03 12:24:10 +00:00
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
def change_extensions
if !check_plugin_exist
return false
else
extention_file = "downloaded_extensions.rb"
if self.module_name_changed?
if self.module_name_was.present?
delete_plugin(extention_file,module_name_was)
end
end
return true
end
end
def delete_plugin(extention_file,plugin_name)
txt = File.read(extention_file) rescue nil
return if txt.nil?
txt_change = txt.gsub(/^[\n]*gem\s*["']#{plugin_name}["'].*\n/,'')
if txt_change != txt
File.open(extention_file,'w+') do |f|
f.write(txt_change)
end
end
end
def check_modal_name
primary_modal_names = self.class.where(:id.ne=>self.id).pluck(:primary_modal_name)
related_modal_names = self.class.where(:id.ne=>self.id).pluck(:related_modal_name).flatten.uniq
other_modal_names = primary_modal_names + related_modal_names
all_modal_names = self.class.get_modal_names_cache
except_modals = Dir.glob("tmp/#{self.module_name}/app/models/*.rb").map{|f|
fn = File.basename(f)
fn.slice(0,fn.length - 3)
}
all_modal_names = all_modal_names - except_modals
all_modal_names = all_modal_names + other_modal_names
all_modal_names = all_modal_names.uniq
self_modal_names = ([self.primary_modal_name]+self.related_modal_name).flatten
if self_modal_names.select{|n| all_modal_names.include?(n)}.count != 0
return false
end
end
def auto_add_display_fields
2021-03-08 14:56:57 +00:00
change_primary_fields = self.primary_modal_fields.map{|f| f["field_name"]} - self.primary_modal_fields_was.to_a.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
2021-03-08 14:56:57 +00:00
change_related_fields = change_related_fields + (field_values.to_a.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)}
modal_names = modal_names.map{|n| n.constantize}.select{|n| n.class == Class }.uniq
@@modal_names = modal_names.map{|n| n.to_s.underscore}
end
def self.get_modal_names_cache
@@modal_names ||= get_modal_names
end
def restart_server
%x(kill -s USR2 `cat tmp/pids/unicorn.pid`)
sleep 2
end
2021-02-27 04:19:24 +00:00
end