Browse Source

Meilleure validation d'événement

recurrent
echarp 8 years ago
parent
commit
2904e2f21e
  1. 3
      app/assets/javascripts/flash.js.coffee
  2. 2
      app/controllers/events_controller.rb
  3. 8
      app/models/event.rb
  4. 7
      app/views/events/_form.html.haml
  5. 34
      test/models/event_test.rb

3
app/assets/javascripts/flash.js.coffee

@ -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(),

2
app/controllers/events_controller.rb

@ -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

8
app/models/event.rb

@ -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

7
app/views/events/_form.html.haml

@ -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'

34
test/models/event_test.rb

@ -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

Loading…
Cancel
Save