Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra validations for links, trackers; and news #385

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
9 changes: 6 additions & 3 deletions app/models/bookmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ 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" },
Oumph marked this conversation as resolved.
Show resolved Hide resolved
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é" }
Trim marked this conversation as resolved.
Show resolved Hide resolved
validates :lang, inclusion: { in: Lang.valid_codes, allow_nil: false, message: "La langue du lien doit être définie" }

def link=(raw)
raw.strip!
return write_attribute :url, nil if raw.blank?
return write_attribute :link, nil if raw.blank?
uri = URI.parse(raw)
# Default to HTTP link if neither scheme nor host is found
if uri.scheme.blank? && uri.host.blank?
Expand Down
4 changes: 4 additions & 0 deletions app/models/lang.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def self.all
vals.zip(keys)
end

def self.valid_codes
$redis.lrange("lang", 0, -1).difference(["xx"])
end

def self.[]=(key, value)
$redis.set("lang/#{key}", value)
$redis.rpush("lang", key)
Expand Down
4 changes: 3 additions & 1 deletion app/models/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ class Link < ActiveRecord::Base
Accessible = [:id, :user, :title, :url, :lang]

validates :title, presence: { message: "Un lien doit obligatoirement avoir un titre" },
length: { maximum: 100, message: "Le titre est trop long" }
length: { maximum: 100, message: "Le titre du lien est trop long" }
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" }
validates :lang, inclusion: { in: Lang.valid_codes, allow_nil: false, message: "La langue du lien doit être définie" }


def url=(raw)
raw.strip!
Expand Down
2 changes: 2 additions & 0 deletions app/models/news.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class News < Content
dependent: :destroy,
inverse_of: :news

validates_associated :section, message: "Veuillez choisir une section pour cette dépêche"

scope :sorted, -> { order(updated_at: :desc) }
scope :draft, -> { where(state: "draft").includes(node: :user) }
scope :candidate, -> { where(state: "candidate") }
Expand Down
2 changes: 2 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Post < Content

belongs_to :forum

validates_associated :forum, message: "Vous devez choisir un forum"

scope :with_node_ordered_by, ->(order) { joins(:node).where("nodes.public = 1").order("nodes.#{order} DESC") }

validates :forum, presence: { message: "Vous devez choisir un forum" }
Expand Down
3 changes: 3 additions & 0 deletions app/models/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ class Tracker < Content
belongs_to :assigned_to_user, class_name: "User"
belongs_to :category

validates_associated :category, message: "Veuillez choisir une catégorie pour cette entrée de suivi"

attr_accessor :pot_de_miel

validates :title, presence: { message: "Le titre est obligatoire" },
length: { maximum: 100, message: "Le titre est trop long" }
validates :category, presence: { message: "Veuillez choisir une catégorie pour cette entrée de suivi" }
validates :wiki_body, presence: { message: "Veuillez décrire cette entrée du suivi" }

scope :opened, -> { where(state: "opened") }
Expand Down
4 changes: 2 additions & 2 deletions app/views/bookmarks/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
= 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
= label_tag :tags
= text_field_tag :tags, nil, class: 'autocomplete', 'data-url' => autocomplete_tags_path, value: params[:tags], size: 100
%p
= form.submit "Prévisualiser", id: "bookmark_preview"
= form.submit "Poster le lien", 'data-disable-with' => "Enregistrement en cours" if @preview_mode
= form.submit "Poster le lien", 'data-disable-with' => "Enregistrement en cours" if @preview_mode and @bookmark.valid?
2 changes: 1 addition & 1 deletion app/views/bookmarks/edit.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%main#contents(role="main")
=h1 "Éditer un lien"

= render "preview", preview: @bookmark if @preview_mode
= render "preview", preview: @bookmark if @preview_mode and @bookmark.valid?

%h2 Édition
= form_for [@bookmark.owner, @bookmark] do |form|
Expand Down
2 changes: 1 addition & 1 deletion app/views/bookmarks/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
%p
Les <a href="/regles_de_moderation">règles de modération</a> sont applicables aux liens comme au reste du site.

= render "preview", preview: @bookmark if @preview_mode
= render "preview", preview: @bookmark if @preview_mode and @bookmark.valid?

= form_for @bookmark do |form|
= render form
2 changes: 1 addition & 1 deletion app/views/comments/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
= form.text_area :wiki_body, required: 'required', spellcheck: 'true', class: 'markItUp', placeholder: "Vous pouvez écrire votre commentaire ici.\n\nMerci de rester poli et courtois, on compte sur vous !"
%p
= form.submit "Prévisualiser", id: "comment_preview"
= form.submit "Poster le commentaire", 'data-disable-with' => "Enregistrement en cours" if @preview_mode
= form.submit "Poster le commentaire", 'data-disable-with' => "Enregistrement en cours" if @preview_mode and @comment.valid?
2 changes: 1 addition & 1 deletion app/views/comments/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
=h1 "Éditer un commentaire"

%div#comments
= render "preview", preview: @comment if @preview_mode
= render "preview", preview: @comment if @preview_mode and @comment.valid?

= form_for [@comment.node, @comment] do |form|
= render form
Expand Down
2 changes: 1 addition & 1 deletion app/views/comments/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
- else
= render @comment.node.content
%div#comments
= render "preview", preview: @comment if @preview_mode
= render "preview", preview: @comment if @preview_mode and @comment.valid?

= form_for [@node, @comment], url: "/nodes/#{@node.id}/comments#comment_new" do |form|
= form.hidden_field :parent_id
Expand Down
2 changes: 1 addition & 1 deletion app/views/diaries/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
= form.label :cc_licensed, 'Je place ce document sous licence Creative Commons Attribution et Partage dans les mêmes conditions, version 4.0 (<a href="http://creativecommons.org/licenses/by-sa/4.0/deed.fr">licence CC By‑SA 4.0</a>)'.html_safe
%p
= form.submit "Prévisualiser", id: "diary_preview"
= form.submit "Poster le journal", 'data-disable-with' => "Enregistrement en cours" if @preview_mode
= form.submit "Poster le journal", 'data-disable-with' => "Enregistrement en cours" if @preview_mode and @diary.valid?
2 changes: 1 addition & 1 deletion app/views/diaries/edit.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%main#contents(role="main")
=h1 "Éditer un journal"

= render "preview", preview: @diary if @preview_mode
= render "preview", preview: @diary if @preview_mode and @diary.valid?

%h2 Édition
= form_for [@diary.owner, @diary] do |form|
Expand Down
2 changes: 1 addition & 1 deletion app/views/diaries/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
%p
Des <a href="/regles_de_moderation">règles de modération</a> sont applicables aux journaux (et au reste du site).

= render "preview", preview: @diary if @preview_mode
= render "preview", preview: @diary if @preview_mode and @diary.valid?

= form_for @diary do |form|
= render form
Expand Down
4 changes: 2 additions & 2 deletions 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" }
Copy link
Member

Choose a reason for hiding this comment

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

On a aussi besoin d'ajouter la même validation pour la section dans ce controlleur.

Si je procède à la même manipulation (enlever l'attribut required), alors, j'ai cette erreur qui apparaît:

image

Ça serait plus sympa d'avoir un bon message d'erreur du controlleur pour éviter de perdre le contenu de la dépêche soumise sans espace de rédaction :)

Copy link
Member

Choose a reason for hiding this comment

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

Théoriquement, pour valider une association à une table (donc, pour la section de news ici), on peut utiliser les propriétés: validates_associated (validation de l'appartenance à la liste) et validates_presence_of (validation de la présence de la valeur) ensemble.

Copy link
Member

Choose a reason for hiding this comment

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

J'ai ajouté "validates_associated" et j'ai changé le système de "prévisualisation" pour ne prévisualiser que si le contenu est valide.

La capture d'écran d'erreur que j'ai posté plus haut était due au fait que l'on essaie de faire la preview d'une news sans section.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On peut envoyer en Anonyme en modération une dépêche avec un lien sans langue définie.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On peut injecter des langues inconnues sur un lien de dépêche en Anonyme (et ensuite le site essaie d'afficher une image inexistante).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

En rédaction, on peut générer des 500 en envoyant des identifiants de section invalide pour la dépêche en cours.

%p
= form.label :wiki_body, "Contenu de la dépêche"
= form.text_area :wiki_body, required: 'required', spellcheck: 'true', class: 'markItUp'
Expand Down Expand Up @@ -51,4 +51,4 @@
= form.label :cc_licensed, 'Je place ce document sous licence Creative Commons Paternité - Partage des conditions initiales à l’identique, version 4.0 (<a href="http://creativecommons.org/licenses/by-sa/4.0/deed.fr">licence CC By-SA 4.0</a>). Ceci est conseillé afin de permettre l’édition coopérative de la dépêche si elle doit être complétée.'.html_safe
%p
= form.submit "Prévisualiser", id: "news_preview"
= form.submit "Soumettre cette dépêche", 'data-disable-with' => "Enregistrement en cours" if @preview_mode
= form.submit "Soumettre cette dépêche", 'data-disable-with' => "Enregistrement en cours" if @preview_mode and @news.valid?
2 changes: 1 addition & 1 deletion app/views/news/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
= list_of(news) do |news|
= link_to news.title, [:redaction, news]

= render "preview", preview: @news if @preview_mode
= render "preview", preview: @news if @preview_mode and @news.valid?

= form_for setup_news(@news), url: '/news' do |form|
= render form
Expand Down
2 changes: 1 addition & 1 deletion app/views/polls/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
= aform.text_field :answer, maxlength: 128, size: 30
%p
= form.submit "Prévisualiser", id: "poll_preview"
= form.submit "Proposer ce sondage", 'data-disable-with' => "Enregistrement en cours" if @preview_mode
= form.submit "Proposer ce sondage", 'data-disable-with' => "Enregistrement en cours" if @preview_mode and @poll.valid?
2 changes: 1 addition & 1 deletion app/views/polls/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
%p
Des <a href="/regles_de_moderation">règles de modération</a> sont applicables aux sondages (et au reste du site).

= render "preview", preview: @poll if @preview_mode
= render "preview", preview: @poll if @preview_mode and @poll.valid?

= form_for setup_poll(@poll) do |form|
= render form
2 changes: 1 addition & 1 deletion app/views/posts/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
= text_field_tag :tags, nil, class: 'autocomplete', 'data-url' => autocomplete_tags_path, value: params[:tags], size: 100
%p
= form.submit "Prévisualiser", id: "post_preview"
= form.submit "Poster le message", 'data-disable-with' => "Enregistrement en cours" if @preview_mode
= form.submit "Poster le message", 'data-disable-with' => "Enregistrement en cours" if @preview_mode and @post.valid?
2 changes: 1 addition & 1 deletion app/views/posts/edit.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%main#contents(role="main")
=h1 "Éditer un message"

= render "preview", preview: @post if @preview_mode
= render "preview", preview: @post if @preview_mode and @post.valid?

%h2 Édition
= form_for [@forum, @post] do |form|
Expand Down
2 changes: 1 addition & 1 deletion app/views/posts/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
%p
Des <a href="/regles_de_moderation">règles de modération</a> sont applicables aux forums (et au reste du site).

= render "preview", preview: @post if @preview_mode
= render "preview", preview: @post if @preview_mode and @post.valid?

= form_for @post, url: "/posts" do |form|
= render form
Expand Down
4 changes: 2 additions & 2 deletions 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" }
Copy link
Member

Choose a reason for hiding this comment

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

Je n'ai pas eu le temps de testé, mais j'imagine qu'il y aura le même problème que pour la section de news.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Encore un 500 si on soumet avec un id de catégorie inconnu.

ActiveRecord::InvalidForeignKey (Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (linux fr.trackers, CONSTRAINT fk_trackers_on_category_id FOREIGN KEY (category_id) REFERENCES categori es (id)): INSERT INTO trackers (...)

- if @tracker.new_record?
%p.pot_de_miel
= form.label :pot_de_miel, "Ne pas remplir ce champ"
Expand All @@ -23,4 +23,4 @@
= form.collection_select :assigned_to_user_id, Account.tracker_admin, :user_id, :login, include_blank: true
%p
= form.submit "Prévisualiser", id: "tracker_preview"
= form.submit "Soumettre", 'data-disable-with' => "Enregistrement en cours" if @preview_mode || @tracker.persisted?
= form.submit "Soumettre", 'data-disable-with' => "Enregistrement en cours" if (@preview_mode || @tracker.persisted?) and @tracker.valid?
2 changes: 1 addition & 1 deletion app/views/trackers/edit.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%main#contents(role="main")
=h1 "Modifier une entrée dans le suivi"

= render "preview", preview: @tracker if @preview_mode
= render "preview", preview: @tracker if @preview_mode and @tracker.valid?

%h2 Édition
= form_for @tracker do |form|
Expand Down
2 changes: 1 addition & 1 deletion app/views/trackers/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
%p
Il s’agit du suivi des suggestions et bogues concernant le site LinuxFr.org.

- if @preview_mode
- if @preview_mode and @tracker.valid?
= render "preview", preview: @tracker
- else
= image_tag "/images/dessins/geekscottes_068.png", alt: "Tu coderas pour moi !", title: "Tu coderas pour moi ! — © Johann « nojhan » Dréo, 7 novembre 2007 — Licence CC‑By‑SA 2.5"
Expand Down
2 changes: 1 addition & 1 deletion app/views/wiki_pages/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
= form.text_field :message, autocomplete: 'off', spellcheck: 'true', maxlength: 250, size: 80
%p
= form.submit "Prévisualiser", id: "wiki_preview"
= form.submit (form.object.new_record? ? "Créer" : "Mettre à jour") if @preview_mode
= form.submit (form.object.new_record? ? "Créer" : "Mettre à jour") if @preview_mode and @wiki_page.valid?
2 changes: 1 addition & 1 deletion app/views/wiki_pages/edit.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%main#contents(role="main")
=h1 "Modifier une page de wiki"

= render "preview", preview: @wiki_page if @preview_mode
= render "preview", preview: @wiki_page if @preview_mode and @wiki_page.valid?

= form_for @wiki_page do |form|
= messages_on_error @wiki_page
Expand Down
2 changes: 1 addition & 1 deletion app/views/wiki_pages/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
%p
Des <a href="/regles_de_moderation">règles de modération</a> sont applicables aux pages du wiki (et au reste du site).

= render "preview", preview: @wiki_page if @preview_mode
= render "preview", preview: @wiki_page if @preview_mode and @wiki_page.valid?

= form_for @wiki_page do |form|
= messages_on_error @wiki_page
Expand Down