if coauthor not in coauthor db then it will be add in db

This commit is contained in:
Rueshyna 2012-11-29 22:37:13 +08:00 committed by chris
parent 7f7b33f310
commit eb97d417bd
2 changed files with 20 additions and 2 deletions

View File

@ -9,11 +9,12 @@ class CoAuthor
field :type 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 }, validates :email, format: { with: VALID_EMAIL_REGEX },
allow_blank: true,
uniqueness: { case_sensitive: false } uniqueness: { case_sensitive: false }
validates :co_author, presence: true 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 end

View File

@ -35,9 +35,26 @@ class WritingJournal
accepts_nested_attributes_for :writing_journal_files, :allow_destroy => true accepts_nested_attributes_for :writing_journal_files, :allow_destroy => true
after_save :save_writing_journal_files after_save :save_writing_journal_files
before_validation :add_http before_validation :add_http
before_save :save_co_author
validates :paper_title, :at_least_one => true 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?} 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 ) def self.search( category_id = nil )
if category_id.to_s.size > 0 if category_id.to_s.size > 0
find(:all, :conditions => {writing_journal_category_id: category_id}).desc( :is_top, :title ) find(:all, :conditions => {writing_journal_category_id: category_id}).desc( :is_top, :title )