From 9f0886af1e7f7743fd674a3c158670a08aec672c Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Fri, 17 Apr 2015 03:39:18 +0300 Subject: [PATCH] Introduce the EventType model This model will be used to define what types of events will happen during a conference. --- app/models/event_type.rb | 7 ++++ .../20150416232523_create_event_types.rb | 15 ++++++++ spec/factories/event_types.rb | 7 ++++ spec/models/event_type_spec.rb | 34 +++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 app/models/event_type.rb create mode 100644 db/migrate/20150416232523_create_event_types.rb create mode 100644 spec/factories/event_types.rb create mode 100644 spec/models/event_type_spec.rb diff --git a/app/models/event_type.rb b/app/models/event_type.rb new file mode 100644 index 0000000..5068a7f --- /dev/null +++ b/app/models/event_type.rb @@ -0,0 +1,7 @@ +class EventType < ActiveRecord::Base + belongs_to :conference + translates :name, :description + + validates :name, presence: true, uniqueness: {scope: :conference, message: :must_be_unique_for_the_conference} + validates :description, presence: true +end diff --git a/db/migrate/20150416232523_create_event_types.rb b/db/migrate/20150416232523_create_event_types.rb new file mode 100644 index 0000000..a2196da --- /dev/null +++ b/db/migrate/20150416232523_create_event_types.rb @@ -0,0 +1,15 @@ +class CreateEventTypes < ActiveRecord::Migration + def up + create_table :event_types do |t| + t.references :conference, index: true, foreign_key: true + + t.timestamps null: false + end + EventType.create_translation_table! name: :string, description: :text + end + + def down + drop_table :event_types + EventType.drop_translation_table! + end +end diff --git a/spec/factories/event_types.rb b/spec/factories/event_types.rb new file mode 100644 index 0000000..33e3327 --- /dev/null +++ b/spec/factories/event_types.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :event_type do + name { |n| "Track #{n}" } + description 'MyText' + conference + end +end diff --git a/spec/models/event_type_spec.rb b/spec/models/event_type_spec.rb new file mode 100644 index 0000000..0673653 --- /dev/null +++ b/spec/models/event_type_spec.rb @@ -0,0 +1,34 @@ +require 'rails_helper' + +RSpec.describe EventType, type: :model do + + describe 'description' do + it 'must be present' do + expect(build(:event_type, description: nil)).to have_error_on :description + end + + it 'is translatable' do + event_type = build(:event_type) + expect(event_type).to have_translatable :description + end + end + + describe 'name' do + it 'must be present' do + expect(build(:event_type, name: nil)).to have_error_on :name + expect(build(:event_type, name: '')).to have_error_on :name + end + + it 'must be unique for a event_type' do + conference = create :conference + create :event_type, name: 'foo', conference: conference + expect(build(:event_type, name: 'foo', conference: conference)).to have_error_on :name + expect(build(:event_type, name: 'foo', conference: create(:conference))).to_not have_error_on :name + end + + it 'is translatable' do + event_type = build(:event_type) + expect(event_type).to have_translatable :name + end + end +end