Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 16 additions & 46 deletions app/models/publication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def extract_bibtex_metadata(bibtex_record)
month = bibtex_record[:month].try(:to_s)
year = bibtex_record[:year].try(:to_s)
self.published_date = Date.new(bibtex_record.year.try(:to_i) || 1, bibtex_record.month_numeric || 1, bibtex_record[:day].try(:to_i) || 1)
self.published_date = nil if self.published_date.to_s == "0001-01-01"
self.published_date = nil if self.published_date.to_s == '0001-01-01'
self.doi = bibtex_record[:doi].try(:to_s)
self.pubmed_id = bibtex_record[:pubmed_id].try(:to_s)
self.booktitle = bibtex_record[:booktitle].try(:to_s)
Expand Down Expand Up @@ -326,7 +326,7 @@ def generate_citation(bibtex_record)
self.citation = ''
month = bibtex_record[:month].try(:to_s)
year = bibtex_record[:year].try(:to_s)
page_or_pages = (bibtex_record[:pages].try(:to_s).match?(/[^0-9]/) ? "pp." : "p." ) unless bibtex_record[:pages].nil?
page_or_pages = (bibtex_record[:pages].try(:to_s).match?(/[^0-9]/) ? 'pp.' : 'p.' ) unless bibtex_record[:pages].nil?
pages = bibtex_record[:pages].try(:to_s)
volume = bibtex_record[:volume].try(:to_s)
series = bibtex_record[:series].try(:to_s)
Expand All @@ -344,25 +344,16 @@ def generate_citation(bibtex_record)
url = parse_bibtex_url(bibtex_record).try(:to_s)
publication_type = PublicationType.find(self.publication_type_id)

if publication_type.is_journal?
case publication_type.key
when 'journalarticle'
self.citation += self.journal.nil? ? '':self.journal
self.citation += volume.blank? ? '': " #{volume}"
self.citation += number.nil? ? '' : "(#{number})"
self.citation += pages.blank? ? '' : (":#{pages}")
=begin
unless year.nil?
self.citation += year.nil? ? '' : (' '+year)
end
=end
elsif publication_type.is_booklet?
when 'booklet'
self.citation += howpublished.blank? ? '': "#{howpublished}"
self.citation += address.nil? ? '' : (", #{address}")
=begin
unless year.nil?
self.citation += year.nil? ? '' : (' '+year)
end
=end
elsif publication_type.is_inbook?
when 'bookchapter'
self.citation += self.booktitle.nil? ? '' : ("In #{self.booktitle}")
self.citation += volume.blank? ? '' : (", volume #{volume}")
self.citation += series.blank? ? '' : (" of #{series}")
Expand All @@ -372,14 +363,7 @@ def generate_citation(bibtex_record)
unless address.nil? || (self.booktitle.try(:include?, address))
self.citation += address.nil? ? '' : (", #{address}")
end
=begin
unless self.booktitle.try(:include?, year)
unless year.nil?
self.citation += year.nil? ? '' : (' '+year)
end
end
=end
elsif publication_type.is_inproceedings? || publication_type.is_incollection? || publication_type.is_book?
when 'conferencepaper', 'collection', 'book'
# InProceedings / InCollection
self.citation += self.booktitle.nil? ? '' : ("In #{self.booktitle}")
self.citation += volume.blank? ? '' : (", vol. #{volume}")
Expand All @@ -390,39 +374,25 @@ def generate_citation(bibtex_record)
unless address.nil? || (self.booktitle.try(:include?, address))
self.citation += address.nil? ? '' : (", #{address}")
end
=begin
unless self.booktitle.try(:include?, year)
unless year.nil?
self.citation += year.nil? ? '' : (', '+year)
end
end
=end
elsif publication_type.is_phd_thesis? || publication_type.is_masters_thesis? || publication_type.is_bachelor_thesis?
when 'bachelorsthesis', 'mastersthesis', 'phdthesis'
#PhD/Master Thesis
self.citation += school.nil? ? '' : (" #{school}")
self.errors.add(:base,'A thesis need to have a school') if school.nil?
self.citation += year.nil? ? '' : (", #{year}")
self.citation += tutor.nil? ? '' : (", #{tutor}(Tutor)")
self.citation += tutorhits.nil? ? '' : (", #{tutorhits}(HITS Tutor)")
self.citation += url.nil? ? '' : (", #{url}")
elsif publication_type.is_proceedings?
when 'conferenceproceeding'
# Proceedings are conference proceedings, it has no authors but editors
# Book
self.journal = self.title
self.citation += volume.blank? ? '' : ("vol. #{volume}")
self.citation += series.blank? ? '' : (" of #{series}")
self.citation += self.publisher.blank? ? '' : (", #{self.publisher}")
=begin
unless month.nil? && year.nil?
self.citation += self.citation.blank? ? '' : ','
self.citation += month.nil? ? '' : (' '+ month.capitalize)
self.citation += year.nil? ? '' : (' '+year)
end
=end
elsif publication_type.is_tech_report?
when 'report'
self.citation += institution.blank? ? ' ': institution
self.citation += type.blank? ? ' ' : (", #{type}")
elsif publication_type.is_unpublished?
when 'preprint'
self.citation += note.blank? ? ' ': note
end

Expand Down Expand Up @@ -583,7 +553,7 @@ def pubmed_entry
if pubmed_id
Rails.cache.fetch("bio-reference-#{pubmed_id}") do
entry = Bio::PubMed.efetch(pubmed_id).first
raise "PubMed entry was nil" if entry.nil?
raise 'PubMed entry was nil' if entry.nil?
entry
end
end
Expand Down Expand Up @@ -620,23 +590,23 @@ def check_bibtex_file (bibtex_record)


if self.title.blank?
errors.add(:base, "Please check your bibtex files, each publication should contain a title or a chapter name.")
errors.add(:base, 'Please check your bibtex files, each publication should contain a title or a chapter name.')
return false
end

if (%w[InCollection InProceedings].include? self.publication_type.title) && (bibtex_record[:booktitle].blank?)
if (['Collection','Conference Paper'].include? self.publication_type.title) && (bibtex_record[:booktitle].blank?)
Copy link

Copilot AI Nov 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Inconsistent spacing within array literals. Ruby style guides typically recommend spaces after commas in arrays for better readability:

if (['Collection', 'Conference Paper'].include? self.publication_type.title) && (bibtex_record[:booktitle].blank?)
Suggested change
if (['Collection','Conference Paper'].include? self.publication_type.title) && (bibtex_record[:booktitle].blank?)
if (['Collection', 'Conference Paper'].include? self.publication_type.title) && (bibtex_record[:booktitle].blank?)

Copilot uses AI. Check for mistakes.
errors.add(:base, "An #{self.publication_type.title} needs to have a booktitle.")
return false
end

unless %w[Booklet Manual Misc Proceedings].include? self.publication_type.title
unless ['Booklet','Text','Other','Conference Proceeding'].include? self.publication_type.title
if bibtex_record[:author].nil? && self.editor.nil?
self.errors.add(:base, "You need at least one author or editor for the #{self.publication_type.title}.")
return false
end
end

if self.publication_type.is_phd_thesis? || self.publication_type.is_masters_thesis? || self.publication_type.is_bachelor_thesis?
if publication_type.key == 'bachelorsthesis' || publication_type.key == 'mastersthesis' || publication_type.key == 'phdthesis'
if bibtex_record[:school].try(:to_s).nil?
self.errors.add(:base,"A #{self.publication_type.title} needs to have a school.")
return false
Expand Down
181 changes: 45 additions & 136 deletions app/models/publication_type.rb
Original file line number Diff line number Diff line change
@@ -1,143 +1,52 @@
class PublicationType < ActiveRecord::Base

class PublicationType < ActiveRecord::Base
has_many :publications

#this returns an instance of PublicationType according to one of the publication types
#if there is not a match nil is returned

#http://bib-it.sourceforge.net/help/fieldsAndEntryTypes.php#Entries

def self.for_type type
keys = { "Journal"=>"article",
"Book"=>"book",
"Booklet"=>"booklet",
"InBook"=>"inbook",
"InCollection"=>"incollection",
"InProceedings"=>"inproceedings",
"Manual"=>"manual",
"Masters_Thesis"=>"masters_thesis",
"Misc"=>"misc",
"Phd_Thesis"=>"phd_thesis",
"Bachelor_Thesis"=>"bachelorsthesis",
"Proceedings"=>"proceedings",
"Tech_Report"=>"tech_report",
"Unpublished"=>"unpublished" }
return PublicationType.find_by(key: keys[type])
end



# Map BibTeX keys → DataCite keys
BIBTEX_TO_DATACITE_KEY = {
'article' => 'journalarticle',
'book' => 'book',
'booklet' => 'booklet',
'inbook' => 'bookchapter',
'incollection' => 'collection',
'inproceedings' => 'conferencepaper',
'proceedings' => 'conferenceproceeding',
'manual' => 'text',
'misc' => 'other',
'unpublished' => 'preprint',
'techreport' => 'report',
'phdthesis' => 'phdthesis',
'mastersthesis' => 'mastersthesis',
'bachelorsthesis' => 'bachelorsthesis'
}.freeze


# Load expected publication types from YAML
def self.type_registry
@type_registry ||= begin
path = Rails.root.join('config/default_data/publication_types.yml')
YAML.load_file(path).values.index_by { |h| h['title'] }
end
end

def self.for_type(type)
yaml_entry = type_registry[type]
return nil unless yaml_entry
find_by(key: yaml_entry['key'])
end

# Extract publication type from BibTeX record
def self.get_publication_type_id(bibtex_record)
str_begin = "@"
str_end = "{"
publication_key = bibtex_record.try(:to_s)[/#{str_begin}(.*?)#{str_end}/m, 1]
unless PublicationType.find_by(key: publication_key).nil?
return PublicationType.find_by(key: publication_key).id
else
return PublicationType.find_by(key: "misc").id
end
end


def self.Journal
self.for_type('Journal')
end

def self.Book
self.for_type('Book')
end

def self.Booklet
self.for_type('Booklet')
end
def self.InBook
self.for_type('InBook')
end
def self.InCollection
self.for_type('InCollection')
end
def self.InProceedings
self.for_type('InProceedings')
end
def self.Manual
self.for_type('Manual')
end
def self.Masters_Thesis
self.for_type('Masters_Thesis')
end
def self.Misc
self.for_type('Misc')
end
def self.Bachelor_Thesis
self.for_type('Bachelor_Thesis')
end
def self.Phd_Thesis
self.for_type('Phd_Thesis')
end
def self.Proceedings
self.for_type('Proceedings')
end
def self.Tech_Report
self.for_type('Tech_Report')
end
def self.Unpublished
self.for_type('Unpublished')
end

def is_journal?
key == "article"
# Extract the BibTeX entry type, e.g. article, inbook, misc...
publication_key = bibtex_record.to_s[/@(.*?)\{/m, 1].to_s.downcase.strip
datacite_key = BIBTEX_TO_DATACITE_KEY[publication_key] || 'other'
pub_type = PublicationType.find_by(key: datacite_key)
pub_type.id
end

def is_book?
key == 'book'
end

def is_booklet?
key == 'booklet'
end

def is_inbook?
key == 'inbook'
end

def is_incollection?
key == 'incollection'
end

def is_inproceedings?
key == 'inproceedings'
end

def is_manual?
key == 'manual'
end

def is_masters_thesis?
key == 'mastersthesis'
end

def is_misc?
key == 'misc'
end

def is_phd_thesis?
key == 'phdthesis'
end

def is_bachelor_thesis?
key == 'bachelorsthesis'
end

def is_proceedings?
key == 'proceedings'
end

def is_tech_report?
key == 'techreport'
end

def is_unpublished?
key == 'unpublished'
type_registry.each_key do |title|
define_singleton_method(title.gsub(/\s+/, '')) do
find_by(key: type_registry[title]['key'])
end
end

end
end
4 changes: 2 additions & 2 deletions app/views/publications/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<span id="publication_type_info" style="display:none;"></span>
<span id="publication_type_selection">
<%= collection_select :publication, :publication_type_id, PublicationType.order(:title), :id, :title,
{:selected => PublicationType.Journal.id},
{:selected => PublicationType.JournalArticle.id},
{:class => 'form-control'} -%>
</span>
</div>
Expand Down Expand Up @@ -79,7 +79,7 @@
<span id="publication_type_info" style="display:none;"></span>
<span id="publication_type_selection">
<%= collection_select :publication, :publication_type_id, PublicationType.order(:title), :id, :title,
{:selected => PublicationType.Journal.id},
{:selected => PublicationType.JournalArticle.id},
{:class => 'form-control'} -%>
</span>
</div>
Expand Down
Loading
Loading