From 3c474256a2556b30120a6e62970e45c95e49c76e Mon Sep 17 00:00:00 2001 From: Bohung Date: Thu, 16 Dec 2021 10:21:24 +0800 Subject: [PATCH] Fix authors show. --- app/helpers/admin/personal_books_helper.rb | 27 +++++++++++++++++++--- app/models/book.rb | 7 +++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/app/helpers/admin/personal_books_helper.rb b/app/helpers/admin/personal_books_helper.rb index 622309b..1628334 100644 --- a/app/helpers/admin/personal_books_helper.rb +++ b/app/helpers/admin/personal_books_helper.rb @@ -1,12 +1,33 @@ module Admin::PersonalBooksHelper def get_authors_text(book) - (book.authors.to_s.blank? ? get_member(book).collect(&:name).join('/') : Nokogiri::HTML(book.authors.to_s).text rescue '') + authors_text = Nokogiri::HTML(book.authors.to_s).text + split_text = authors_text.match(/[、,\/]/) + split_text = split_text.nil? ? '/' : split_text[0] + full_authors_names = get_member(book).collect(&:name) + if authors_text.present? + authors_names = authors_text.split(split_text).select{|a| !(full_authors_names.include?(full_authors_names))} + full_authors_names += authors_names + end + full_authors_names.join(split_text) end def get_authors_show(book) - (book.authors.to_s.blank? ? get_member_show(book) : book.authors.to_s) rescue '' + authors_text = Nokogiri::HTML(book.authors.to_s).text + split_text = authors_text.match(/[、,\/]/) + split_text = split_text.nil? ? '/' : split_text[0] + full_authors_names = [] + full_authors = get_member(book).collect{|member| + member_name = member.name + full_authors_names << member_name + "#{member_name}" + } + if authors_text.present? + authors_names = authors_text.split(split_text).select{|a| !(full_authors_names.include?(full_authors_names.strip()))} + full_authors += authors_names + end + full_authors.join(split_text) end def get_member(book) - Array(MemberProfile.find(Array(book).collect(&:member_profile_id))) + Array(MemberProfile.where(:id.in=>Array(book).collect(&:member_profile_id))) end def get_member_show(book) get_member(book).collect{|member| "#{member.name}"}.join('/') diff --git a/app/models/book.rb b/app/models/book.rb index 7290d9e..75f0374 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -159,9 +159,10 @@ class Book value = (self.book_author_types.collect{|type| type.title}.join(',') rescue "") when "author_name" value = [] + name_translations = self.member_profile.name_translations rescue {} ([I18n.locale]+(Site.first.in_use_locales-[I18n.locale])).each do |locale| - if self.member_profile.first_name_translations[locale] || self.member_profile.last_name_translations[locale] - value << "#{self.member_profile.first_name_translations[locale]} #{self.member_profile.last_name_translations[locale]}" + if name_translations[locale] + value << name_translations[locale] end end value = value.join(" / ") @@ -180,7 +181,7 @@ class Book when "url" value = self.url.to_s.blank? ? "" : "#{self.url}" when "authors" - value = get_member_show(self) rescue "#" + value = get_authors_show(self) rescue "" else value = (self.send(field) rescue "") end