forgeplus/app/models/topic.rb

204 lines
6.4 KiB
Ruby

# == Schema Information
#
# Table name: topics
#
# id :integer not null, primary key
# type :string(255)
# title :string(255)
# uuid :integer
# url :string(255)
# order_index :integer
# location :integer default("0")
#
class Topic < ApplicationRecord
enum location: { home: 0, competition: 1, footer: 2 }
default_scope { order(order_index: :desc)}
scope :with_single_type, ->(type){where(type: trans_simpletype_to_classtype(type))}
scope :with_mutil_type, ->(type){where(type: group_class_type_names(type))}
# self.inheritance_column = nil
def self.has_templates?(type)
type_simple_name = Topic.type_simple_name(type)
["ck_case", "scientific_organization"].include?(type_simple_name)
end
def self.base_types
[
# ["forums", "Topic::ActivityForum", "首页平台动态"],
# ["forums", "Topic::ExcellentProject", "首页优秀项目"],
# ["forums", "Topic::ExperienceForum", "首页经验分享"],
# ["forums", "Topic::TechnicalDiscussion", "首页技术讨论"],
# ["forums", "Topic::PinnedForum", "首页精选文章"],
# ["forums", "Topic::CkForum", "创客首页-创客内容"],
# ["forums", "Topic::JsForum", "创客首页-竞赛内容"],
# ["forums", "Topic::GlccNews", "GLCC新闻稿"],
["forums", "Topic::JoinUs", "底部加入我们"],
["banners", "Topic::Banner", "首页banner"],
["banners", "Topic::Card", "开源首页卡片"],
["banners", "Topic::CkBanner", "创客空间卡片"],
["banners", "Topic::AppletBanner", "小程序首页banner"],
# ["logos", "Topic::Cooperator", "首页合作伙伴"],
["logos", "Topic::Organization", "项目首页组织logo"],
["logos", "Topic::ScientificOrganization", "首页/友情链接-科研机构logo"],
["logos", "Topic::School", "首页/友情链接-高校logo"],
# ["logos", "Topic::FriendlyLink", "首页友情链接-科研机构logo"],
["others", "Topic::ServiceTerm", "服务条款"],
]
end
def self.group_class_types(type)
Topic.base_types.select { |s| s[0] == type }
end
def self.group_class_type_names(type)
Topic.group_class_types(type).map{|s| s[1]}
end
def self.group_types_options(type)
Topic.group_class_types(type).map{|s| [s[2],s[1]]}
end
def self.sub_types
[
["Topic::ActivityForum", "activity_forum", "首页平台动态"],
["Topic::Banner", "banner", "首页banner"],
["Topic::Card", "card", "首页卡片内容"],
["Topic::Cooperator", "cooperator", "首页合作伙伴"],
["Topic::Organization", "organization", "首页组织logo"],
["Topic::ScientificOrganization", "scientific_organization", "科研机构logo"],
["Topic::School", "school", "首页高校logo"],
["Topic::ExcellentProject", "excellent_project", "首页优秀项目"],
["Topic::ExperienceForum", "experience_forum", "首页经验分享"],
["Topic::GlccNews", "glcc_news", "GLCC新闻稿"],
["Topic::PinnedForum", "pinned_forum", "首页精选文章"],
["Topic::AppletBanner", "applet_banner", "Applet首页banner"],
["Topic::FriendlyLink", "friendly_link", "底部友情链接"],
["Topic::JoinUs", "Join_us", "底部加入我们"],
["Topic::CkBanner", "ck_banner", "创客空间卡片内容"],
["Topic::TechnicalDiscussion", "technical_discussion", "首页技术讨论"],
["Topic::CkForum", "ck_forum", "创客首页-创客内容"],
["Topic::JsForum", "js_forum", "创客首页-竞赛内容"],
["Topic::ServiceTerm", "service_term", "服务条款"],
["Topic::CkCase", "ck_case", "创客案例"],
]
end
def self.opt_select
Topic.sub_types.map{|s| [s[2],s[0]]}
end
def type_cn_name
filter = Topic.base_types.select { |s| s[1] == self.type }
filter.blank? ? self.type : filter.first[2]
end
def self.type_simple_name(type)
filter = Topic.sub_types.select { |s| s[0] == type }
filter.blank? ? type : filter.first[1]
end
def self.type_filter_cn_name(type)
filter = Topic.sub_types.select { |s| s[0] == type }
filter.blank? ? type : filter.first[2]
end
def type_group_name
filter = Topic.base_types.select { |s| s[1] == self.type }
filter.blank? ? self.type : filter.first[0]
end
def image
image_url('image')
end
def self.trans_simpletype_to_classtype(type)
case type
when 'activity_forum'
'Topic::ActivityForum'
when 'banner'
'Topic::Banner'
when 'card'
'Topic::Card'
when 'cooperator'
'Topic::Cooperator'
when 'organization'
'Topic::Organization'
when 'school'
'Topic::School'
when 'excellent_project'
'Topic::ExcellentProject'
when 'experience_forum'
'Topic::ExperienceForum'
when "glcc_news"
'Topic::GlccNews'
when 'pinned_forum'
'Topic::PinnedForum'
when 'applet_banner'
'Topic::AppletBanner'
when 'friendly_link'
'Topic::FriendlyLink'
when 'join_us'
'Topic::JoinUs'
when 'ck_banner'
'Topic::CkBanner'
when 'technical_discussion'
'Topic::TechnicalDiscussion'
when 'ck_js_forum'
['Topic::CkForum', 'Topic::JsForum']
when 'service_term'
'Topic::ServiceTerm'
when 'scientific_organization'
'Topic::ScientificOrganization'
when 'ck_case'
'Topic::CkCase'
else
type
end
end
private
def get_visitor_data
data = {
visits: 0,
created_time: format_time(Time.now),
from:"other"
}
if self.url.include?("gitlink.org.cn/forums/") || self.url.include?("trustie.net/forums/")
request_memo = Forum::Memos::GetService.call(self.uuid)
data[:visits] = request_memo.nil? ? 0 : request_memo["memo"]["viewed_count"]
data[:created_time] = request_memo.nil? ? format_time(Time.now) : request_memo["memo"]["published_time"]
data[:from] = "forums"
end
if self.url.include?("gitlink.org.cn/zone/") || self.url.include?("trustie.net/zone/")
request_doc = Getway::Cms::GetService.call(self.uuid)
data[:visits] = request_doc.nil? ? 0 : request_doc["data"]["visits"]
data[:created_time] = request_doc.nil? ? format_time(Time.now) : request_doc["data"]["publishTime"]
data[:from] = "zone"
end
data
end
private
def image_url(type)
return nil unless Util::FileManage.exists?(self, type)
Util::FileManage.source_disk_file_url(self, type)
end
end