personal_plugin_generator/template_generator/app/models/plugin_template.rb

110 lines
3.1 KiB
Ruby
Raw Normal View History

2021-02-27 04:19:24 +00:00
class PluginTemplate
2021-03-01 15:44:52 +00:00
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include MemberHelper
include Slug
2021-02-27 04:19:24 +00:00
col_fields
2021-03-01 15:44:52 +00:00
plugin_template_related_files_fields
2021-02-27 04:19:24 +00:00
2021-03-03 07:44:42 +00:00
parse_again_start
2021-03-01 15:44:52 +00:00
belongs_to :plugin_template_related
2021-03-03 07:44:42 +00:00
parse_again_end
2021-02-27 04:19:24 +00:00
2021-03-03 07:44:42 +00:00
field :rss2_id
2021-03-01 15:44:52 +00:00
belongs_to :member_profile
2021-02-27 04:19:24 +00:00
2022-07-26 11:15:16 +00:00
scope :sort_hash, ->{ order_by(plugin_template_sort_hash) }
2021-03-01 15:44:52 +00:00
scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(plugin_template_sort_hash) }
2021-02-27 04:19:24 +00:00
2021-03-03 07:44:42 +00:00
parse_again_start
2021-03-01 15:44:52 +00:00
member_methods_define
parse_again_end
parse_again_start
periodic_methods_define
2021-03-03 07:44:42 +00:00
parse_again_end
before_save do
before_save_codes
end
def parse_time(time_str)
DateTime.parse("0000-01-01 " + time_str)
end
2021-03-01 15:44:52 +00:00
def self.get_plugin_datas_to_member(datas)
2021-02-27 04:19:24 +00:00
fields_to_show = col_name_to_show
fields_to_remove = []
pd_title = []
fields_to_show.each do |t|
if (self.fields[t].type.to_s == "String" || self.fields[t].type.to_s == "Object" rescue false)
fields_to_remove << t if (datas.where(t.to_sym.ne => nil, t.to_sym.ne => "").count == 0 rescue false)
2021-03-01 15:44:52 +00:00
elsif (self.relations.include?(t.pluralize) rescue false)
2021-03-08 13:02:36 +00:00
fields_to_remove << t if (datas.where(t.pluralize.to_sym.ne=>[]).count == 0 rescue false)
elsif period_fields_text.include?(t)
fields_to_remove << t if (datas.select{|d| d.send(t) != " ~ " }.count == 0 rescue false)
else
2021-02-27 04:19:24 +00:00
fields_to_remove << t if (datas.where(t.to_sym.ne => nil).count == 0 rescue false)
end
pd_title << {
2021-03-01 15:44:52 +00:00
"plugin_data_title" => I18n.t("personal_plugin_template.#{t}")
2021-02-27 04:19:24 +00:00
} if !fields_to_remove.include?(t)
end
fields_to_show = fields_to_show - fields_to_remove
plugin_datas = datas.sort_for_frontend.collect.with_index do |p,index|
pd_data = []
fields_to_show.collect do |t|
2021-03-01 15:44:52 +00:00
pd_data << { "data_title" => p.display_field(t) }
2021-02-27 04:19:24 +00:00
end
{
"pd_datas" => pd_data,
"type-sort" => (p.course_category.sort_position.to_i rescue 1000),
"sort-index" => index
}
end
plugin_datas = plugin_datas.sort_by{|pd| [pd["type-sort"], pd["sort-index"]]}
return [pd_title,plugin_datas]
end
2021-03-01 15:44:52 +00:00
def get_plugin_data(fields_to_show)
2021-02-27 04:19:24 +00:00
plugin_datas = []
fields_to_show.each do |field|
plugin_data = self.get_plugin_field_data(field) rescue nil
next if plugin_data.blank? or plugin_data['value'].blank?
plugin_datas << plugin_data
end
plugin_datas
2021-03-01 15:44:52 +00:00
end
2021-02-27 04:19:24 +00:00
def get_plugin_field_data(field)
2021-03-01 15:44:52 +00:00
plugin_template = self
2021-02-27 04:19:24 +00:00
value_case_codes
value = (value =~ /\A#{URI::regexp(['http', 'https'])}\z/) ? "<a href='#{value}' target='blank'>#{value}</a>" : value
{
"key"=>field,
"title_class"=>"plugin_template-#{field.gsub('_','-')}-field",
"value_class"=>"plugin_template-#{field.gsub('_','-')}-value",
"title"=>I18n.t('personal_plugin_template.'+field),
"value"=>value
}
end
2021-03-03 07:44:42 +00:00
def display_field(field,text_only=false)
2021-03-01 15:44:52 +00:00
plugin_template = self
2021-02-27 04:19:24 +00:00
display_field_code
end
end