namespace :announcement_link_widget do desc "Combination of widgets" task :make_cache,[:url] => :environment do |task,args| I18n.available_locales.each do |locale| OrbitHelper.set_site_locale locale announcement_app = ModuleApp.find_by_key("announcement") link_app = ModuleApp.find_by_key("web_resource") OrbitHelper.override_widget_module_app("announcement") link_app_tags = link_app.tags common_tags = [] an_tags = [] l_tags = [] announcement_app.tags.each do |tag| if !tag.name.nil? ct = link_app_tags.where(:name => tag.name).first rescue nil if !ct.nil? common_tags << ct an_tags << tag l_tags << ct end end end common_tags_frontend = common_tags.collect do |ct| { "tag-name" => ct.name_translations[locale.to_s], "tag-id" => ct.name.downcase.gsub(" ", "_"), "announcements" => [], "links" => [] } end if common_tags_frontend.size > 0 Bulletin.all.can_display.is_approved.order_by(:postdate=>'desc').each do |ann| intersection = ann.tags & an_tags if intersection.size > 0 intersection.each do |t| name = t.name.downcase.gsub(" ", "_") index = common_tags_frontend.index{|t| t["tag-id"] == name} ctf = common_tags_frontend[index] if ann.title_translations[locale.to_s] != "" ctf["announcements"] << {"title" => HTMLEntities.new.encode(ann.title_translations[locale.to_s]),"link"=>OrbitHelper.widget_item_url(ann.to_param)} end end end end WebLink.all.can_display.order_by(:created_at=>'desc').each do |wl| intersection = wl.tags & l_tags # debugger if intersection.size > 0 intersection.each do |t| name = t.name.downcase.gsub(" ", "_") index = common_tags_frontend.index{|t| t["tag-id"] == name} ctf = common_tags_frontend[index] if wl.title_translations[locale.to_s] != "" ctf["links"] << {"title" => wl.title_translations[locale.to_s],"url" => wl.url_translations[locale.to_s]} end end end end # debugger end ts = { "bulletins" => { "en" => "Bulletin", "zh_tw" => "公告訊息" }, "related_links" => { "en" => "Related Links", "zh_tw" => "相關連結" } } @data = { "tags" => common_tags_frontend, "extras" => { "heading-announcements" => ts["bulletins"][locale.to_s], "heading-links" => ts["related_links"][locale.to_s] } } file = File.join(Rails.root, "public" , "announcement_link_widget_#{locale.to_s}.json") File.open(file,"w") do |f| f.write(@data.to_json) end end end end