Skip to content

Commit d6ba4d0

Browse files
committed
Add extra validations for links, trackers; and news
- bookmarks: unique title, unique URL, force lang definition - news (direct to moderation): force section definition, force lang definition on links, different messages for max length of news title and link title - news (in redaction): force lang definition on links, different messages for max length of news title and link title - trackers: for category definition
1 parent 82d265e commit d6ba4d0

File tree

5 files changed

+14
-6
lines changed

5 files changed

+14
-6
lines changed

app/models/bookmark.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ class Bookmark < Content
2424
belongs_to :owner, class_name: 'User'
2525

2626
validates :title, presence: { message: "Le titre est obligatoire" },
27-
length: { maximum: 100, message: "Le titre est trop long" }
27+
length: { maximum: 100, message: "Le titre est trop long" },
28+
uniqueness: { message: "Un lien avec le même titre a déjà été proposé" }
2829
validates :link, presence: { message: "Vous ne pouvez pas poster un lien vide" },
2930
http_url: { message: "Le lien n'est pas valide" },
30-
length: { maximum: 255, message: "Le lien est trop long" }
31+
length: { maximum: 255, message: "Le lien est trop long" },
32+
uniqueness: { message: "Le lien a déjà été proposé" }
3133

3234
def link=(raw)
3335
raw.strip!

app/models/link.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ class Link < ActiveRecord::Base
2525
Accessible = [:id, :user, :title, :url, :lang]
2626

2727
validates :title, presence: { message: "Un lien doit obligatoirement avoir un titre" },
28-
length: { maximum: 100, message: "Le titre est trop long" }
28+
length: { maximum: 100, message: "Le titre du lien est trop long" }
2929
validates :url, http_url: { protocols: PROTOCOLS, message: "L'adresse n'est pas valide" },
3030
presence: { message: "Un lien doit obligatoirement avoir une adresse" },
3131
length: { maximum: 255, message: "L’adresse est trop longue" }
32+
validate :lang_validation
3233

3334
def url=(raw)
3435
raw.strip!
@@ -44,6 +45,11 @@ def url=(raw)
4445
write_attribute :url, raw
4546
end
4647

48+
def lang_validation
49+
if lang == "xx"
50+
errors.add(:lang, "La langue du lien doit être définie") unless title.blank? or url.blank?
51+
end
52+
end
4753
### Behaviour ###
4854

4955
def self.hit(id)

app/views/bookmarks/_form.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
= form.text_field :link, autocomplete: 'off', required: 'required', spellcheck: 'false', maxlength: 1024
99
%p
1010
= form.label :lang, "Langue"
11-
= form.select :lang, Lang.all
11+
= form.select :lang, Lang.all, { include_blank: true }, { required: "required" }
1212
%p
1313
- if form.object.new_record?
1414
%p

app/views/news/_form.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
= form.text_field :title, autocomplete: 'off', required: 'required', spellcheck: 'true', maxlength: 100
1717
%p
1818
= form.label :section_id, "Section de la dépêche"
19-
= form.collection_select :section_id, Section.published, :id, :title
19+
= form.collection_select :section_id, Section.published, :id, :title, { include_blank: true }, { required: "required" }
2020
%p
2121
= form.label :wiki_body, "Contenu de la dépêche"
2222
= form.text_area :wiki_body, required: 'required', spellcheck: 'true', class: 'markItUp'

app/views/trackers/_form.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
= form.text_field :title, autocomplete: 'off', required: 'required', spellcheck: 'true', maxlength: 100
66
%p
77
= form.label :category_id, "Catégorie"
8-
= form.collection_select :category_id, Category.all, :id, :title
8+
= form.collection_select :category_id, Category.all, :id, :title, { include_blank: true }, { required: "required" }
99
- if @tracker.new_record?
1010
%p.pot_de_miel
1111
= form.label :pot_de_miel, "Ne pas remplir ce champ"

0 commit comments

Comments
 (0)