recruitment_mod/lib/recruitment_mod/engine.rb

218 lines
10 KiB
Ruby

module RecruitmentMod
class Engine < ::Rails::Engine
initializer "recruitment_mod" do
Rails.application.config.to_prepare do
require "yaml"
begin
translate_data = Dir["#{RecruitmentMod::Engine.root}/config/locales/*.yml"] .map{|yaml_file| YAML.load(File.read(yaml_file))}
data = {}
key1 = {}
key2 = {}
key3 = {}
key4 = {}
key5 = {}
key1_attr = []
key2_attr = []
key3_attr = []
key4_attr = []
data_item = {}
key_item1 = {}
key_item2 = {}
key_item3 = {}
key_item4 = {}
value_item1 = {}
value_item2 = {}
value_item3 = {}
value_item4 = {}
key1_options = ['small_size','medium_size','orignal_size']
key2_options = ['not_enable_tabs','enable_tabs_with_categories_include_all','enable_tabs_with_categories']
key3_options = ['default','upper_left','lower_left','upper_right','lower_right']
key4_options = ['the_same_as_data_count','display_all_in_other_tabs']
#After fix I18n.load_path, translation can work there
yes_no_options = ['no_','yes_'].map{|v| I18n.available_locales.map{|k| I18n.with_locale(k){[k,I18n.t(v)]}}.to_h}
key1_options.each_with_index do |k,i|
key1_attr[i] = {}
end
key2_options.each_with_index do |k,i|
key2_attr[i] = {}
end
key3_options.each_with_index do |k,i|
key3_attr[i] = {}
end
key4_options.each_with_index do |k,i|
key4_attr[i] = {}
end
translate_data.each do |t_data|
v = t_data.values
k = t_data.keys[0]
key1[k] = v[0]['recruitment']['picture_showing_size']
key2[k] = v[0]['recruitment']['tabs_options']
key3[k] = v[0]['recruitment']['read_more_position_options']
key4[k] = v[0]['recruitment']['all_tabs_setting']
key5[k] = v[0]['recruitment']['show_today_data_first']
key1_options.each_with_index do |kk,i|
key1_attr[i][k] = v[0]['recruitment'][kk]
end
key2_options.each_with_index do |kk,i|
key2_attr[i][k] = v[0]['recruitment'][kk]
end
key3_options.each_with_index do |kk,i|
key3_attr[i][k] = v[0]['recruitment'][kk]
end
key4_options.each_with_index do |kk,i|
key4_attr[i][k] = v[0]['recruitment'][kk]
end
key_item1[k] = v[0]['recruitment']['showing_back_and_next']
key_item2[k] = v[0]['recruitment']['enable_search']
key_item3[k] = v[0]['recruitment']['ad_banner_location']
value_item1[k] = v[0]['recruitment']['not_show']
value_item2[k] = v[0]['recruitment']['show_bottom']
value_item3[k] = v[0]['recruitment']['show_top']
end
key_item4 = key5
data[key1] = key1_attr
data[key2] = key2_attr
data[key3] = key3_attr
data[key4] = key4_attr
data[key5] = yes_no_options
data_item[key_item1] = [value_item1,value_item2,value_item3]
data_item[key_item2] = yes_no_options
data_item[key_item3] = [value_item2,value_item3]
data_item[key_item4] = yes_no_options
data_item[key1] = key1_attr
if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console')
require File.expand_path('../../../app/models/recruitment_cache', __FILE__)
if defined?(RecruitmentCache)
RecruitmentCache.destroy_all
end
require File.expand_path('../../../app/models/recruitment_custom_title', __FILE__)
if defined? RecruitmentCustomTitle
RecruitmentCustomTitle.get_map
end
end
rescue => e
puts ['error in recruitment',e,e.backtrace]
end
if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console')
Thread.new do
begin
Migrate.call
rescue => e
puts ['recruitment_mod',e, e.backtrace]
end
end
end
OrbitApp.registration "recruitment_mod", :type => "ModuleApp" do
db = ::Mongoid::Sessions.default
collection = db[:module_apps]
update_results = collection.update_many({key: 'recruitment'},'$set'=>{key: 'recruitment_mod', title: 'recruitment_mod'})
if update_results.n != 0
puts "Updating recruitment to recruitment_mod!"
collection = db[:pages]
collection.update_many({:module=> 'recruitment'},'$set'=>{:module=> 'recruitment_mod'})
collection = db[:sub_parts]
collection.update_many({:module=> 'recruitment'},'$set'=>{:module=> 'recruitment_mod'})
template_path = Rails.root.to_s + '/app/templates'
all_template = Dir.glob(template_path+'/*/')
all_template.each do |folder|
Bundler.with_clean_env{system ("mkdir -p #{folder}modules/recruitment_mod; cp -rf #{folder}modules/recruitment/* #{folder}modules/recruitment_mod/. && rm -rf #{folder}modules/recruitment_mod/recruitment")}
end
end
module_label "recruitment.recruitment"
base_url File.expand_path File.dirname(__FILE__)
widget_methods ["widget","random_recruitment_widget", "tag_cloud"]
widget_settings [{"data_count"=>30}]
taggable "Recruitment"
hashtaggable "Recruitment"
categorizable
authorizable
frontend_enabled
feeds_url "/xhr/recruitment/feeds"
feeds_time_field (::RecruitmentHelper.is_postdate_sort_first ? ['postdate', 'event_date'] : ['event_date', 'postdate'])
data_count 1..30
begin
show_options data
show_option_items data_item
rescue => e
puts ['there_was_no_show_option_method',e]
end
if File.basename($0) != 'rake'
begin
avoid_page_cache RecruitmentCache
avoid_page_cache RecruitmentFeedCache
avoid_page_cache RecruitmentFeed
rescue => e
puts ["avoid_page_cache", e.to_s]
end
end
side_bar do
head_label_i18n 'recruitment.recruitment', icon_class: "icons-megaphone"
available_for "users"
active_for_controllers (['admin/recruitment'])
head_link_path "admin_recruitment_index_path"
context_link 'recruitment.all_articles',
:link_path=>"admin_recruitment_index_path" ,
:priority=>1,
:active_for_action=>{'admin/recruitment'=>'index'},
:available_for => 'users'
context_link 'recruitment.manually_sort',
:link_path=>"edit_sort_admin_recruitment_index_path" ,
:priority=>2,
:active_for_action=>{'admin/recruitment'=>'edit_sort'},
:available_for => 'managers'
context_link 'new_',
:link_path=>"new_admin_recruitment_path" ,
:priority=>2,
:active_for_action=>{'admin/recruitment'=>'new'},
:available_for => 'sub_managers'
context_link 'categories',
:link_path=>"admin_module_app_categories_path" ,
:link_arg=>"{:module_app_id=>ModuleApp.find_by(:key=>'recruitment_mod').id}",
:priority=>3,
:active_for_action=>{'admin/recruitment'=>'categories'},
:active_for_category => 'RecruitmentModule',
:available_for => 'managers'
context_link 'tags',
:link_path=>"admin_module_app_tags_path" ,
:link_arg=>"{:module_app_id=>ModuleApp.find_by(:key=>'recruitment_mod').id}",
:priority=>4,
:active_for_action=>{'admin/recruitment'=>'tags'},
:active_for_tag => 'RecruitmentModule',
:available_for => 'managers'
context_link 'recruitment.custom_fields_title',
:link_path=>"custom_fields_title_admin_recruitment_index_path" ,
:priority=>5,
:active_for_action=>{'admin/recruitment'=>'custom_fields_title'},
:available_for => 'managers'
context_link 'recruitment.feed_list',
:link_path=>"admin_recruitment_feed_path" ,
:priority=>6,
:active_for_action=>{'admin/recruitment'=>'feed'},
:available_for => 'managers'
context_link 'recruitment.import',
:link_path=>"admin_recruitment_import_path" ,
:priority=>7,
:active_for_action=>{'admin/recruitment'=>'import'},
:available_for => 'managers'
context_link 'recruitment.settings',
:link_path=>"admin_recruitment_settings_path" ,
:priority=>8,
:active_for_action=>{'admin/recruitment'=>'settings'},
:available_for => 'managers'
end
end
# temp = YAML.load_file(File.join(Rails.root,"config","mongoid.yml"))
# dbsettings = temp["production"]["sessions"]["default"]
# s = Moped::Session.new(dbsettings["hosts"])
# s.use dbsettings["database"]
# s[:bulletins].indexes.create({expirable_created_at: 1},{ expireAfterSeconds: 180 })
end
end
end
end