From 064e0a3fd753875d39c8aa6e461f6c7d0d5694cb Mon Sep 17 00:00:00 2001 From: Henne Vogelsang Date: Wed, 16 Jul 2014 00:01:40 +0200 Subject: [PATCH] Create default event_types. More event validations. Fixes #319 --- app/models/conference.rb | 16 +++++++++++++++- app/models/event.rb | 16 ++++++++-------- app/models/event_type.rb | 3 +++ 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/app/models/conference.rb b/app/models/conference.rb index 67b756ba..953fb9a4 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -91,6 +91,7 @@ class Conference < ActiveRecord::Base validates_format_of :short_title, :with => /\A[a-zA-Z0-9_-]*\z/ before_create :generate_guid before_create :create_venue + before_create :create_event_types before_create :create_email_settings before_create :add_color @@ -696,13 +697,26 @@ class Conference < ActiveRecord::Base end ## - # Creates a venue and sets self.venue_id to it's id. Used as before_create. + # Creates a Venue for this Conference. Used as before_create. # def create_venue self.venue_id = Venue.create.id true end + ## + # Creates default EventTypes for this Conference. Used as before_create. + # + def create_event_types + event_types << EventType.create(title: 'Talk', length: 30, color: '#FF0000', + minimum_abstract_length: 0, + maximum_abstract_length: 500) + event_types << EventType.create(title: 'Workshop', length: 60, color: '#0000FF', + minimum_abstract_length: 0, + maximum_abstract_length: 500) + true + end + ## # Creates a EmailSettings association proxy. Used as before_create. # diff --git a/app/models/event.rb b/app/models/event.rb index 85ffa892..078b512e 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -33,7 +33,8 @@ class Event < ActiveRecord::Base validate :biography_exists validates :title, presence: true validates :abstract, presence: true - validates :media_type, allow_nil: true, inclusion: { in: Conference.media_types.values } + validates :event_type, presence: true + validates :media_type, inclusion: { in: Conference.media_types.values }, allow_blank: true scope :confirmed, -> { where(state: 'confirmed') } @@ -181,15 +182,14 @@ class Event < ActiveRecord::Base private def abstract_limit + # If we don't have an event type, there is no need to count anything + return unless event_type len = abstract.split.size - max = event_type.maximum_abstract_length - min = event_type.minimum_abstract_length + max_words = event_type.maximum_abstract_length + min_words = event_type.minimum_abstract_length - if len < min - errors.add(:abstract, "cannot have less than #{min} words") - end - - errors.add(:abstract, "cannot have more than #{max} words") if len > max + errors.add(:abstract, "cannot have less than #{min_words} words") if len < min_words + errors.add(:abstract, "cannot have more than #{max_words} words") if len > max_words end def biography_exists diff --git a/app/models/event_type.rb b/app/models/event_type.rb index 26236b11..dfa81a71 100644 --- a/app/models/event_type.rb +++ b/app/models/event_type.rb @@ -3,7 +3,10 @@ class EventType < ActiveRecord::Base belongs_to :conference + validates :title, presence: true validates :length, :numericality => {:greater_than => 0} + validates :minimum_abstract_length, presence: true + validates :maximum_abstract_length, presence: true alias_attribute :name, :title end