This commit is contained in:
BoHung Chiu 2023-07-01 15:35:21 +08:00
parent 6e0244db2c
commit becc66f769
1 changed files with 42 additions and 7 deletions

View File

@ -31,7 +31,7 @@ class JournalPaper
# paginates_per 10 # paginates_per 10
#relations #relations
has_and_belongs_to_many :member_profile, class_name: "MemberProfile", foreign_key: 'member_profile_id' field :member_profile_id, type: Array
belongs_to :journal_paper_type belongs_to :journal_paper_type
has_and_belongs_to_many :journal_types has_and_belongs_to_many :journal_types
@ -54,9 +54,23 @@ class JournalPaper
scope :sort_year_date, ->{ order_by(:year => "desc", :publication_date => "desc", :id=>"desc") } scope :sort_year_date, ->{ order_by(:year => "desc", :publication_date => "desc", :id=>"desc") }
scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year => "desc", :publication_date => "desc", :id=>"desc") } scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year => "desc", :publication_date => "desc", :id=>"desc") }
alias_method :member_profiles, :member_profile before_save :fix_member_profile_id
alias_method :member_profiles=, :member_profile= after_save :reload_member_profiles
alias_method :org_member_profile=, :member_profile=
def fix_member_profile_id
org_member_profile_id = self[:member_profile_id]
if org_member_profile_id.class == Array
self.member_profile_id = self[:member_profile_id].map{|id| BSON::ObjectId(id)}
else
self.member_profile_id = Array(org_member_profile_id).compact.map{|id| BSON::ObjectId(id)}
end
true
end
def reload_member_profiles
@member_profiles = MemberProfile.where(:id.in=>self.member_profile_id).to_a
@member_profile = @member_profiles.first
return @member_profiles
end
def create_link def create_link
title = [] title = []
@ -215,12 +229,33 @@ class JournalPaper
plugin_datas plugin_datas
end end
def member_profile() def member_profiles
member_profiles.first @member_profiles ||= MemberProfile.where(:id.in=>self.member_profile_id).to_a
@member_profiles
end
def member_profiles=(mps)
self.member_profile_id = mps.map(&:id)
@member_profiles = mps
@member_profile = @member_profiles.first
@member_profiles
end
def member_profile
@member_profile ||= MemberProfile.where(:id.in=>self.member_profile_id).first
@member_profile
end end
def member_profile=(mp) def member_profile=(mp)
org_member_profile=(mp.class != MemberProfile ? mp : [mp]) if mp.class == MemberProfile
self.member_profile_id = [mp.id]
@member_profiles = [mp]
@member_profile = mp
else
self.member_profile_id = mp.map(&:id)
@member_profiles = mp
@member_profile = mp.first
end
end end
def get_plugin_field_data(field) def get_plugin_field_data(field)