From 89de56d94deb4088ccc2f2eb3d4cd631c5e4e58e Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Sun, 10 Aug 2014 21:17:06 +0300 Subject: [PATCH] Add an initial implementation of the Event model Initial implementation & specs of the Event model and its children --- app/models/conference.rb | 1 + app/models/event.rb | 10 ++++++ app/models/lecture.rb | 3 ++ app/models/track.rb | 1 + app/models/workshop.rb | 3 ++ db/migrate/20140810174338_create_events.rb | 20 +++++++++++ spec/factories/events.rb | 27 +++++++++++++++ spec/models/lecture_spec.rb | 23 +++++++++++++ spec/models/workshop_spec.rb | 23 +++++++++++++ .../shared_examples_for_events.rb | 33 +++++++++++++++++++ 10 files changed, 144 insertions(+) create mode 100644 app/models/event.rb create mode 100644 app/models/lecture.rb create mode 100644 app/models/workshop.rb create mode 100644 db/migrate/20140810174338_create_events.rb create mode 100644 spec/factories/events.rb create mode 100644 spec/models/lecture_spec.rb create mode 100644 spec/models/workshop_spec.rb create mode 100644 spec/support/shared_examples/shared_examples_for_events.rb diff --git a/app/models/conference.rb b/app/models/conference.rb index 393d575..37a2107 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -7,6 +7,7 @@ class Conference < ActiveRecord::Base validate :end_date_is_before_start_date has_many :tracks + has_many :events, through: :tracks private diff --git a/app/models/event.rb b/app/models/event.rb new file mode 100644 index 0000000..3cd353d --- /dev/null +++ b/app/models/event.rb @@ -0,0 +1,10 @@ +class Event < ActiveRecord::Base + validates :title, presence: true + validates :length, presence: true, numericality: {only_integer: true, greater_than: 0} + validates :abstract, presence: true + validates :description, presence: true + + belongs_to :track + has_one :conference, through: :track + belongs_to :user +end diff --git a/app/models/lecture.rb b/app/models/lecture.rb new file mode 100644 index 0000000..bdf971d --- /dev/null +++ b/app/models/lecture.rb @@ -0,0 +1,3 @@ +class Lecture < Event + validates :length, numericality: {greater_than_or_equal_to: 40, less_than_or_equal_to: 45} +end diff --git a/app/models/track.rb b/app/models/track.rb index 77ae746..6335631 100644 --- a/app/models/track.rb +++ b/app/models/track.rb @@ -1,5 +1,6 @@ class Track < ActiveRecord::Base belongs_to :conference + has_many :events validates :name, presence: true validates :color, presence: true, format: {with: /\A[a-f0-9]{6}\z/i} diff --git a/app/models/workshop.rb b/app/models/workshop.rb new file mode 100644 index 0000000..afe44aa --- /dev/null +++ b/app/models/workshop.rb @@ -0,0 +1,3 @@ +class Workshop < Event + validates :length, numericality: {greater_than_or_equal_to: 30, less_than_or_equal_to: 120} +end diff --git a/db/migrate/20140810174338_create_events.rb b/db/migrate/20140810174338_create_events.rb new file mode 100644 index 0000000..c88fe1b --- /dev/null +++ b/db/migrate/20140810174338_create_events.rb @@ -0,0 +1,20 @@ +class CreateEvents < ActiveRecord::Migration + def change + create_table :events do |t| + t.string :title, null: false + t.string :subtitle + t.string :type + t.integer :length, null: false, default: 45 + t.string :language, null: false, default: 'bg_BG' + t.integer :state + t.text :abstract, null: false + t.text :description, null: false + t.text :notes + t.references :track, index: true + t.references :user, index: true + + t.timestamps + end + add_index :events, :type + end +end diff --git a/spec/factories/events.rb b/spec/factories/events.rb new file mode 100644 index 0000000..d969b63 --- /dev/null +++ b/spec/factories/events.rb @@ -0,0 +1,27 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :lecture do + title { |n| "Lecture #{n}" } + subtitle "Lorem Ipsum" + length 45 + language "bg_BG" + abstract "An Abstract" + description "A Description" + notes "Some Notes" + track + user + end + + factory :workshop do + title { |n| "Workshop #{n}" } + subtitle "Lorem Ipsum" + length 60 + language "bg_BG" + abstract "An Abstract" + description "A Description" + notes "Some Notes" + track + user + end +end diff --git a/spec/models/lecture_spec.rb b/spec/models/lecture_spec.rb new file mode 100644 index 0000000..296eab1 --- /dev/null +++ b/spec/models/lecture_spec.rb @@ -0,0 +1,23 @@ +require 'rails_helper' + +RSpec.describe Lecture, :type => :model do + let(:event) { build :lecture } + + it_behaves_like 'an event' + + describe 'length' do + it 'must be between 40 and 45 minutes' do + event.length = 30 + expect(event).to have_error_on :length + + event.length = 60 + expect(event).to have_error_on :length + + event.length = 40 + expect(event).to_not have_error_on :length + + event.length = 45 + expect(event).to_not have_error_on :length + end + end +end diff --git a/spec/models/workshop_spec.rb b/spec/models/workshop_spec.rb new file mode 100644 index 0000000..c55dcad --- /dev/null +++ b/spec/models/workshop_spec.rb @@ -0,0 +1,23 @@ +require 'rails_helper' + +RSpec.describe Workshop, :type => :model do + let(:event) { build :workshop } + + it_behaves_like 'an event' + + describe 'length' do + it 'must be between 30 and 120 minutes' do + event.length = 20 + expect(event).to have_error_on :length + + event.length = 240 + expect(event).to have_error_on :length + + event.length = 30 + expect(event).to_not have_error_on :length + + event.length = 120 + expect(event).to_not have_error_on :length + end + end +end diff --git a/spec/support/shared_examples/shared_examples_for_events.rb b/spec/support/shared_examples/shared_examples_for_events.rb new file mode 100644 index 0000000..a3656c0 --- /dev/null +++ b/spec/support/shared_examples/shared_examples_for_events.rb @@ -0,0 +1,33 @@ +RSpec.shared_examples 'an event' do + it 'is invalid without a title' do + event.title = '' + expect(event).to have_error_on :title + end + + describe 'length' do + it 'must be present' do + event.length = '' + expect(event).to have_error_on :length + end + + it 'must be a number' do + event.length = 'foo' + expect(event).to have_error_on :length + end + + it 'must be larger than zero' do + event.length = '-10' + expect(event).to have_error_on :length + end + end + + it 'is invalid without an abstract' do + event.abstract = '' + expect(event).to have_error_on :abstract + end + + it 'is invalid without a description' do + event.description = '' + expect(event).to have_error_on :description + end +end