personal_plugin_generator/app/models/personal_plugin_field.rb

160 lines
6.9 KiB
Ruby

class PersonalPluginField
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include MemberHelper
field :one_line_title_format, :type => String, :default => ""
field :enable_one_line_title, :type => Boolean, :default => false
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
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
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 get_sorted_fields(root_name, page_name, tds=nil)
if self.fields_order.present?
fields_order = (0...tds.count).to_a
if (self.fields_order["#{root_name}_#{page_name}"].present? rescue false)
self.fields_order["#{root_name}_#{page_name}"].to_a.each_with_index do |order,i|
fields_order[i] = order.to_i
end
end
if tds.respond_to?(:values)
tds = tds.values
end
tds = tds.sort_by.with_index{|td,i| fields_order[i]}
else
field_values = self[root_name][page_name] rescue []
if field_values.present?
if tds.respond_to?(:values)
new_tds = {}
field_values.each do |field_value|
new_tds[field_value] = tds[field_value] if tds[field_value]
end
tds.each do |k,v|
if new_tds[k].nil?
new_tds[k] = v
end
end
tds = new_tds.values
else
new_tds = field_values.clone
tds.each do |v|
new_tds << v unless new_tds.include?(v)
end
tds = new_tds
end
else
if tds.respond_to?(:values)
tds = tds.values
end
end
end
tds
end
def auto_add_display_fields
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
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
end