fixed author sample data generation and model method

This commit is contained in:
Rueshyna 2013-03-11 14:53:21 +08:00 committed by Matt K. Fu
parent bdaef3af0f
commit a3de4cc2d4
4 changed files with 37 additions and 4 deletions

View File

@ -42,6 +42,7 @@ class WritingConference
validates :paper_title, :at_least_one => true validates :paper_title, :at_least_one => true
before_validation :add_http before_validation :add_http
after_save :save_writing_conference_files after_save :save_writing_conference_files
before_save :update_co_author
validates :url, :format => /^(http|https):\/\/(([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5})|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(:[0-9]{1,5})?(\/.*)?/i, :unless => Proc.new{self.url.blank?} validates :url, :format => /^(http|https):\/\/(([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5})|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(:[0-9]{1,5})?(\/.*)?/i, :unless => Proc.new{self.url.blank?}
attr_reader :author_tokens attr_reader :author_tokens
@ -52,7 +53,7 @@ class WritingConference
ConferenceCoAuthor.find(id).id ConferenceCoAuthor.find(id).id
rescue rescue
if id != "0" if id != "0"
new_co_author = ConferenceCoAuthor.new(:co_author => id, :name_id => create_user_id) new_co_author = ConferenceCoAuthor.new(:co_author => id)
new_co_author.save new_co_author.save
new_co_author.id new_co_author.id
else else
@ -63,6 +64,19 @@ class WritingConference
self.conference_co_author_ids = authors_ids self.conference_co_author_ids = authors_ids
end end
def update_co_author
if new_record?
current_user = self.create_user_id
else
current_user = self.update_user_id
end
self.conference_co_author_ids.each do |id|
co_author = ConferenceCoAuthor.find(id)
co_author.update_attribute(:name_id, current_user)
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_conference_category_id: category_id}).desc( :is_top, :title ) find(:all, :conditions => {writing_conference_category_id: category_id}).desc( :is_top, :title )

View File

@ -26,7 +26,7 @@ FactoryGirl.define do
"#{data_json[n]["year"]}" "#{data_json[n]["year"]}"
end end
f.sequence(:authors) do |n| f.sequence(:author_tokens) do |n|
"#{data_json[n]["author"].map{|m| m.split(",").reverse.join(" ")}.join(",")}" "#{data_json[n]["author"].map{|m| m.split(",").reverse.join(" ")}.join(",")}"
end end

View File

@ -37,18 +37,24 @@ class WritingJournal
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 #before_save :save_co_author
before_save :update_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})|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(:[0-9]{1,5})?(\/.*)?/i, :unless => Proc.new{self.url.blank?} validates :url, :format => /^(http|https):\/\/(([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5})|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(:[0-9]{1,5})?(\/.*)?/i, :unless => Proc.new{self.url.blank?}
attr_reader :author_tokens attr_reader :author_tokens
def author_tokens=(ids) def author_tokens=(ids)
if new_record?
current_user = create_user_id
else
current_user = update_user_id
end
authors_ids = ids.split(",").map{|id| authors_ids = ids.split(",").map{|id|
begin begin
JournalCoAuthor.find(id).id JournalCoAuthor.find(id).id
rescue rescue
if id != "0" if id != "0"
new_co_author = JournalCoAuthor.new(:co_author => id, :name_id => create_user_id) new_co_author = JournalCoAuthor.new(:co_author => id)
new_co_author.save new_co_author.save
new_co_author.id new_co_author.id
else else
@ -58,6 +64,19 @@ class WritingJournal
} }
self.journal_co_author_ids = authors_ids self.journal_co_author_ids = authors_ids
end end
def update_co_author
if new_record?
current_user = self.create_user_id
else
current_user = self.update_user_id
end
self.journal_co_author_ids.each do |id|
co_author = JournalCoAuthor.find(id)
co_author.update_attribute(:name_id, current_user)
end
end
# old method # old method
#def save_co_author #def save_co_author
# authors_list = authors.split(",").map{|n| n.strip.gsub(/\s+/," ")}.reject{|n| n.empty? } # authors_list = authors.split(",").map{|n| n.strip.gsub(/\s+/," ")}.reject{|n| n.empty? }

View File

@ -30,7 +30,7 @@ FactoryGirl.define do
"#{data_json[n]["year"]}" "#{data_json[n]["year"]}"
end end
f.sequence(:authors) do |n| f.sequence(:author_tokens) do |n|
"#{data_json[n]["author"].map{|m| m.split(",").reverse.join(" ")}.join(",")}" "#{data_json[n]["author"].map{|m| m.split(",").reverse.join(" ")}.join(",")}"
end end