Meilleure validation d'événement

This commit is contained in:
echarp 2014-05-31 23:13:43 +02:00
parent 090620a873
commit 2904e2f21e
5 changed files with 40 additions and 14 deletions

View File

@ -1,5 +1,6 @@
$(document).ready ->
$('#flash_messages .flash').removeClass('flash').each ->
$('#flash_messages .flash, #error_explanation.flash').removeClass('flash').each ->
$(this).hide()
jQuery.gritter.add
image: "/assets/#{$(this).attr('class')}.png",
title: $(this).children('h2').text(),

View File

@ -39,6 +39,7 @@ class EventsController < InheritedResources::Base
@event.region = Region.find(params[:event][:region])
if params[:visu]
@event.valid?
render action: :new
return
end
@ -65,6 +66,7 @@ class EventsController < InheritedResources::Base
if params[:visu]
@event.attributes = event_params
@event.valid?
render action: :edit
return
end

View File

@ -3,11 +3,12 @@ class Event < ActiveRecord::Base
has_many :notes
has_one :related_city, foreign_key: :name, primary_key: :city, class_name: City
validates_presence_of :title, :description, :city, :region, :url, :contact, :submitter
validates_presence_of :title, :description, :city, :region, :url, :contact
validates_format_of :url, with: /https?:\/\//
validates :contact, email: true
validates :submitter, email: true
default_scope { where moderated: 1 }
scope :past, -> { where('end_time < now()').order(start_time: :desc) }
@ -28,9 +29,14 @@ class Event < ActiveRecord::Base
scope :region, -> region { where region: region }
scope :tag, -> tag { where "tags like ?", "%#{tag}%" }
before_validation(on: :create) do
self.submission_time = Date.today
self.decision_time = Date.today
if self.submitter.empty?
self.submitter = self.contact
end
end
before_create do

View File

@ -2,11 +2,10 @@
= form_for @event do |f|
- if @event.errors.any?
#error_explanation
#error_explanation.error.flash
%h2= "#{pluralize(@event.errors.count, "error")} prohibited this event from being saved:"
%ul
- @event.errors.full_messages.each do |msg|
%li= msg
- @event.errors.full_messages.each do |msg|
%p= msg
- unless @event.id
= raw markdown.render t '.subtitle'

View File

@ -10,24 +10,42 @@ class EventTest < ActiveSupport::TestCase
city: City.first().name,
region: Region.first(),
url: 'http://example.com',
contact: 'test@example.com',
submitter: 'test@example.com'
contact: 'contact@example.com',
submitter: 'submitter@example.com'
)
assert @event.save(), @event.errors.messages
assert_equal 32, @event.secret.size()
assert_equal 32, @event.moderator_mail_id.size()
assert_equal 32, @event.submitter_mail_id.size()
end
test "validations" do
@event = Event.new(
title: 'hello world',
start_time: Time.new(),
end_time: Time.new() + 1,
description: 'et hop!',
city: City.first().name,
region: Region.first(),
url: 'http://example.com',
contact: 'contact@example.com'
)
assert @event.valid?, @event.errors.messages
assert_equal @event.contact, @event.submitter
@event.contact = 'hop@@@'
assert !@event.valid?, @event.errors.messages
@event.contact = 'contact@example.com'
assert @event.valid?, @event.errors.messages
# Check invalid url
@event.url = 'htt://truc.com'
assert !@event.valid?
assert !@event.valid?, @event.errors.messages
@event.url = 'http:/truc.com'
assert !@event.valid?
@event.contact = 'hop@@@'
assert !@event.valid?
assert !@event.valid?, @event.errors.messages
end
end