From 0284c8ec7eb4696bcbd9de91ff676b2b534fc687 Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Sat, 15 Oct 2016 21:33:02 +0300 Subject: [PATCH] Make sure the event type belongs to the parent conference --- app/models/event.rb | 15 ++++++++++++--- app/views/management/events/edit.html.slim | 6 +++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/app/models/event.rb b/app/models/event.rb index 9d3d854..5934622 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -22,11 +22,12 @@ class Event < ActiveRecord::Base validates :description, presence: true validates :agreement, acceptance: true validates :track, presence: true - validate :track_belongs_to_the_selected_conference validates :language, inclusion: {in: I18n.available_locales.map(&:to_s)}, presence: true validates :event_type, presence: true validates :length, presence: true, numericality: {only_integer: true} validate :length_is_within_the_permitted_interval + validate :track_belongs_to_the_selected_conference + validate :event_type_belongs_to_the_selected_conference delegate :status, to: :proposition @@ -76,6 +77,12 @@ class Event < ActiveRecord::Base private + def event_type_belongs_to_the_selected_conference + unless conference.present? and conference.event_types.include?(event_type) + errors.add :event_type, :must_be_a_valid_event_type + end + end + def track_belongs_to_the_selected_conference unless conference.present? and conference.tracks.include?(track) errors.add :track, :must_be_a_valid_track @@ -83,8 +90,10 @@ class Event < ActiveRecord::Base end def length_is_within_the_permitted_interval - unless length >= event_type.minimum_length and length <= event_type.maximum_length - errors.add :length, :must_be_between, minimum: event_type.minimum_length, maximum: event_type.maximum_length + if event_type.present? + unless length >= event_type.minimum_length and length <= event_type.maximum_length + errors.add :length, :must_be_between, minimum: event_type.minimum_length, maximum: event_type.maximum_length + end end end end diff --git a/app/views/management/events/edit.html.slim b/app/views/management/events/edit.html.slim index e55003d..bdad53b 100644 --- a/app/views/management/events/edit.html.slim +++ b/app/views/management/events/edit.html.slim @@ -1,5 +1,5 @@ - content_for :title - = t('.edit', event_type: @event.event_type.name.mb_chars.downcase, event_title: @event.title) + = t('.edit', event_type: @event.event_type.try(:name).try(:mb_chars).try(:downcase), event_title: @event.title) .row .col-xs-12 @@ -11,9 +11,9 @@ = simple_nested_form_for [:management, @conference, @event], wrapper: :horizontal_form, html: { class: 'form-horizontal' } do |f| = f.input :title = f.input :subtitle - = f.association :event_type + = f.association :event_type, collection: current_conference.event_types = f.association :track, collection: current_conference.tracks.map { |track| [track.name, track.id, {title: track.description}] } - = f.input :length, hint: t('simple_form.hints.event.length', type: @event.event_type.name.mb_chars.downcase, min: @event.event_type.minimum_length, max: @event.event_type.maximum_length) + = f.input :length = f.input :language, collection: locale_collection, include_blank: false = f.input :abstract = f.input :description