From eb97d417bd2f264de4e4e25f4f51f1402809f47c Mon Sep 17 00:00:00 2001 From: Rueshyna Date: Thu, 29 Nov 2012 22:37:13 +0800 Subject: [PATCH] if coauthor not in coauthor db then it will be add in db --- app/models/user/co_author.rb | 5 +++-- .../app/models/writing_journal.rb | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/models/user/co_author.rb b/app/models/user/co_author.rb index ab7c75a5..f85d7839 100644 --- a/app/models/user/co_author.rb +++ b/app/models/user/co_author.rb @@ -9,11 +9,12 @@ class CoAuthor field :type - VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i + VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/ validates :email, format: { with: VALID_EMAIL_REGEX }, + allow_blank: true, uniqueness: { case_sensitive: false } validates :co_author, presence: true - before_save { |coauthor| coauthor.email = email.downcase } + before_save { |coauthor| coauthor.email = email.downcase if not coauthor.email.nil?} end diff --git a/vendor/built_in_modules/personal_journal/app/models/writing_journal.rb b/vendor/built_in_modules/personal_journal/app/models/writing_journal.rb index 49dc0015..f9127758 100644 --- a/vendor/built_in_modules/personal_journal/app/models/writing_journal.rb +++ b/vendor/built_in_modules/personal_journal/app/models/writing_journal.rb @@ -35,9 +35,26 @@ class WritingJournal accepts_nested_attributes_for :writing_journal_files, :allow_destroy => true after_save :save_writing_journal_files before_validation :add_http + before_save :save_co_author validates :paper_title, :at_least_one => true validates :url, :format => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix, :unless => Proc.new{self.url.blank?} + def save_co_author + authors_list = authors.split(",").map{|n| n.strip.gsub(/\s+/," ")}.reject{|n| n.empty? } + if new_record? + current_user = create_user_id + else + current_user = update_user_id + end + + authors_db = CoAuthor.where(:name_id => current_user).map(&:co_author) + authors_list.delete(User.find(current_user).name) + authors_list = authors_list.delete_if{|author| authors_db.include?author} + authors_list.each do |author| + CoAuthor.new(:co_author => author, :name_id => current_user).save + end + end + def self.search( category_id = nil ) if category_id.to_s.size > 0 find(:all, :conditions => {writing_journal_category_id: category_id}).desc( :is_top, :title )