announcement-link-widget/lib/tasks/announcement_link_widget.rake

87 lines
2.8 KiB
Ruby
Raw Normal View History

2014-11-04 07:48:24 +00:00
namespace :announcement_link_widget do
desc "Combination of widgets"
task :make_cache,[:url] => :environment do |task,args|
2014-11-04 09:28:43 +00:00
I18n.available_locales.each do |locale|
OrbitHelper.set_site_locale locale
announcement_app = ModuleApp.find_by_key("announcement")
2014-11-04 07:48:24 +00:00
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|
{
2014-11-04 09:28:43 +00:00
"tag-name" => ct.name_translations[locale.to_s],
2014-11-04 07:48:24 +00:00
"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|
2014-11-04 07:48:24 +00:00
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] != ""
2015-03-17 06:52:27 +00:00
ctf["announcements"] << {"title" => HTMLEntities.new.encode(ann.title_translations[locale.to_s]),"link"=>OrbitHelper.widget_item_url(ann.to_param)}
end
2014-11-04 07:48:24 +00:00
end
end
end
2014-11-04 13:11:47 +00:00
WebLink.all.can_display.order_by(:created_at=>'desc').each do |wl|
2014-11-04 07:48:24 +00:00
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
2014-11-04 07:48:24 +00:00
end
end
end
# debugger
end
2014-11-05 09:48:39 +00:00
ts = {
2014-11-05 10:07:03 +00:00
"bulletins" => {
2014-11-05 09:48:39 +00:00
"en" => "Bulletin",
"zh_tw" => "公告訊息"
},
"related_links" => {
"en" => "Related Links",
"zh_tw" => "相關連結"
}
}
2014-11-04 07:48:24 +00:00
@data = {
"tags" => common_tags_frontend,
"extras" => {
2014-11-05 09:48:39 +00:00
"heading-announcements" => ts["bulletins"][locale.to_s],
"heading-links" => ts["related_links"][locale.to_s]
2014-11-04 07:48:24 +00:00
}
}
2014-11-04 09:28:43 +00:00
file = File.join(Rails.root, "public" , "announcement_link_widget_#{locale.to_s}.json")
2014-11-04 08:11:05 +00:00
File.open(file,"w") do |f|
2014-11-04 07:48:24 +00:00
f.write(@data.to_json)
end
2014-11-04 09:28:43 +00:00
end
2014-11-04 07:48:24 +00:00
end
end