Skip to content

Commit

Permalink
Added url validation, some todo's, minor cleanup, changed URL to url
Browse files Browse the repository at this point in the history
  • Loading branch information
BethFrank committed Jan 29, 2014
1 parent 75e7076 commit 77f800d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 deletions.
56 changes: 42 additions & 14 deletions app/models/source/bibtex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@

class Source::Bibtex < Source
include SoftValidation

# include Shared::Notable in source.rb
#
# @!attribute publisher
Expand Down Expand Up @@ -276,7 +277,7 @@ class Source::Bibtex < Source
validates_inclusion_of :month,
in: ::VALID_BIBTEX_MONTHS,
allow_nil: true,
message: '%{value} is not a valid month'
message: ' month'
validates_numericality_of :day,
allow_nil: true,
only_integer: true,
Expand All @@ -285,8 +286,16 @@ class Source::Bibtex < Source
:unless => 'year.nil? || month.nil?',
message: '%{value} is not a valid day for the month provided'

# validates :url, :format => /\A#{URI::regexp}\z/, allow_nil: true # this line is essentially the same as below
# but isn't as clear. Note that both validations allow multiple urls strung together with a ',' provided
# no spaces are included.
validates :url, :format => { :with => URI::regexp(%w(http https ftp)),
message: "[%{value}] is not a valid URL"}, allow_nil: true

before_validation :check_has_field #,:day_valid? #, :year_valid?
before_save :set_nomenclature_date
#TODO before_save set cached values

#endregion validations

# nil is last by default, exclude it explicitly with another condition if need be
Expand All @@ -311,7 +320,7 @@ class Source::Bibtex < Source
:editor,
:booktitle,
:title,
:URL,
:url,
:journal,
:year,
:stated_year
Expand Down Expand Up @@ -445,8 +454,27 @@ def isbn=(value)
self.identifiers.build(type: 'Identifier::Guid::Isbn', identifier: value)
end

#TODO if language is set => set language_id
#endregion getters & setters

def url_as_uri # turn bibtex URL field into a Ruby URI object
URI(self.url) unless self.url.blank?
end
def url_valid?
# this won't work in validation because validation methods don't allow "rescue"
debugger
return true if self.url.blank?
case URI.parse(self.url)
when URI::BadURIError, URI::InvalidURIError
false
else
true
end
end




#region has_<attribute>? section
def has_authors? # is there a bibtex author or author roles?
return true if !(self.author.blank?)
Expand Down Expand Up @@ -521,9 +549,9 @@ def generate_nomenclature_date
#region hard validations

# replaced with inclusion validation
#def check_bibtex_type # must have a valid bibtex_type
# errors.add(:bibtex_type, 'not a valid bibtex type') if !::VALID_BIBTEX_TYPES.include?(self.bibtex_type)
#end
# def check_bibtex_type # must have a valid bibtex_type
# errors.add(:bibtex_type, 'not a valid bibtex type') if !::VALID_BIBTEX_TYPES.include?(self.bibtex_type)
# end

def check_has_field # must have at least one of the required fields (TW_REQ_FIELDS)
valid = false
Expand Down Expand Up @@ -640,23 +668,23 @@ def sv_has_institution
# BETH: I don't think we need these, let's discuss (Matt)
# def sv_has_identifier
# # TODO write linkage to identifiers (rather than local field save)
# # we have URL, ISBN, ISSN & LCCN as bibtex fields, but they are also identifiers.
# # we have url, doi, isbn, & issn as bibtex fields, but they are also identifiers.
# # do need to make the linkages to identifiers as well as save in the local field?
# end
#
# def sv_has_url
# # TODO need to be converted to check for a URL identifier
# #if (self.URL.blank?)
# # soft_validations.add(:URL, 'There is no URL associated with this source.')
# #if (self.url.blank?)
# # soft_validations.add(:url, 'There is no URL associated with this source.')
# #end
# end
#
# def sv_has_note
# # TODO we may need to check of a note in the TW sense as well - has_note? above.
# if (self.note.blank?)
# soft_validations.add(:note, 'There is no note associated with this source.')
# end
# end
def sv_has_note
# TODO we may need to check of a note in the TW sense as well - has_note? above.
if (self.note.blank?)
soft_validations.add(:note, 'There is no note associated with this source.')
end
end

def sv_missing_required_bibtex_fields
case self.bibtex_type
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20140129180627_change_source_column_url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ChangeSourceColumnUrl < ActiveRecord::Migration
def change
remove_column :sources, :URL
add_column :sources, :url, :string end
end
22 changes: 20 additions & 2 deletions spec/models/source/bibtex_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
end

specify 'must have one of the following fields: :author, :booktitle, :editor, :journal,
:title, :year, :URL, :stated_year' do
:title, :year, :url, :stated_year' do
error_message = 'no core data provided'
local_src = Source::Bibtex.new()
expect(local_src.valid?).to be_false
Expand Down Expand Up @@ -280,6 +280,24 @@
end
end
end

specify 'the url must be valid' do
src = FactoryGirl.build(:valid_source_bibtex)
err = '] is not a valid URL'
expect(src.valid?).to be_true # nil url is valid
src.url = 'bad url'
expect(src.valid?).to be_false
expect(src.errors.messages[:url].include?('['+src.url+err)).to be_true
src.url = 'http://speciesfile.org'
expect(src.valid?).to be_true
src.url = 'speciesfile.org'
expect(src.valid?).to be_false
expect(src.errors.messages[:url].include?('['+src.url+err)).to be_true
src.url = 'https://google.com'
expect(src.valid?).to be_true
src.url = 'ftp://test.edu'
expect(src.valid?).to be_true
end
end

context 'instance methods - ' do
Expand Down Expand Up @@ -506,7 +524,7 @@
expect(src.year_suffix).to eq('b')
expect(src.year_with_suffix).to eq('1921b')
end
specify 'correctly converts year suffix to BibTeX enty' do
specify 'correctly converts year suffix to BibTeX entry' do
src = FactoryGirl.create(:valid_source_bibtex)
src[:year] = '1922'
src[:year_suffix] = 'c'
Expand Down

0 comments on commit 77f800d

Please sign in to comment.