From d83bd64c060334f32dc4dd873aaad5b5b3afa21a Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Tue, 4 Nov 2014 15:48:24 +0800 Subject: [PATCH] rake for faster caching --- .../announcement_link_widgets_controller.rb | 83 +++---------------- lib/tasks/announcement_link_widget.rake | 70 ++++++++++++++++ lib/tasks/announcement_link_widget_tasks.rake | 4 - 3 files changed, 82 insertions(+), 75 deletions(-) create mode 100644 lib/tasks/announcement_link_widget.rake delete mode 100644 lib/tasks/announcement_link_widget_tasks.rake diff --git a/app/controllers/announcement_link_widgets_controller.rb b/app/controllers/announcement_link_widgets_controller.rb index 583d17a..b68bd9c 100644 --- a/app/controllers/announcement_link_widgets_controller.rb +++ b/app/controllers/announcement_link_widgets_controller.rb @@ -1,82 +1,23 @@ +require 'json' class AnnouncementLinkWidgetsController < ApplicationController - include PagesHelper - layout false def widget - { - "extras" => {} - } - end - - def render_content - 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, - "tag-id" => ct.name.downcase.gsub(" ", "_"), + data = { + "tags" => [{ + "tag-name" => "", + "tag-id" => "", "announcements" => [], "links" => [] - } - end - - if common_tags_frontend.size > 0 - Bulletin.all.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] - ctf["announcements"] << {"title" => ann.title,"link"=>OrbitHelper.widget_item_url(ann.to_param)} - end - end - end - - WebLink.all.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] - ctf["links"] << {"title" => wl.title,"url" => wl.url} - end - end - end - - # debugger - end - - { - "tags" => common_tags_frontend, + }], "extras" => { "heading-announcements" => "Announcements", "heading-links" => "Links" } } - - end - - def content - OrbitHelper.set_site_locale I18n.locale - OrbitHelper.register_subpart(params["sub_part_id"]) - html = render_widget_for_frontend("announcement_link_widget","render_content","content") - render :html => html.html_safe + f = File.join(Rails.root, "public" , "announcement_link_widget.json") + if File.exists?(f) + contents = File.read('public/announcement_link_widget.json') + data = JSON.parse(contents) + end + data end end \ No newline at end of file diff --git a/lib/tasks/announcement_link_widget.rake b/lib/tasks/announcement_link_widget.rake new file mode 100644 index 0000000..fcae522 --- /dev/null +++ b/lib/tasks/announcement_link_widget.rake @@ -0,0 +1,70 @@ +namespace :announcement_link_widget do + desc "Combination of widgets" + task :make_cache,[:url] => :environment do |task,args| + 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, + "tag-id" => ct.name.downcase.gsub(" ", "_"), + "announcements" => [], + "links" => [] + } + end + + if common_tags_frontend.size > 0 + Bulletin.all.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] + ctf["announcements"] << {"title" => ann.title,"link"=>OrbitHelper.widget_item_url(ann.to_param)} + end + end + end + + WebLink.all.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] + ctf["links"] << {"title" => wl.title,"url" => wl.url} + end + end + end + + # debugger + end + + @data = { + "tags" => common_tags_frontend, + "extras" => { + "heading-announcements" => "Announcements", + "heading-links" => "Links" + } + } + File.open("public/announcement_link_widget.json","w") do |f| + f.write(@data.to_json) + end + end +end \ No newline at end of file diff --git a/lib/tasks/announcement_link_widget_tasks.rake b/lib/tasks/announcement_link_widget_tasks.rake deleted file mode 100644 index 741d409..0000000 --- a/lib/tasks/announcement_link_widget_tasks.rake +++ /dev/null @@ -1,4 +0,0 @@ -# desc "Explaining what the task does" -# task :announcement_link_widget do -# # Task goes here -# end