Skip to content

Commit

Permalink
autostrip other urls before validation
Browse files Browse the repository at this point in the history
  • Loading branch information
stuzart committed Oct 14, 2021
1 parent 8085bc5 commit c4870eb
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 19 deletions.
24 changes: 12 additions & 12 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,29 @@ class Event < ApplicationRecord
# load the configuration for the pagination
grouped_pagination

validate :validate_data_files
def validate_data_files
df = data_files.to_a
errors.add(:data_files, 'May only contain one association to each data file') unless (df.count == df.uniq.count)
end

validate :validate_end_date
def validate_end_date
errors.add(:end_date, 'is before start date.') unless end_date.nil? || start_date.nil? || end_date >= start_date
end
auto_strip_attributes :url

validates_presence_of :title
validates :title, length: { maximum: 255 }

validates :description, length: { maximum: 65_535 }

validates_presence_of :start_date

# validates_is_url_string :url
validates :url, url: {allow_nil: true, allow_blank: true}

validates :country, country:true, allow_blank: true

validate :validate_data_files
def validate_data_files
df = data_files.to_a
errors.add(:data_files, 'May only contain one association to each data file') unless (df.count == df.uniq.count)
end

validate :validate_end_date
def validate_end_date
errors.add(:end_date, 'is before start date.') unless end_date.nil? || start_date.nil? || end_date >= start_date
end

def show_contributor_avatars?
false
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/institution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class Institution < ApplicationRecord
acts_as_yellow_pages
title_trimmer

auto_strip_attributes :web_page

validates :title, uniqueness: true
validates :web_page, url: { allow_nil: true, allow_blank: true }
validates :country, country: true
Expand Down
2 changes: 1 addition & 1 deletion app/models/organism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Organism < ApplicationRecord

has_and_belongs_to_many :projects
has_many :programmes, through: :projects

before_validation :convert_concept_uri

validates_presence_of :title
Expand Down
2 changes: 2 additions & 0 deletions app/models/programme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Programme < ApplicationRecord
end
accepts_nested_attributes_for :projects

auto_strip_attributes :web_page

# validations
validates :title, uniqueness: true
validates :title, length: { maximum: 255 }
Expand Down
2 changes: 2 additions & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class Project < ApplicationRecord

scope :without_programme, -> { where('programme_id IS NULL') }

auto_strip_attributes :web_page, :wiki_page

validates :web_page, url: {allow_nil: true, allow_blank: true}
validates :wiki_page, url: {allow_nil: true, allow_blank: true}

Expand Down
2 changes: 2 additions & 0 deletions app/models/sample_controlled_vocab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class SampleControlledVocab < ApplicationRecord
has_many :samples, through: :sample_types
belongs_to :repository_standard, inverse_of: :sample_controlled_vocabs

auto_strip_attributes :ols_root_term_uri

validates :title, presence: true, uniqueness: true
validates :ols_root_term_uri, url: { allow_blank: true }

Expand Down
21 changes: 21 additions & 0 deletions test/unit/event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ def setup
assert @event.save
end

test 'validates and tests url' do
@event.url = nil
assert @event.valid?

@event.url = ''
assert @event.valid?

@event.url = 'fish'
refute @event.valid?

@event.url = 'http://google.com'
assert @event.valid?

@event.url = 'https://google.com'
assert @event.valid?

@event.url = ' http://google.com '
assert @event.valid?
assert_equal 'http://google.com', @event.url
end

test 'start date required' do
@event.start_date = nil
assert !@event.valid?
Expand Down
7 changes: 6 additions & 1 deletion test/unit/institution_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_can_be_edited_by
assert i.can_edit?(u), "Institution :one should be editable by this user, as he's an admin"
end

def test_valid
test 'validation' do
i = Factory(:institution)
assert i.valid?

Expand Down Expand Up @@ -90,6 +90,11 @@ def test_valid
i.web_page = 'https://google.com'
assert i.valid?

# gets stripped before validation
i.web_page = ' https://google.com '
assert i.valid?
assert_equal 'https://google.com', i.web_page

i.web_page = 'http://google.com/fred'
assert i.valid?

Expand Down
4 changes: 0 additions & 4 deletions test/unit/person_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -658,10 +658,6 @@ def test_valid
refute p.valid?
assert_equal 1,p.errors.full_messages.count
assert_equal "Full name can't be blank",p.errors.full_messages.first




end

def test_email_with_capitalise_valid
Expand Down
5 changes: 5 additions & 0 deletions test/unit/programme_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class ProgrammeTest < ActiveSupport::TestCase
assert p.valid?
p.web_page = 'https://google.com'
assert p.valid?

# strips before validation
p.web_page = ' https://google.com '
assert p.valid?
assert_equal 'https://google.com', p.web_page
end

test 'validate title and decription length' do
Expand Down
12 changes: 11 additions & 1 deletion test/unit/project_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def test_update_first_letter
assert_equal 'T', p.first_letter
end

def test_valid
test 'validation' do
p = projects(:one)

p.web_page = nil
Expand All @@ -446,6 +446,11 @@ def test_valid
p.web_page = 'https://google.com'
assert p.valid?

# gets stripped before validation
p.web_page = ' https://google.com '
assert p.valid?
assert_equal 'https://google.com', p.web_page

p.web_page = 'http://google.com/fred'
assert p.valid?

Expand All @@ -470,6 +475,11 @@ def test_valid
p.wiki_page = 'https://google.com'
assert p.valid?

# gets stripped before validation
p.wiki_page = ' https://google.com '
assert p.valid?
assert_equal 'https://google.com', p.wiki_page

p.wiki_page = 'http://google.com/fred'
assert p.valid?

Expand Down

0 comments on commit c4870eb

Please sign in to comment.