Skip to content

Commit

Permalink
Add extra validations for links, trackers; and news
Browse files Browse the repository at this point in the history
- bookmarks: unique title, unique URL, force lang definition
- news (direct to moderation): force section definition, force lang definition on links
- news (in redaction): force lang definition on links
- trackers: for category definition
  • Loading branch information
Oumph committed Mar 10, 2024
1 parent dbc19c6 commit 703cb21
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 5 deletions.
6 changes: 4 additions & 2 deletions app/models/bookmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ class Bookmark < Content
belongs_to :owner, class_name: 'User'

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

def link=(raw)
raw.strip!
Expand Down
6 changes: 6 additions & 0 deletions app/models/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Link < ActiveRecord::Base
validates :url, http_url: { protocols: PROTOCOLS, message: "L'adresse n'est pas valide" },
presence: { message: "Un lien doit obligatoirement avoir une adresse" },
length: { maximum: 255, message: "L’adresse est trop longue" }
validate :lang_validation

def url=(raw)
raw.strip!
Expand All @@ -44,6 +45,11 @@ def url=(raw)
write_attribute :url, raw
end

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

def self.hit(id)
Expand Down
2 changes: 1 addition & 1 deletion app/views/bookmarks/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
= form.text_field :link, autocomplete: 'off', required: 'required', spellcheck: 'false', maxlength: 1024
%p
= form.label :lang, "Langue"
= form.select :lang, Lang.all
= form.select :lang, Lang.all, { include_blank: true }, { required: "required" }
%p
- if form.object.new_record?
%p
Expand Down
2 changes: 1 addition & 1 deletion app/views/news/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
= form.text_field :title, autocomplete: 'off', required: 'required', spellcheck: 'true', maxlength: 100
%p
= form.label :section_id, "Section de la dépêche"
= form.collection_select :section_id, Section.published, :id, :title
= form.collection_select :section_id, Section.published, :id, :title, { include_blank: true }, { required: "required" }
%p
= form.label :wiki_body, "Contenu de la dépêche"
= form.text_area :wiki_body, required: 'required', spellcheck: 'true', class: 'markItUp'
Expand Down
2 changes: 1 addition & 1 deletion app/views/trackers/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
= form.text_field :title, autocomplete: 'off', required: 'required', spellcheck: 'true', maxlength: 100
%p
= form.label :category_id, "Catégorie"
= form.collection_select :category_id, Category.all, :id, :title
= form.collection_select :category_id, Category.all, :id, :title, { include_blank: true }, { required: "required" }
- if @tracker.new_record?
%p.pot_de_miel
= form.label :pot_de_miel, "Ne pas remplir ce champ"
Expand Down

0 comments on commit 703cb21

Please sign in to comment.