Make sure the event type belongs to the parent conference

This commit is contained in:
Petko Bordjukov 2016-10-15 21:33:02 +03:00
parent bfb8c7e53d
commit 0284c8ec7e
2 changed files with 15 additions and 6 deletions

View File

@ -22,11 +22,12 @@ class Event < ActiveRecord::Base
validates :description, presence: true validates :description, presence: true
validates :agreement, acceptance: true validates :agreement, acceptance: true
validates :track, presence: 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 :language, inclusion: {in: I18n.available_locales.map(&:to_s)}, presence: true
validates :event_type, presence: true validates :event_type, presence: true
validates :length, presence: true, numericality: {only_integer: true} validates :length, presence: true, numericality: {only_integer: true}
validate :length_is_within_the_permitted_interval 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 delegate :status, to: :proposition
@ -76,6 +77,12 @@ class Event < ActiveRecord::Base
private 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 def track_belongs_to_the_selected_conference
unless conference.present? and conference.tracks.include?(track) unless conference.present? and conference.tracks.include?(track)
errors.add :track, :must_be_a_valid_track errors.add :track, :must_be_a_valid_track
@ -83,8 +90,10 @@ class Event < ActiveRecord::Base
end end
def length_is_within_the_permitted_interval def length_is_within_the_permitted_interval
if event_type.present?
unless length >= event_type.minimum_length and length <= event_type.maximum_length 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 errors.add :length, :must_be_between, minimum: event_type.minimum_length, maximum: event_type.maximum_length
end end
end end
end
end end

View File

@ -1,5 +1,5 @@
- content_for :title - 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 .row
.col-xs-12 .col-xs-12
@ -11,9 +11,9 @@
= simple_nested_form_for [:management, @conference, @event], wrapper: :horizontal_form, html: { class: 'form-horizontal' } do |f| = simple_nested_form_for [:management, @conference, @event], wrapper: :horizontal_form, html: { class: 'form-horizontal' } do |f|
= f.input :title = f.input :title
= f.input :subtitle = 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.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 :language, collection: locale_collection, include_blank: false
= f.input :abstract = f.input :abstract
= f.input :description = f.input :description