97 lines
4.1 KiB
Ruby
97 lines
4.1 KiB
Ruby
class PersonalPluginField
|
|
include Mongoid::Document
|
|
include Mongoid::Timestamps
|
|
include OrbitModel::Status
|
|
include MemberHelper
|
|
field :author_name, :type => String, :default => "",:localize => true
|
|
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 => {}
|
|
field :log_text, :type => String, :default => ""
|
|
field :fields_order, :type => Hash, :default => {}
|
|
field :copy_id
|
|
before_save :change_extensions,:check_modal_name
|
|
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
|
|
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 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
|
|
end
|