Meilleure validation d'événement
This commit is contained in:
parent
090620a873
commit
2904e2f21e
@ -1,5 +1,6 @@
|
|||||||
$(document).ready ->
|
$(document).ready ->
|
||||||
$('#flash_messages .flash').removeClass('flash').each ->
|
$('#flash_messages .flash, #error_explanation.flash').removeClass('flash').each ->
|
||||||
|
$(this).hide()
|
||||||
jQuery.gritter.add
|
jQuery.gritter.add
|
||||||
image: "/assets/#{$(this).attr('class')}.png",
|
image: "/assets/#{$(this).attr('class')}.png",
|
||||||
title: $(this).children('h2').text(),
|
title: $(this).children('h2').text(),
|
||||||
|
@ -39,6 +39,7 @@ class EventsController < InheritedResources::Base
|
|||||||
@event.region = Region.find(params[:event][:region])
|
@event.region = Region.find(params[:event][:region])
|
||||||
|
|
||||||
if params[:visu]
|
if params[:visu]
|
||||||
|
@event.valid?
|
||||||
render action: :new
|
render action: :new
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -65,6 +66,7 @@ class EventsController < InheritedResources::Base
|
|||||||
|
|
||||||
if params[:visu]
|
if params[:visu]
|
||||||
@event.attributes = event_params
|
@event.attributes = event_params
|
||||||
|
@event.valid?
|
||||||
render action: :edit
|
render action: :edit
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
@ -3,11 +3,12 @@ class Event < ActiveRecord::Base
|
|||||||
has_many :notes
|
has_many :notes
|
||||||
has_one :related_city, foreign_key: :name, primary_key: :city, class_name: City
|
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_format_of :url, with: /https?:\/\//
|
||||||
validates :contact, email: true
|
validates :contact, email: true
|
||||||
validates :submitter, email: true
|
validates :submitter, email: true
|
||||||
|
|
||||||
|
|
||||||
default_scope { where moderated: 1 }
|
default_scope { where moderated: 1 }
|
||||||
|
|
||||||
scope :past, -> { where('end_time < now()').order(start_time: :desc) }
|
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 :region, -> region { where region: region }
|
||||||
scope :tag, -> tag { where "tags like ?", "%#{tag}%" }
|
scope :tag, -> tag { where "tags like ?", "%#{tag}%" }
|
||||||
|
|
||||||
|
|
||||||
before_validation(on: :create) do
|
before_validation(on: :create) do
|
||||||
self.submission_time = Date.today
|
self.submission_time = Date.today
|
||||||
self.decision_time = Date.today
|
self.decision_time = Date.today
|
||||||
|
|
||||||
|
if self.submitter.empty?
|
||||||
|
self.submitter = self.contact
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
before_create do
|
before_create do
|
||||||
|
@ -2,11 +2,10 @@
|
|||||||
|
|
||||||
= form_for @event do |f|
|
= form_for @event do |f|
|
||||||
- if @event.errors.any?
|
- if @event.errors.any?
|
||||||
#error_explanation
|
#error_explanation.error.flash
|
||||||
%h2= "#{pluralize(@event.errors.count, "error")} prohibited this event from being saved:"
|
%h2= "#{pluralize(@event.errors.count, "error")} prohibited this event from being saved:"
|
||||||
%ul
|
- @event.errors.full_messages.each do |msg|
|
||||||
- @event.errors.full_messages.each do |msg|
|
%p= msg
|
||||||
%li= msg
|
|
||||||
|
|
||||||
- unless @event.id
|
- unless @event.id
|
||||||
= raw markdown.render t '.subtitle'
|
= raw markdown.render t '.subtitle'
|
||||||
|
@ -10,24 +10,42 @@ class EventTest < ActiveSupport::TestCase
|
|||||||
city: City.first().name,
|
city: City.first().name,
|
||||||
region: Region.first(),
|
region: Region.first(),
|
||||||
url: 'http://example.com',
|
url: 'http://example.com',
|
||||||
contact: 'test@example.com',
|
contact: 'contact@example.com',
|
||||||
submitter: 'test@example.com'
|
submitter: 'submitter@example.com'
|
||||||
)
|
)
|
||||||
assert @event.save(), @event.errors.messages
|
assert @event.save(), @event.errors.messages
|
||||||
|
|
||||||
assert_equal 32, @event.secret.size()
|
assert_equal 32, @event.secret.size()
|
||||||
assert_equal 32, @event.moderator_mail_id.size()
|
assert_equal 32, @event.moderator_mail_id.size()
|
||||||
assert_equal 32, @event.submitter_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
|
# Check invalid url
|
||||||
@event.url = 'htt://truc.com'
|
@event.url = 'htt://truc.com'
|
||||||
assert !@event.valid?
|
assert !@event.valid?, @event.errors.messages
|
||||||
|
|
||||||
@event.url = 'http:/truc.com'
|
@event.url = 'http:/truc.com'
|
||||||
assert !@event.valid?
|
assert !@event.valid?, @event.errors.messages
|
||||||
|
|
||||||
@event.contact = 'hop@@@'
|
|
||||||
assert !@event.valid?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user